Data Structures & AlgorithmsHardFlipkart/Uber OA · placement
Product of all elements except self, using prefix and suffix products (no division). What is printed?
Product of all elements except self, using prefix and suffix products (no division). What is printed?
a = [1, 1, 0, 2, -1] n = len(a); out = [1] * n p = 1 for i in range(n): out[i] = p; p *= a[i] s = 1 for i in range(n - 1, -1, -1): out[i] *= s; s *= a[i] print(out)
- A. [0, −2, 0, 0, 0]
- B. [0, 0, 0, 0, 0]
- C. [0, 0, −2, 0, 0]
- D. [0, 0, 0, −2, 0]
Correct answer: C. [0, 0, −2, 0, 0]
out[i] must take the product BEFORE multiplying in a[i]; a zero in the array makes every other position 0 except the zero's own. Output: [0, 0, −2, 0, 0].
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
- Every row and every column of this 4 × 4 matrix is sorted ascending (rows separated by ';'): 9 11 16 36; 14 19 37 42; 29 40 45 50; 41 46…
- Every row and every column of this 4 × 4 matrix is sorted ascending (rows separated by ';'): 3 8 19 33; 17 22 35 42; 24 36 49 52; 38 50 53…
- Sort [−1, 3, 2, 2, 6, 1, −2] and, for each i, move two pointers to find the triplet sum closest to 5. What is that closest sum?
- What is the maximum product of a non-empty contiguous subarray of [3, 4, 3, 3, −2, −2, −1, 3]? (Track both the maximum and the minimum…
- Bubble sort (ascending; each pass compares adjacent pairs left to right and swaps if out of order) is applied to [3, 37, 10, 52, 30, 23…
- Records are sorted with a composite key. What is printed? python recs = [('anu', 80), ('zoya', 70), ('dev', 70), ('ravi', 70), ('meera'…