Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft
What is the length of the shortest contiguous subarray of [1, 3, 1, 8, 7, 3, 4, 7, 1, 2] whose sum is at least 20? (All values are positive, so a shrinking sliding window works.)
- A. 10
- B. 4
- C. 3
- D. 5
Correct answer: B. 4
Expand right until the sum reaches 20, then shrink from the left while it stays ≥ 20, recording the length each time. The minimum length is 4.
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
- Count zero-sum contiguous subarrays with a prefix-sum frequency map. What is printed? python from collections import defaultdict a = [0…
- Rotate a square matrix 90° clockwise in place by transposing and then reversing each row. What is printed? python m = [[1, 2, 3], [4, 5…
- For the binary array [0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1], replace 0 by −1 and use prefix sums with a first-occurrence map. What is the…
- Which application does NOT naturally use a stack?
- Two numbers are stored as linked lists with digits in REVERSE order (units first). What does this addition print? python def add(a, b): d…
- How many contiguous subarrays of [5, 4, 4, 3, 2, 5, 2, 2] have a sum between 7 and 11 inclusive? (All values positive: count atMost(11) −…