Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft
A 2-D prefix-sum table answers rectangle queries in O(1). For the 4 × 4 matrix (rows separated by ';', 0-based) 4 8 2 1; 4 3 1 4; 7 6 2 4; 1 5 6 4, what is the sum of the sub-rectangle from (1, 2) to (2, 2) inclusive?
- A. 37
- B. -9
- C. 1
- D. 3
Correct answer: D. 3
Sum = P[3][3] − P[1][3] − P[3][2] + P[1][2] = 3, where P is 1-based. Forgetting to add back the doubly-subtracted corner gives -9.
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
- Which sorting algorithms are stable in their standard array implementations: insertion sort, selection sort, merge sort, heap sort, quick…
- For each element of [23, 12, 7, 21, 25, 8, 13, 1], find the next greater element to its RIGHT (−1 if none), using a monotonic stack. What…
- Reverse the order of words, collapsing multiple spaces. What is printed? python s = " oil is data new " w = s.split() print("|" + "…
- Merge two sorted linked lists by relinking nodes (ties take the node from the first list). What is printed? python def merge(a, b): d = t…
- ^ is exponentiation (highest precedence, right-associative); and / come next; + and − are lowest; others are left-associative. What is the…
- Keys 93, 31, 142, 74, 55, 139, 99 are inserted in this order into an empty table of size 11 by double hashing: h1(k) = k mod 11, h2(k) = 5…