Data Structures & AlgorithmsModerateInfosys SP/DSE · Product Company SDE
Problem: For n = 10⁶ readings and window size k = 10⁵, report every window maximum. Which approach fits the limits best?
- A. Rescan each window – O(nk)
- B. Monotonic deque of indices – O(n) total
- C. Sort each window – O(n k log k)
- D. Prefix maximum array only – O(n)
Correct answer: B. Monotonic deque of indices – O(n) total
Rescanning costs about 10¹¹ operations. A deque that keeps decreasing values pushes and pops each index once: O(n). A plain prefix-maximum array cannot 'forget' elements that leave the window (it works only for windows starting at 0); a block-wise prefix/suffix maximum trick also gives O(n) but the deque is the standard answer.
More Coding Interview Problems questions
- Problem: Count how many substrings of length k in a DNA string contain exactly k distinct letters. python def countdistinct(s, k): freq, c…
- Problem: Find the length of the longest substring that can be turned into a single repeated letter by changing at most k characters…
- Problem: Return the smallest window of s that contains every character of t (with multiplicity). python from collections import Counter…
- Problem: A username is valid if it reads the same forwards and backwards after removing non-alphanumeric characters and ignoring case…
- Problem: Return True if one character can be deleted from a word to make it a palindrome (or it already is one). python def almost(s): def…
- Problem: Compress a string by replacing each run with the letter followed by the run length (only when the run is longer than 1). python…