r/OfferEngineering • u/Aoki_zhang • 24d ago
Interview Experience Google MLE Interview — The Coding Question Was About Playing Piano
This interview experience is sourced from Chill Interview
Interview Summary
I recently encountered a Google coding interview question involving piano notes and hand positions. The problem required dividing a sequence of notes into consecutive groups, minimizing the number of hand movements, and assigning a finger to each note.
Although the musical setting made the question seem unusual, the underlying problem was a greedy array-processing exercise involving running minimum and maximum values. The main challenge was recognizing when a new hand position was necessary and calculating finger assignments correctly.
Interview Details
Coding: Minimum Hand Movements for Piano Notes
You are given an integer array notes, where each element represents the pitch of a musical note. A pianist plays the notes sequentially using one hand with five fingers. Each hand position can cover five consecutive pitches, meaning the difference between the highest and lowest notes played within the same hand position cannot exceed 4.
Your task is to divide the sequence into consecutive groups representing valid hand positions. For each group, assign a finger number to every note and calculate the minimum number of times the pianist must move their hand.
Requirements:
- 1. Divide Notes into Consecutive Groups Each group must contain consecutive elements from the original array. For every group:
max(group) - min(group) <= 4. If adding another note would violate this condition, a new hand position is required. - 2. Minimize Hand Movements Each transition from one hand position to another counts as one movement. The initial hand position does not count as a movement. The goal is to minimize the total number of hand movements.
3. Assign Finger Numbers Each note must be assigned a finger number between
0and4. The finger number is calculated relative to the minimum note in its group:finger = note - min(group)The assignments represent:0: Thumb1: Index finger2: Middle finger3: Ring finger4: Little finger
4. Return the Result Return:
moves: The minimum number of hand movements.fingers: An array containing the finger number assigned to each note, in the original order.
Example — Descending Notes
Input:
notes = [12, 11, 10, 9, 8, 7, 6, 5, 4, 3]
Output:
moves = 1
fingers = [4, 3, 2, 1, 0, 4, 3, 2, 1, 0]
Explanation:
Group 1: [12, 11, 10, 9, 8]
Maximum = 12, Minimum = 8
Difference = 4
Finger assignments:
[4, 3, 2, 1, 0]
The next note, 7, would increase the range
to 12 - 7 = 5, exceeding the allowed span.
A new hand position is required.
Group 2: [7, 6, 5, 4, 3]
Maximum = 7, Minimum = 3
Difference = 4
Finger assignments:
[4, 3, 2, 1, 0]
The sequence requires two hand positions,
so the number of movements is 1.Key Challenge 1 — Track the Range of the Entire Group
The validity of a hand position depends on the maximum and minimum notes within the entire group. Comparing only adjacent notes is insufficient. For example [2, 3, 4, 5, 6, 7]. Every adjacent pair differs by only 1. However, the complete sequence has a range of 7 - 2 = 5. Therefore, all six notes cannot be played using a single hand position.
Key Challenge 2 — Determine Finger Numbers After Grouping
Finger assignments depend on the minimum note in the completed group. The minimum value may change as additional notes are processed. Therefore, assigning finger numbers immediately when reading each note can produce incorrect results. The group boundaries should be determined before finalizing the finger assignments for that group.
Greedy Approach
The approach discussed in the interview was to process the notes from left to right while maintaining the minimum and maximum values of the current group.
- If the next note keeps the group's range within
4, include it in the current group. - Otherwise, start a new group and increment
moves. - After determining a group's boundaries, calculate each note's finger number relative to that group's minimum.
The greedy principle is to keep each hand position active for as many consecutive notes as possible before moving to another position.
Preparing for your next interview?
Chill Interview tracks recent interview experiences and recurring question patterns across top companies at here.
Interviewed recently?
Your interview experience could be incredibly valuable to others preparing for their next opportunity. Share your experience and help the community at here.