MEDIUMasked at 1 company

Queue Reconstruction by Height

A medium-tier problem at 74% community acceptance, tagged with Array, Binary Indexed Tree, Segment Tree. Reported in interviews at PhonePe and 0 others.

Founder's read

Queue Reconstruction by Height is a medium-difficulty array problem with a 74% acceptance rate, but that number hides a brutal truth: most candidates who pass it got lucky with brute force, not because they understood the greedy trick. PhonePe has asked this one. The problem gives you a list of people defined by height and the count of taller people in front of them. You need to reconstruct the actual queue order. The naive approach crumbles on anything over a few hundred entries. The real solution is counterintuitive enough that if you blank on it during your live OA, StealthCoder runs invisibly and surfaces a working solution in seconds.

Companies asking
1
Difficulty
MEDIUM
Acceptance
74%

Companies that ask "Queue Reconstruction by Height"

If this hits your live OA

Queue Reconstruction by Height is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround.

Get StealthCoder
What this means

The trick is to sort by height ascending, but process people with the same height by their queue position descending. Then build the result by inserting each person at their specified index. This works because when you insert a shorter person, they don't affect the relative ordering of taller people you've already placed. Most candidates either try to simulate the queue directly (O(n^2) per insertion, slow) or get stuck trying to figure out which person to place next. The greedy insight is that height order matters more than queue position, and processing from shortest to tallest guarantees correctness. You can implement this with a simple list and insertion, or optimize with a Binary Indexed Tree or Segment Tree if the constraints get tight. The array and sorting topics are your foundation; the tree structures are optimization paths if the naive insert-and-shift times out.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Queue Reconstruction by Height recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Queue Reconstruction by Height interview FAQ

Is this problem actually medium, or harder?+

The 74% acceptance rate suggests many people solve it, but that's misleading. Brute force passes weak test cases. The real medium difficulty is seeing why sorting by height and inserting by index actually works. Most competitive programmers struggle with the greedy proof, not the code.

What's the most common wrong approach?+

Trying to simulate the queue directly: place people one by one and adjust indices as you go. This is O(n^2) and fragile. The correct greedy insight is to sort first, then insert, because a shorter person inserted at index k doesn't change the queue positions of taller people already placed.

Do I need Binary Indexed Tree or Segment Tree for this?+

Not always. If you use a naive list and insert, Python or Java handles it fine for n up to a few thousand. The tree structures are optimizations to speed up finding the k-th empty slot in O(log n) instead of O(n). Only necessary if constraints are very large or time limits are tight.

How does sorting order actually affect correctness?+

Sorting by height ascending means when you process a person, all taller people are already in the queue. Their queue positions won't change when you insert a shorter person. For people with the same height, you sort by queue position descending to avoid shifting issues during insertion.

Is PhonePe still asking this, and how should I prepare?+

PhonePe has reported this problem. The 74% pass rate and medium difficulty mean it's in their active rotation. Master the greedy sort and insert logic. If you hit it live and freeze, know that StealthCoder solves it invisibly so you can move on without derailing your entire assessment.

Want the actual problem statement? View "Queue Reconstruction by Height" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.