Data Structures & AlgorithmsHardGATE CS · TCS NQT
Which technique gives O(1) time per query for 'sum of a[l..r]' on a static array after O(n) preprocessing?
- A. Prefix sums
- B. Segment tree with lazy propagation
- C. Sorting plus binary search
- D. Hash map of every subarray sum
Correct answer: A. Prefix sums
Prefix sums answer each range sum as prefix[r] - prefix[l-1] in constant time. A segment tree answers in O(log n), which is slower than necessary for a static array.
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
- Given an array that may contain negative numbers, which method counts the subarrays whose sum equals exactly k in O(n) expected time?
- Which structure returns the maximum of every window of size k in a sliding pass in O(n) total time?
- To test whether string s2 is a rotation of string s1 in linear time, which approach is standard?
- For each element of an array of distinct integers, the task is to count how many earlier elements are smaller. Which structure achieves…
- What is the length of the longest common subsequence of the three strings "ABCBDAB", "BDCABA" and "BCADB"?
- How many non-empty subsequences of "abcbab" (chosen by index, so the same letters at different positions count as different subsequences)…