Data Structures & AlgorithmsModerateProduct Company SDE · array OA

Problem: What is the smallest positive integer missing from [3, 4, -1, 1]? Run the code.

Problem: What is the smallest positive integer missing from [3, 4, -1, 1]? Run the code.
a = [3, 4, -1, 1]
m = 1
while m in a:
    m += 1
print(m)
What is printed?
  1. A. 5
  2. B. 1
  3. C. 2
  4. D. 3

Correct answer: C. 2

1 is present, so the first missing positive is 2. The linear-time version places values in their index slots instead of searching.

More Coding Interview Problems questions

Exams that ask this topic