Data Structures & AlgorithmsHardProduct company OA · Infosys pattern

Using a sliding window, what is the length of the longest contiguous subarray of [1, 2, 1, 3, 4, 3, 5, 3, 3] that contains at most 2 distinct values?

  1. A. 3
  2. B. 5
  3. C. 6
  4. D. 4

Correct answer: D. 4

Candidates: [1, 2, 1] (3), [3, 4, 3] (3), [5, 3, 3] (3) and [3, 5, 3, 3] at indices 5–8 (4). Any longer window picks up a third value (e.g. 4 before 3, 5, 3, 3). '5' would need [4, 3, 5, 3, 3], which has three distinct values.

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

Exams that ask this topic