Data Structures & AlgorithmsHard

In the unsorted array 100 4 200 1 3 2 4 101 102 103 3, what is the length of the longest run of CONSECUTIVE integers (values like 5, 6, 7 in any order of positions, duplicates counted once)?

  1. A. 9
  2. B. 4
  3. C. 11
  4. D. 5

Correct answer: B. 4

Put all values in a hash set, then start a run only at a value whose predecessor is absent; each run is walked once, so the whole check is linear. Counting distinct values or array length gives totals, not runs.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic