A Prefix Sum (also called a cumulative sum) is an array or data structure where each element at index i represents the sum of all elements in the input array from the beginning up to index i.
const A = [1, 2, 3, 4, 5];
const P = new Array(A.length);
P[0] = A[0];
for(let i = 1; i < A.length; i++){
P[i] = P[i - 1] + A[i];
}
// Now P = [1, 3, 6, 10, 15];
To Compute the sum A[1..3] (i.e., 2 + 3 + 4 = 9):
P[3] - P[0] = 10 - 1 = 9
1. Given an array of integers nums of length n. Design an algorithm that answers multiple range sum queries.
Each query provides two integers l and r (0 <= l <= r < n), and the algorithm should return the sum of all elements from index l to r, inclusive.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | nums = [5, 1, 3], queries = [[0, 0]] | nums = [1, 2, 3, ..., 10⁵] queries = 10⁵ queries like [[0, n-1]] | nums = [0] queries = [[0, 0]] or [[0, 0], [0, 0], ...] |
Output | [5] | [5000050000, ..., repeated] | [0] |
Explanation | Single index query is direct | Large array, high-frequency full-range queries | Handles minimal input and multiple trivial queries |
2. Given an array of integers. Design an algorithm that returns a new array where each element at position i is the sum of all elements from the start of the array up to index i, inclusive.
This cumulative sum should represent the progressive total as the array is traversed from left to right.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [1, 2, 3] | [10_000] * 10⁵ | [42] |
Output | [1, 3, 6] | [10_000, 20_000, ..., 1_000_000_000] | [42] |
Explanation | Small array, low values, fast sum | Largest possible array size and value range—performance tested | Single element input tests minimum bound |
3. Given an integer array nums and an integer k. Design an algorithm that will return the total number of continuous subarrays that sum up to k.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | nums = [1, 2, 1, 2, 1], k = 3 | nums = [0] * 10⁴, k = 0 | nums = [3], k = 3 |
Output | 4 | 50005000 (all zero subarrays count toward k = 0) | 1 |
Explanation | Subarrays: [1,2], [2,1], [1,2], [3] | All subarrays of zeros count as valid — triangle sum | Single element matches target exactly |
4. Given an array of integers nums. Design an algorithm whether it is possible to make th array non-decreasing by modifying at most one element.
A non-decreasing array is one in which each element is less than or equal to the next one to the right.
The algorithm should return True if the array can be made non-decreasing with at most one modification, and False otherwise.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [1, 2, 3, 4] | [4, 3, 2, 1] | [3, 4, 2, 5] |
Output | True | False | True |
Explanation | Already non-decreasing | Requires more than one modification | Modify 4 to 2 (or 2 to 4) |
5. Given an array of integers nums, design an algorithm that returns the index of an element such that the sum of the elements to its left is equal to the sum of the elements to its right.
If multiple such indices exist, return the leftmost one. If no such index exists, return -1.
The sums of the left and right parts exclude the value at the index itself.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [1, 2, 3, 3] | [1, 2, 3, 4, 5, 6] | [5] |
Output | 2 | -1 | 0 |
Explanation | 1+2 = 3, and right of 3 is also 3 | No index satisfies the condition | One element → left and right sums are 0 |
1. Given an array of integers nums and an integer k. Design an algorithm that returns the maximum sum of any contiguous subarray of length k.
If the the array contains fewer than k elements, return -1.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | nums = [1, 2, 3, 4, 5], k = 2 | nums = [-5, -3, -1, -2], k = 2 | nums = [1, 2], k = 3 |
Output | 9 | -4 | -1 |
Explanation | Subarray [4, 5] has sum 9 | Subarray [-1, -2] has the highest sum in negatives | Not enough elements to form subarray of size k |
2. Given an array of integers nums and an integer k. Design an algorithm that returns the maximum average value of any contiguous subarray of length exactly k.
Return the result as a floating-point number, with a precision of at least 5 decimal places.
If the array contains fewer than k elements, return -1.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | nums = [1, 2, 3, 4, 5], k = 2 | nums = [-10, -8, -6, -4], k = 2 | nums = [1, 2], k = 3 |
Output | 4.5 | -5.0 | -1 |
Explanation | Subarray [4, 5] has highest avg = 4.5 | Best subarray is [-6, -4] → avg = -5 | Not enough elements to form window |
3. Given two strings s and p. Design an algorithm that return all starting indices in s where the substring is an anagram of p.
The returned list should be in ascending order.
An anagram is a rearrangement of letters. For example, "abc" and "cba" are anagrams.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | s = "cbaebabacd", p = "abc" | s = "aaaaaaaaaa", p = "aa" | s = "abc", p = "abcd" |
Output | [0, 6] | [0, 1, 2, 3, 4, 5, 6, 7, 8] | [] |
Explanation | Anagrams "cba" at 0 and "bac" at 6 | Every substring of length 2 is "aa" (anagram) | No substrings of s match p |
4. Given a string s. Design an algorithm that returns the length of the longest contiguous substring that contains at most two distinct characters.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | "aabbaac" | "abcdef" | "a" |
Output | 5 | 2 | 1 |
Explanation | "aabba" is the longest with only a and b | Every 2-char window breaks with a 3rd unique | Only one character, length is 1 |
5. Given an integer array nums, an integer indexDiff, and an integer valueDiff. Design an algorithm that determine whether there exist two distinct indices i and j such that:
Return true if such a pair existsm otherwise return false.
Note: The condition must satisfy both the index constraint and the value difference constraint.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | nums = [1, 2, 3, 1], indexDiff = 3, valueDiff = 0 | nums = [1, 10, 20, 30], indexDiff = 1, valueDiff = 0 | nums = [1], indexDiff = 1, valueDiff = 0 |
Output | True | False | False |
Explanation | Duplicate value 1 found at distance 3 | No two values close enough within allowed distance | Only one element, so no pair exists |
1. Given an array of integers nums. Design an algorithm that return all possible contiguous subarrays of nums.
Each subarray must consist of elements that are adjacent in the original array, and their relative ordering must be preserved.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [1, 2] | [1, 2, 3, 4, 5] | [7] |
Output | [[1], [1,2], [2]] | [[1],[1,2],...,[1,2,3,4,5],[2],[2,3],...,[5]] (15 items) | [[7]] |
Explanation | All 3 possible subarrays listed | Algorithm prints all 15 contiguous subarrays | Only one subarray is possible |
2. Given an array of integers nums and an integer k. Design an algorithm tha treturn the total number of contiguous subarrays whose sum is equal to k.
A subarray is a non-empty, contiguous portion of the array.
Return the count of all such subarrays.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | nums = [1, 2, 1], k = 3 | nums = [0, 0, 0, 0], k = 0 | nums = [5], k = 10 |
Output | 2 | 10 | 0 |
Explanation | Subarrays [1, 2] and [2, 1] | 10 different subarrays of zeros sum to 0 | No subarray sums to 10 |
3. Given an array of integers nums. Design an algorithms to find the maximum possible sum of any contiguous subarray.
A subarray is a non-empty, contiguous section of the array.
Return the maximum sum among all possible subarrays.
Parameter\ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | [1, 2, 3, -1, 5] | [-5, -4, -2, -1] | [7] |
Output | 10 | -1 | 7 |
Explanation | [1, 2, 3, -1, 5] = sum 10 | All are negative; max is largest (-1) | Only one element → itself is max |