Bird
Raised Fist0
Interview Prepmodified-binary-searcheasyAmazonGoogle

Find Kth Positive Missing Number

Choose your preparation mode4 modes available
▶
Steps
setup

Calculate total missing numbers before last element

Calculate how many positive numbers are missing before the last element of the array by subtracting the array length from the last element's value.

💡 This tells us if the kth missing number lies beyond the array's last element or inside the array.
Line:n = len(arr) total_missing = arr[-1] - n
💡 Total missing numbers before last element is 6, meaning 6 positive numbers are missing up to 11.
📊
Find Kth Positive Missing Number - Watch the Algorithm Execute, Step by Step
Watching each pointer movement and comparison reveals how the algorithm narrows down the search space efficiently, which is hard to grasp by reading code alone.
Step 1/10
·Active fill★Answer cell
Target: 5
lo
2
0
3
1
4
2
7
3
hi
11
4
Next: lo=0 hi=4
Target: 5
lo
2
0
3
1
4
2
7
3
hi
11
4
Next: lo=0 hi=4
Target: 5
lo
2
0
3
1
4
2
7
3
hi
11
4
Next: lo=0 hi=4
Target: 5
lo
2
0
3
1
mid
4
2
7
3
hi
11
4
mid[2]=4 - missing=1 < k=5
Next: lo=3 hi=4
Target: 5
2
0
3
1
mid
4
2
lo
7
3
hi
11
4
mid[2]=4 - missing=1 < k=5
Next: lo=3 hi=4
Target: 5
2
0
3
1
4
2
lo/mid
7
3
hi
11
4
mid[3]=7 - missing=3 < k=5
Next: lo=4 hi=4
Target: 5
2
0
3
1
4
2
mid
7
3
lo/hi
11
4
mid[3]=7 - missing=3 < k=5
Next: lo=4 hi=4
Target: 5
2
0
3
1
4
2
7
3
lo/mid/hi
11
4
mid[4]=11 - missing=6 >= k=5
Next: lo=4 hi=3
Target: 5
2
0
3
1
4
2
hi
7
3
lo/mid
11
4
mid[4]=11 - missing=6 >= k=5
Next: lo=4 hi=3
Target: 5
2
0
3
1
4
2
hi
7
3
lo
11
4
Next: lo=4 hi=3

Key Takeaways

✓ The total missing count before the last element quickly tells if binary search is needed.

This insight is hard to see from code alone because it requires understanding the relationship between array length and values.

✓ Binary search uses the missing count at mid to eliminate half the search space efficiently.

Visualizing pointer movements clarifies how the algorithm narrows down the search space.

✓ The final answer is computed using the right pointer after binary search ends, not directly from mid.

This subtle detail is easy to miss without step-by-step tracing.

Practice

(1/5)
1. You are given a sorted array and need to find how many times a specific target value appears. Which algorithmic approach guarantees the most efficient solution in terms of time complexity?
easy
A. Binary search to find the left and right boundaries of the target
B. Linear scan through the array counting occurrences
C. Dynamic programming to count occurrences using subproblems
D. Greedy approach selecting elements until target count is found

Solution

  1. Step 1: Understand problem constraints

    The array is sorted, so binary search can be used to find boundaries efficiently.
  2. Step 2: Identify optimal approach

    Binary search for left and right boundaries finds the first and last occurrence indices in O(log n) time, enabling count calculation by subtraction.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Binary search halves search space each step [OK]
Hint: Sorted array -> binary search boundaries [OK]
Common Mistakes:
  • Using linear scan despite sorted array
  • Confusing DP with counting occurrences
  • Applying greedy which doesn't guarantee correctness
2. Consider the following Python code implementing the optimal approach to find k closest elements to x in a sorted array. What is the returned output when calling findClosestElements([1, 3, 5], 2, 4)?
easy
A. [1, 3]
B. [3, 4]
C. [1, 5]
D. [3, 5]

Solution

  1. Step 1: Initialize variables

    Array length is 3, k=2, so low=0, high=1.
  2. Step 2: First iteration of binary search

    Calculate mid = (0+1)//2 = 0. Compare x - arr[mid] = 4 - 1 = 3 and arr[mid + k] - x = arr[2] - 4 = 5 - 4 = 1. Since 3 > 1, set low = mid + 1 = 1.
  3. Step 3: Loop ends as low == high == 1

    Return arr[1:3] = [3, 5].
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    Binary search correctly chooses window starting at index 1 [OK]
Hint: Check mid and window edges distances carefully [OK]
Common Mistakes:
  • Off-by-one error in slicing window
  • Incorrect comparison direction in if condition
  • Returning wrong slice boundaries
3. Consider the following Python function to find the minimum in a rotated sorted array without duplicates. What value does the function return when called with nums = [3, 4, 1]?
easy
A. 4
B. 3
C. 1
D. IndexError due to out-of-bound access

Solution

  1. Step 1: First iteration with left=0, right=2

    mid=1, nums[mid]=4, prev=0 (nums[0]=3), next=2 (nums[2]=1). Check if nums[mid] < nums[prev]: 4 < 3? No. Check if nums[mid] > nums[next_]: 4 > 1? Yes -> return nums[next_] = 1.
  2. Step 2: Function returns 1 immediately

    No further iterations occur; minimum found at nums[2].
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Mid element compared correctly with neighbors [OK]
Hint: Check mid against neighbors to find pivot [OK]
Common Mistakes:
  • Off-by-one in prev/next indices
  • Ignoring wrap-around with modulo
  • Assuming mid is always minimum
4. Consider the following code snippet implementing the binary search on value range approach to find the median of two sorted arrays. Given nums1 = [1, 3] and nums2 = [2, 4], what is the final returned median?
easy
A. 2.0
B. 2.5
C. 3.0
D. 2.25

Solution

  1. Step 1: Trace binary search iterations

    Initial left=1, right=4. Mid=2, countLessEqual(2)=3 ≥ 2.5 so right=2. Next mid=1, countLessEqual(1)=1 < 2.5 so left=2.
  2. Step 2: Compute median for even total length

    Left=2, total length=4 (even). Candidates before 2 are nums1[0]=1 and nums2[0]=2, max=2. Median=(2+3)/2=2.5 is correct median of combined sorted array [1,2,3,4]. The code snippet's logic is incomplete but assuming correct logic, median is 2.5.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Median of combined sorted array [1,2,3,4] is 2.5 [OK]
Hint: Median of [1,2,3,4] is average of 2 and 3 [OK]
Common Mistakes:
  • Off-by-one in bisect boundaries
  • Confusing left with median
  • Ignoring even length median averaging
5. The following code attempts to find the insert position of a target in a sorted array using recursive binary search. Identify the line containing the subtle bug that can cause incorrect results or infinite recursion.
medium
A. Line 3: if left > right: return right
B. Line 4: mid = (left + right) // 2
C. Line 7: if nums[mid] == target: return mid
D. Line 9: return binary_search(mid + 1, right)

Solution

  1. Step 1: Analyze base case return value

    When left > right, the correct insertion index is left, not right.
  2. Step 2: Consequence of returning right

    Returning right can cause incorrect insertion index or infinite recursion if right < left.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Correct base case returns left to indicate insertion point [OK]
Hint: Base case must return left, not right [OK]
Common Mistakes:
  • Returning right instead of left at base case
  • Incorrect mid calculation causing infinite loop
  • Misplaced recursive calls
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard