Reconstruct a queue from heights and counts of people at least as tall in front

Quick Overview

Rebuild a queue from a shuffled list of people, where each person records their height and how many people at least as tall stand in front of them. It tests greedy reasoning, sorting with a tie-break rule, and proving that a step-by-step construction gives the only valid queue.

Reconstruct a queue from heights and counts of people at least as tall in front

Company: ByteDance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an array of people, `people`, which are the attributes of some people in a queue (not necessarily in order). Each `people[i] = [h, k]` represents a person of height `h` with exactly `k` other people in front who have a height greater than or equal to `h`. Reconstruct and return the queue that is represented by the input array `people`. The returned queue should be formatted as an array `queue`, where `queue[j] = [h, k]` is the attributes of the person at position `j` in the queue (`queue[0]` is the person at the front of the queue). ### Function Signature ```python def reconstruct_queue(people: list[list[int]]) -> list[list[int]]: ``` ### Rules - Each `people[i]` is a two-element list `[h, k]`. Return every input pair exactly once, ordered from the front of the queue to the back. - `k` counts the people in front whose height is greater than or equal to `h`, so a person of the same height in front counts. - The input always describes a valid queue, and a valid input has exactly one reconstruction, so the expected output is unique. ### Constraints - `1 <= people.length <= 2000` - `0 <= h <= 10⁶` - `0 <= k < people.length` - It is guaranteed that the queue can be reconstructed. ### Examples ```text Input: people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]] Output: [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] ``` Reading from the front, every count checks out. For example, `[5,2]` has `[5,0]` and `[7,0]` ahead of it, and `[4,4]` has four people ahead of it, all taller. ```text Input: people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]] Output: [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]] ``` `[6,0]` can stand last because no one ahead of it is as tall as 6. `[3,2]` has `[4,0]` and `[5,0]` ahead of it, while the shorter `[2,2]` ahead of it does not count. ```text Input: people = [[5,2],[5,0],[5,1]] Output: [[5,0],[5,1],[5,2]] ``` Equal heights count toward `k`, so people of the same height stand in increasing order of `k`.

Overview: Rebuild a queue from a shuffled list of people, where each person records their height and how many people at least as tall stand in front of them. It tests greedy reasoning, sorting with a tie-break rule, and proving that a step-by-step construction gives the only valid queue.

ByteDance logo
ByteDance
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an array of people, people, which are the attributes of some people in a queue (not necessarily in order). Each people[i] = [h, k] represents a person of height h with exactly k other people in front who have a height greater than or equal to h.

Reconstruct and return the queue that is represented by the input array people. The returned queue should be formatted as an array queue, where queue[j] = [h, k] is the attributes of the person at position j in the queue (queue[0] is the person at the front of the queue).

Function Signature

def reconstruct_queue(people: list[list[int]]) -> list[list[int]]:

Rules

  • Each people[i] is a two-element list [h, k] . Return every input pair exactly once, ordered from the front of the queue to the back.
  • k counts the people in front whose height is greater than or equal to h , so a person of the same height in front counts.
  • The input always describes a valid queue, and a valid input has exactly one reconstruction, so the expected output is unique.

Constraints

  • 1 <= people.length <= 2000
  • 0 <= h <= 10⁶
  • 0 <= k < people.length
  • It is guaranteed that the queue can be reconstructed.

Examples

Input: people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
Output: [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]

Reading from the front, every count checks out. For example, [5,2] has [5,0] and [7,0] ahead of it, and [4,4] has four people ahead of it, all taller.

Input: people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
Output: [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]

[6,0] can stand last because no one ahead of it is as tall as 6. [3,2] has [4,0] and [5,0] ahead of it, while the shorter [2,2] ahead of it does not count.

Input: people = [[5,2],[5,0],[5,1]]
Output: [[5,0],[5,1],[5,2]]

Equal heights count toward k, so people of the same height stand in increasing order of k.