Rebuild a Photo Lineup From Heights and Taller-in-Front Counts

Last updated: November 29, 2026

Quick Overview

People in a team photo lineup are described by pairs (h, k): height h and the number of people in front of them with height at least h. Given the pairs shuffled, reconstruct the lineup.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


People in a team photo lineup are described by pairs (h, k): height h and the number of people in front of them with height at least h. Given the pairs shuffled, reconstruct the lineup.

A Google onsite favorite for testing whether a candidate can find the right processing order before writing code. The interviewer wants the tallest-first insertion idea and an argument for why shorter people inserted later cannot break earlier constraints.

What the Interviewer Expects
  • Sort by height descending, then k ascending
  • Insert each person at index k
  • Explain why later (shorter) insertions do not invalidate taller people's counts
  • State the O(n^2) cost and discuss speeding it up
  • Verify on a small example
Key Topics to Cover
Greedy
Sorting
Insertion order
Fenwick tree
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
  • Bring the insertion step to O(n log n) with a Fenwick or segment tree.
  • Detect and report inputs that are inconsistent.
  • Count must include only strictly taller people. What changes?
  • Reconstruct when k counts people behind instead of in front.
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: list of [h, k] pairs, guaranteed to describe some valid lineup. Output: the lineup as a list of pairs, front first. k counts people in front with height >= h....

High-Level Design

Process people from tallest to shortest. When we place a person, everyone already placed is at least as tall, so their k means exactly "insert at index k" among the current list. A shorter person in...


Submit Your Answer
Markdown supported