Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft
An array of 8 zeros (indices 0–7) receives range updates with a difference array (diff[l] += v, diff[r + 1] −= v, then prefix-sum): add 5 to [2..6]; add 1 to [2..7]; add 3 to [2..3]; add 6 to [1..3]. What is the final array?
- A. [0, 6, 15, 6, 6, 6, 1, 0]
- B. [0, 6, 15, 15, 6, 6, 6, 1]
- C. [1, 6, 15, 15, 6, 6, 6, 1]
- D. [0, 1, 6, 6, 6, 6, 15, 15]
Correct answer: B. [0, 6, 15, 15, 6, 6, 6, 1]
Mark +v at l and −v at r + 1 (not at r, which would drop the last index), then take the running sum: [0, 6, 15, 15, 6, 6, 6, 1].
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 bring all the 1s of [1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0] together into…
- Reverse the order of words, collapsing multiple spaces. What is printed? python s = " new the is oil " w = s.split() print("|" + "…
- You must answer many queries 'sum of A[l..r]' while also updating single elements, on an array of n elements. Which choice gives O(log n)…
- An array of 8 integers holds values 1..7 with exactly one value appearing twice: [6, 5, 7, 4, 3, 2, 1, 1]. Treating i → a[i] as a linked…
- Overlapping versus non-overlapping substring counts. What is printed? python s, p = "aabaabbbabab", "bab" over = sum(1 for i in…
- Rabin–Karp with base 10 and a deliberately small modulus counts true matches and spurious hits (hash equal, text different). What is…