Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
There is a party where n friends numbered from 0 to n - 1 are attending. There is an infinite number of chairs in this party that are numbered from 0 to infinity. When a friend arrives at the party, they sit on the unoccupied chair with the smallest number.
0, 1, and 5 are occupied when a friend comes, they will sit on chair number 2.When a friend leaves the party, their chair becomes unoccupied at the moment they leave. If another friend arrives at that same moment, they can sit in that chair.
You are given a 0-indexed 2D integer array times where times[i] = [arrivali, leavingi], indicating the arrival and leaving times of the ith friend respectively, and an integer targetFriend. All arrival times are distinct.
Return the chair number that the friend numbered targetFriend will sit on.
Example 1:
Input: times = [[1,4],[2,3],[4,6]], targetFriend = 1 Output: 1 Explanation: - Friend 0 arrives at time 1 and sits on chair 0. - Friend 1 arrives at time 2 and sits on chair 1. - Friend 1 leaves at time 3 and chair 1 becomes empty. - Friend 0 leaves at time 4 and chair 0 becomes empty. - Friend 2 arrives at time 4 and sits on chair 0. Since friend 1 sat on chair 1, we return 1.
Example 2:
Input: times = [[3,10],[1,5],[2,6]], targetFriend = 0 Output: 2 Explanation: - Friend 1 arrives at time 1 and sits on chair 0. - Friend 2 arrives at time 2 and sits on chair 1. - Friend 0 arrives at time 3 and sits on chair 2. - Friend 1 leaves at time 5 and chair 0 becomes empty. - Friend 2 leaves at time 6 and chair 1 becomes empty. - Friend 0 leaves at time 10 and chair 2 becomes empty. Since friend 0 sat on chair 2, we return 2.
Constraints:
n == times.length2 <= n <= 104times[i].length == 21 <= arrivali < leavingi <= 1050 <= targetFriend <= n - 1arrivali time is distinct.Problem summary: There is a party where n friends numbered from 0 to n - 1 are attending. There is an infinite number of chairs in this party that are numbered from 0 to infinity. When a friend arrives at the party, they sit on the unoccupied chair with the smallest number. For example, if chairs 0, 1, and 5 are occupied when a friend comes, they will sit on chair number 2. When a friend leaves the party, their chair becomes unoccupied at the moment they leave. If another friend arrives at that same moment, they can sit in that chair. You are given a 0-indexed 2D integer array times where times[i] = [arrivali, leavingi], indicating the arrival and leaving times of the ith friend respectively, and an integer targetFriend. All arrival times are distinct. Return the chair number that the friend numbered targetFriend will sit on.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[[1,4],[2,3],[4,6]] 1
[[3,10],[1,5],[2,6]] 0
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1942: The Number of the Smallest Unoccupied Chair
class Solution {
public int smallestChair(int[][] times, int targetFriend) {
int n = times.length;
PriorityQueue<Integer> idle = new PriorityQueue<>();
PriorityQueue<int[]> busy = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
for (int i = 0; i < n; ++i) {
times[i] = new int[] {times[i][0], times[i][1], i};
idle.offer(i);
}
Arrays.sort(times, Comparator.comparingInt(a -> a[0]));
for (var e : times) {
int arrival = e[0], leaving = e[1], i = e[2];
while (!busy.isEmpty() && busy.peek()[0] <= arrival) {
idle.offer(busy.poll()[1]);
}
int j = idle.poll();
if (i == targetFriend) {
return j;
}
busy.offer(new int[] {leaving, j});
}
return -1;
}
}
// Accepted solution for LeetCode #1942: The Number of the Smallest Unoccupied Chair
func smallestChair(times [][]int, targetFriend int) int {
idle := hp{}
busy := hp2{}
for i := range times {
times[i] = append(times[i], i)
heap.Push(&idle, i)
}
sort.Slice(times, func(i, j int) bool { return times[i][0] < times[j][0] })
for _, e := range times {
arrival, leaving, i := e[0], e[1], e[2]
for len(busy) > 0 && busy[0].t <= arrival {
heap.Push(&idle, heap.Pop(&busy).(pair).i)
}
j := heap.Pop(&idle).(int)
if i == targetFriend {
return j
}
heap.Push(&busy, pair{leaving, j})
}
return -1
}
type hp struct{ sort.IntSlice }
func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() any {
a := h.IntSlice
v := a[len(a)-1]
h.IntSlice = a[:len(a)-1]
return v
}
type pair struct{ t, i int }
type hp2 []pair
func (h hp2) Len() int { return len(h) }
func (h hp2) Less(i, j int) bool { return h[i].t < h[j].t || (h[i].t == h[j].t && h[i].i < h[j].i) }
func (h hp2) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *hp2) Push(v any) { *h = append(*h, v.(pair)) }
func (h *hp2) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
# Accepted solution for LeetCode #1942: The Number of the Smallest Unoccupied Chair
class Solution:
def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
n = len(times)
for i in range(n):
times[i].append(i)
times.sort()
idle = list(range(n))
heapify(idle)
busy = []
for arrival, leaving, i in times:
while busy and busy[0][0] <= arrival:
heappush(idle, heappop(busy)[1])
j = heappop(idle)
if i == targetFriend:
return j
heappush(busy, (leaving, j))
// Accepted solution for LeetCode #1942: The Number of the Smallest Unoccupied Chair
/**
* [1942] The Number of the Smallest Unoccupied Chair
*
* There is a party where n friends numbered from 0 to n - 1 are attending. There is an infinite number of chairs in this party that are numbered from 0 to infinity. When a friend arrives at the party, they sit on the unoccupied chair with the smallest number.
*
* For example, if chairs 0, 1, and 5 are occupied when a friend comes, they will sit on chair number 2.
*
* When a friend leaves the party, their chair becomes unoccupied at the moment they leave. If another friend arrives at that same moment, they can sit in that chair.
* You are given a 0-indexed 2D integer array times where times[i] = [arrivali, leavingi], indicating the arrival and leaving times of the i^th friend respectively, and an integer targetFriend. All arrival times are distinct.
* Return the chair number that the friend numbered targetFriend will sit on.
*
* Example 1:
*
* Input: times = [[1,4],[2,3],[4,6]], targetFriend = 1
* Output: 1
* Explanation:
* - Friend 0 arrives at time 1 and sits on chair 0.
* - Friend 1 arrives at time 2 and sits on chair 1.
* - Friend 1 leaves at time 3 and chair 1 becomes empty.
* - Friend 0 leaves at time 4 and chair 0 becomes empty.
* - Friend 2 arrives at time 4 and sits on chair 0.
* Since friend 1 sat on chair 1, we return 1.
*
* Example 2:
*
* Input: times = [[3,10],[1,5],[2,6]], targetFriend = 0
* Output: 2
* Explanation:
* - Friend 1 arrives at time 1 and sits on chair 0.
* - Friend 2 arrives at time 2 and sits on chair 1.
* - Friend 0 arrives at time 3 and sits on chair 2.
* - Friend 1 leaves at time 5 and chair 0 becomes empty.
* - Friend 2 leaves at time 6 and chair 1 becomes empty.
* - Friend 0 leaves at time 10 and chair 2 becomes empty.
* Since friend 0 sat on chair 2, we return 2.
*
*
* Constraints:
*
* n == times.length
* 2 <= n <= 10^4
* times[i].length == 2
* 1 <= arrivali < leavingi <= 10^5
* 0 <= targetFriend <= n - 1
* Each arrivali time is distinct.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/the-number-of-the-smallest-unoccupied-chair/
// discuss: https://leetcode.com/problems/the-number-of-the-smallest-unoccupied-chair/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn smallest_chair(times: Vec<Vec<i32>>, target_friend: i32) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1942_example_1() {
let times = vec![vec![1, 4], vec![2, 3], vec![4, 6]];
let target_friend = 1;
let result = 1;
assert_eq!(Solution::smallest_chair(times, target_friend), result);
}
#[test]
#[ignore]
fn test_1942_example_2() {
let times = vec![vec![3, 10], vec![1, 5], vec![2, 6]];
let target_friend = 0;
let result = 2;
assert_eq!(Solution::smallest_chair(times, target_friend), result);
}
}
// Accepted solution for LeetCode #1942: The Number of the Smallest Unoccupied Chair
function smallestChair(times: number[][], targetFriend: number): number {
const n = times.length;
const idle = new MinPriorityQueue<number>();
const busy = new PriorityQueue((a, b) => a[0] - b[0]);
for (let i = 0; i < n; ++i) {
times[i].push(i);
idle.enqueue(i);
}
times.sort((a, b) => a[0] - b[0]);
for (const [arrival, leaving, i] of times) {
while (busy.size() > 0 && busy.front()[0] <= arrival) {
idle.enqueue(busy.dequeue()[1]);
}
const j = idle.dequeue();
if (i === targetFriend) {
return j;
}
busy.enqueue([leaving, j]);
}
return -1;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.