Kth Missing Positive Number

EasyBinary Searchbinary search

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given arr, a strictly increasing array of positive integers, and an integer k. Consider the positive integers 1, 2, 3, ... that do not appear in arr. Return the k-th of them (1-indexed). The answer may be larger than every element of arr. A linear scan is fine; try for O(log n) using how many numbers are missing before each index.

Examples

Input: arr = [2,3,4,7,11], k = 5
Output: 9
The missing positives are 1, 5, 6, 8, 9, 10, 12, ... The 5th is 9.
Input: arr = [1,2,3,4], k = 2
Output: 6
Nothing is missing inside the array, so the missing numbers start at 5; the 2nd is 6.

Constraints

  • 1 <= arr.length <= 1000
  • 1 <= arr[i] <= 1000
  • 1 <= k <= 1000
  • `arr[i] < arr[j]` for all `i < j`.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More binary search problems