Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft
In the binary array [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0] you may flip at most 2 zeros to ones. What is the length of the longest run of consecutive ones achievable? (Sliding window with at most 2 zeros inside.)
- A. 10
- B. 9
- C. 12
- D. 8
Correct answer: C. 12
Keep a window with at most 2 zeros; when a third zero enters, move the left edge past the earliest zero. Window [0..11] (eight 1s, the 0 0, then 1 1) has length 12. Adding 2 to the longest run (8 + 2 = 10) ignores that flips join runs; 9 allows only one flip and 8 none.
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
- Evaluate an infix expression of single digits with two stacks (operands and operators, usual precedence, left-associative). What is…
- Swap every two adjacent nodes of a linked list by relinking (not by swapping values). What is printed? python def swappairs(h): d = p =…
- How many pairs (i < j) of [2, 15, 17, 10, 24, 2, 10, 2, 4, 9] have a[i] + a[j] divisible by 4? (Count remainders mod 4 and pair r with 4 −…
- A 2-D array A[0..11][0..29] of 2-byte elements is stored in column-major order starting at address 2000. What is the address of A[9][6]?
- For each day, a monotonic stack finds how many days you must wait for a strictly warmer temperature (0 if never). What is printed? python…
- Longest run of consecutive integers (in any order) using a hash set and starting only from numbers whose predecessor is absent. What is…