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?
- A. 3
- B. 5
- C. 6
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A chained hash table has 5 buckets, h(k) = k mod 5, and every new key is inserted at the HEAD of its chain. Keys 12, 7, 22, 3, 17, 2 are…
- The list 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 is reversed in groups of k = 3 nodes; a final group with fewer than k nodes is left as it is. What…
- A singly linked list holds 10 → 21 → 32 → 43 → 54 → 65. It is rearranged so that all nodes at ODD positions (1st, 3rd, …) come first…
- The juggling algorithm rotates an array of n = 12 elements left by k = 8 positions by following cycles of the form i → i + k (mod n). How…
- Interpolation search (pos = lo + (key − a[lo]) · (hi − lo) / (a[hi] − a[lo]), integer division) looks for 32 in [1, 2, 4, 8, 16, 32, 64…
- What is the sum of min(subarray) over all contiguous subarrays of [2, 5, 1, 4, 3]? (A monotonic stack counts, for each element, how many…