Fewest Total Moves to Seat Every Googler at a Hot Desk

Last updated: November 29, 2026

Quick Overview

Hot desks in an office row are at given positions, and the same number of Googlers stand at given positions along the row. Each move shifts one person by one position; return the minimum total moves to seat everyone at a distinct desk.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Hot desks in an office row are at given positions, and the same number of Googlers stand at given positions along the row. Each move shifts one person by one position; return the minimum total moves to seat everyone at a distinct desk.

A short greedy that Google phone screens use to check you can justify sorted matching on a line. The interviewer usually asks why crossing assignments can be uncrossed.

What the Interviewer Expects
  • Sort desks and people
  • Pair them in sorted order
  • Sum absolute differences
  • Explain why sorted matching minimizes total distance
  • O(n log n) time
Key Topics to Cover
Greedy
Sorting
Matching on a line
Exchange argument
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Two people may share a desk.
  • Minimize the maximum move instead of the total.
  • There are more desks than people.
  • Desks are on a circle.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: lists desks and people of the same length n, integer positions, duplicates allowed. Output: minimum sum of |desk - person| over a one-to-one assignment....

High-Level Design

Sort both lists and match the i-th smallest person with the i-th smallest desk.

Why: if person p1 < p2 is matched to desks d1 > d2 (crossed), swapping to p1 with d2 and p2 with d1 never increases tot...


Submit Your Answer
Markdown supported