Queue Reconstruction by Height

Algorithms
Medium
Stripe
102.9K views

You are asked to reconstruct a queue of people based on their height and the number of people in front of them who are taller or the same height. Use a Greedy approach combined with sorting.

Why Interviewers Ask This

Stripe interviewers ask this problem to evaluate a candidate's ability to optimize sorting strategies and apply greedy logic under constraints. They specifically test whether you can recognize that processing elements in a specific order (tallest first) simplifies the insertion logic, transforming a complex O(N^2) simulation into an efficient solution. This assesses your capacity to reframe algorithmic problems by changing the perspective of data traversal.

How to Answer This Question

1. Analyze the constraints: Understand that 'k' represents people with height >= current person. 2. Sort strategically: Propose sorting primarily by height descending, and secondarily by k ascending. Explain why taller people should be processed first so their relative positions don't shift later insertions. 3. Define the Greedy Choice: Articulate that inserting each person at index 'k' is safe because all previously placed people are taller or equal, satisfying the condition immediately without affecting future shorter people. 4. Implement Simulation: Describe using a dynamic list where you insert the current person directly at index 'k'. 5. Validate Complexity: Conclude by analyzing time complexity as O(N log N) for sorting plus O(N^2) for insertions, noting that while insertion is costly, it is often acceptable for Medium difficulty unless N is massive.

Key Points to Cover

  • Explicitly state the dual-sort criteria: height descending, then k ascending.
  • Demonstrate understanding that inserting at index k works because prior elements are always taller.
  • Acknowledge the trade-off between O(N log N) sorting and O(N^2) insertion steps.
  • Walk through a concrete trace with a small dataset to validate the logic.
  • Explain why this greedy choice prevents backtracking or reordering later.

Sample Answer

To solve Queue Reconstruction by Height efficiently, I would start by clarifying the core constraint: each person needs exactly 'k' people in front who are taller or of equal height. A brute-force approach checking every pair would be too slow, so I'd propose a greedy strategy. First, I would sort the input array. The key insight is to process people from tallest to shortest. If two people have the same height, we sort them by their 'k' value in ascending order. By placing taller people first, we ensure that when we place a shorter person later, the existing people in the queue will never violate their height requirement. For example, given [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]], after sorting by height descending and k ascending, we get [[7,0], [7,1], [6,1], [5,0], [5,2], [4,4]]. We then iterate through this sorted list. For [7,0], we insert at index 0. For [7,1], we insert at index 1. When we reach [6,1], we insert at index 1; the person at index 0 (height 7) counts towards the 'k', and the new person shifts everyone else down. Since all previously inserted people are taller, the 'k' count remains valid. Finally, we return the reconstructed list. This approach leverages Stripe's focus on clean, logical algorithms that balance readability with performance, ensuring we handle edge cases like duplicate heights naturally.

Common Mistakes to Avoid

  • Sorting only by height without considering the secondary sort on k, which breaks the logic for equal heights.
  • Attempting to simulate the queue by scanning forward for every person instead of inserting directly at index k.
  • Failing to explain why processing tallest-to-shortest is necessary for the greedy property to hold.
  • Ignoring the time complexity implications of list insertions in languages with non-constant time shifting.

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 57 Stripe questions