Find the Kth Unused Positive ID in a Sorted Allocation List

Last updated: November 29, 2026

Quick Overview

Allocated IDs are given as a strictly increasing array of positive integers. Return the k-th smallest positive integer that has not been allocated, in better than linear time.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


Allocated IDs are given as a strictly increasing array of positive integers. Return the k-th smallest positive integer that has not been allocated, in better than linear time.

Meta screens like binary search problems where the predicate is not the value itself. Here the interviewer checks that you see `a[i] - (i + 1)` counts missing IDs before index i, and that you can binary search on that monotonic quantity without off-by-one errors.

What the Interviewer Expects
  • Derives that a[i] - i - 1 missing values precede index i
  • Notes that this count is non-decreasing, enabling binary search
  • Finds the first index where the missing count reaches k
  • Computes the answer as lo + k with a short proof
  • Starts from the O(n) scan and improves to O(log n)
Key Topics to Cover
Binary search
Arrays
Counting
Monotonic predicate
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • IDs can be reclaimed and reallocated; how do you support updates?
  • Return the k-th missing ID in a range [lo, hi] instead.
  • The array is too large for memory and stored in fixed-size blocks on disk.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Array is strictly increasing with positive integers, possibly empty. k >= 1. The answer may exceed the last element. Target O(log n)....

High-Level Design

Before index i, the values 1..a[i] contain i allocated ones (a[0]..a[i-1]) plus a[i] itself, so missing(i) = a[i] - (i + 1) IDs are free below a[i]. This is non-decreasing in i. Binary search for `l...


Submit Your Answer
Markdown supported