Data Structures & AlgorithmsVery hardProduct company OA
The stock span of day i is the number of consecutive days ending at day i whose price is ≤ the price on day i. For prices [31, 27, 14, 21, 30, 18, 25, 40], what are the spans?
- A. 1, 1, 1, 2, 3, 1, 2, 8
- B. 1, 1, 1, 2, 4, 1, 2, 3
- C. 1, 2, 3, 4, 5, 6, 7, 8
- D. 1, 1, 1, 2, 4, 1, 2, 8
Correct answer: D. 1, 1, 1, 2, 4, 1, 2, 8
With a stack of indices holding decreasing prices: 21 pops 14 → span 2; 30 pops 21 and 27 → span 4 (days 27, 14, 21, 30); 25 pops 18 → span 2; 40 pops everything → span 8. '3' for day 5 forgets that 14 was already absorbed into 21's span; '3' for the last day stops at 30 instead of continuing past 31.
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
- What is the minimum number of swaps (of any two elements) needed to sort the permutation [4, 3, 1, 2, 6, 5] into ascending order?
- A stack-based decoder expands k[s] into s repeated k times (nesting allowed). What does '2[x3[y]z]q' decode to?
- Adjacent equal pairs are removed repeatedly with a stack until none remain. What is left of 'pqqprrsttsx'?
- An array is split into the MAXIMUM number of contiguous chunks such that sorting each chunk separately and concatenating them gives the…
- An extended Boyer–Moore vote keeps two candidates to find every value occurring MORE than n/3 times, then verifies them with a second…
- Only the head pointer of a singly linked list of n nodes is known (no length is stored). What is the time to reach its middle node?