Machine Learning Engineer • Junior-Level
Machine Learning Engineer • Junior-Level
High QualityPrep Other Coding Questions Asked by Google
Continue with targeted prep from Google's coding question bank.
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
Unlock Full Details
Create a free account to unlock this interview experience report.