Reconstruct Queue (Alternative Sort)

Algorithms
Medium
Meta
143.1K 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. Use a greedy approach with a non-standard sort.

Why Interviewers Ask This

Meta asks this problem to evaluate a candidate's ability to handle complex sorting logic and greedy algorithm design. It tests whether you can recognize that standard sorting fails here, requiring a custom comparator strategy. Interviewers specifically look for your capacity to transform a seemingly chaotic reconstruction task into an ordered process by prioritizing height and count constraints efficiently.

How to Answer This Question

1. Clarify the input format and constraints, ensuring you understand that 'k' represents taller people in front. 2. Propose a greedy strategy: sort the array primarily by height in descending order, and secondarily by k in ascending order. This ensures taller people are placed first without disturbing existing positions of other tall individuals. 3. Initialize an empty list or dynamic array to build the reconstructed queue. 4. Iterate through the sorted list; for each person, insert them at index equal to their k value. Since taller people are already placed, inserting a shorter person at index k guarantees exactly k taller people precede them. 5. Analyze time complexity (O(N^2) due to insertion) versus space complexity, discussing optimizations if N is large. 6. Walk through a concrete example like [[7,0],[4,4],[7,1],[5,0]] to validate the logic step-by-step before coding.

Key Points to Cover

  • Recognize that standard sorting is insufficient and requires a custom comparator.
  • Understand that processing taller people first allows simpler insertion logic for shorter people.
  • Correctly implement the secondary sort key (ascending k) for equal heights.
  • Demonstrate understanding of how insertion at index k satisfies the constraint dynamically.
  • Clearly articulate the trade-off between O(N^2) insertion time and code simplicity.

Sample Answer

To solve the Reconstruct Queue problem, I would start by analyzing the constraints. The core challenge is that inserting a person affects the relative order of others. A brute-force approach checking every permutation would be too slow. Instead, I propose a greedy approach based on sorting. First, I will sort the input array such that people with greater heights come first. If two people have the same height, the one with the smaller k value comes first. This specific ordering is crucial because placing taller people first means that when we later insert shorter people, the taller ones already in the list won't change their relative positions. Next, I initialize an empty result list. I iterate through the sorted array. For each person, say [h, k], I insert them directly into the result list at index k. Because all currently in the list are taller than or equal to the current person, inserting at index k ensures exactly k taller people are ahead of them. For example, given [[7,0],[4,4],[7,1],[5,0]], after sorting we get [[7,0],[7,1],[5,0],[4,4]]. Inserting [7,0] gives [[7,0]]. Inserting [7,1] at index 1 gives [[7,0],[7,1]]. Inserting [5,0] at index 0 shifts everyone, resulting in [[5,0],[7,0],[7,1]]. Finally, inserting [4,4] at index 4 yields the final correct queue. This approach runs in O(N^2) time due to list insertions, which is acceptable for typical interview constraints, and demonstrates strong logical structuring suitable for Meta's engineering standards.

Common Mistakes to Avoid

  • Sorting only by height in ascending order, which makes it impossible to determine correct positions for shorter people later.
  • Ignoring the secondary sort condition for equal heights, leading to incorrect placement when multiple people share the same height.
  • Attempting to use a static array without resizing, failing to account for shifting elements during insertion.
  • Overcomplicating the solution with data structures like Segment Trees when a simple greedy insertion suffices.

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 71 Meta questions