Data Structures & AlgorithmsModerate
For every window of exactly 3 consecutive elements of 7 3 9 4 8 2 6, compute (maximum − minimum) inside that window. What is the SMALLEST such value?
- A. 6
- B. 5
- C. 4
- D. -2
Correct answer: B. 5
A deque-based sliding window gives the maximum and minimum of each length-3 window; the answer is the smallest spread among them. Using length-2 windows, the largest spread, or only the outer elements tests different things.
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 sorted linked list holds 1 2 2 3 3 3 4 4 4 4. Delete nodes so that each value keeps AT MOST TWO nodes, preserving order. How many nodes…
- How many different valid sequences of PUSH and POP operations are there for a stack that performs exactly 6 pushes and 6 pops in total…
- How many contiguous subarrays of 3 1 2 4 2 3 1 2 have a sum EXACTLY equal to twice their length?
- What is the maximum sum of a contiguous subarray of 4 2 4 5 6 2 8 that contains NO repeated value?
- How many contiguous subarrays of length at least 2 of 1 1 2 2 2 3 1 1 1 1 consist of ONE repeated value?
- Treat 5 -2 3 4 -6 1 as a CIRCULAR array. What is the largest sum of three consecutive (circular) elements?