← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Google SWE coding round with a piano hand movement problem that sounds cute until you actually have to think about it carefully. The follow-up added a printing requirement that changed how you'd want to structure the solution.

Questions Asked (1)

Q1

You're given an integer array. Imagine placing one hand on a piano keyboard. The hand can cover a range of consecutive keys at any position. Count the minimum number of times you need to reposition the hand to play all the keys in the array. For example, [1,2,3,4,5] requires 0 moves, [5,9,1] requires 1 move. Follow-up: each time you move the hand, print the group of keys played by the previous hand position, and also print the final group at the end.

Algorithms & Data Structures
Author's notes

The base case clicked pretty fast.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the hand as a sliding window of fixed size (the hand span) over the sorted unique keys, and greedily extend the window as far as possible to cover consecutive keys. Count the number of windows needed to cover all keys; each new window after the first represents a hand reposition. For the follow-up, record the keys covered by each window and print them when moving to the next window, plus the final window at the end.

Pro tip: Clarify the hand span upfront—it's a crucial parameter that might be implied by the example (e.g., span=5 for [1,2,3,4,5]). Also, discuss edge cases like duplicate keys, unsorted input, and keys outside the piano range to show thoroughness.

1. Clarify the problem and constraints

Ask about the hand span (number of keys the hand can cover), whether the array can contain duplicates, and if the keys are within a standard piano range (e.g., 1-88). Confirm that the hand can be placed anywhere on the keyboard.

2. Sort and deduplicate the keys

Sort the array in ascending order and remove duplicates, as playing the same key multiple times doesn't require repositioning. This simplifies the problem to covering a set of unique keys with intervals of length equal to the hand span.

3. Greedy interval covering

Initialize a window starting at the smallest key. Extend the window to cover as many consecutive keys as possible within the hand span. When the next key exceeds the current window's end, increment the move count and start a new window at that key. Repeat until all keys are covered.

4. Handle the follow-up: record and print groups

During the greedy process, store the keys covered by each window. When moving to a new window, print the previous window's keys. After processing all keys, print the final window's keys.

5. Analyze complexity and test edge cases

State that sorting takes O(n log n) time and the greedy pass takes O(n) time, so overall O(n log n). Test with edge cases: empty array, single key, all keys within one span, keys exactly at span boundaries, and large gaps.

Key Points to Mention

  • Hand span is a fixed parameter; clarify its value if not given.
  • Sorting and deduplicating the input simplifies the problem.
  • Greedy algorithm: always cover as many keys as possible with the current hand position.
  • Move count is the number of windows minus one (or zero if all keys fit in one window).
  • For the follow-up, maintain a list of groups and print them appropriately.
  • Time complexity: O(n log n) due to sorting; space complexity: O(n) for storing groups.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.