Queue Reconstruction by Height
Problem Statement
People stand in a line, but the list you are given is shuffled. Each person is written as [h, k]:
his the person's height.kis how many people in front of them are at least as tall as them.
Rebuild the line from front to back and return it.
people: a list of [h, k] pairs, in random order.
The rebuilt line as a list of [h, k] pairs, from the front of the line to the back.
The person [4,4] has four people at least as tall in front: [5,0], [7,0], [5,2] and [6,1]. The person [7,1] has one person at least as tall in front: [7,0].
The person [1,4] has four people in front: [4,0], [5,0], [2,2] and [3,2], and all are at least 1 tall.
- • 1 ≤ people.length ≤ 2000
- • 0 ≤ h ≤ 10^6
- • 0 ≤ k < people.length
- • The list always comes from a real line, so a valid answer exists.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Try every arrangement of the people; keep the first one where every k is right
Sort by height (tallest first, then k ascending); insert each person at index k
There are N! orders of the people. For each order, check every position: count how many people in front are at least as tall, and compare that with k. Return the first order that passes every check.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.