Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
Question 1 of 60 · Iterative and recursive binary search · Easy
What is the time complexity of binary search on a sorted array of elements?
Not quite. Try another option.
Answer:
Binary search halves the search space in each step, giving a logarithmic time complexity of .
Question 2 of 60 · Iterative and recursive binary search · Easy
Binary search requires the input array to be:
Not quite. Try another option.
Answer: Sorted
Binary search relies on comparing the target with the middle element to discard half the array, which only works if the array is sorted.
Question 3 of 60 · Iterative and recursive binary search · Easy
In binary search, how is the middle index typically computed to avoid integer overflow for indices and ?
Not quite. Try another option.
Answer:
Using prevents overflow that could occur from directly adding and when both are large.
Question 4 of 60 · Jump search · Easy
For an array of size , what is the optimal block (jump) size used in jump search?
Not quite. Try another option.
Answer:
Jumping by minimizes the total work, giving jump search its time complexity of .
Question 5 of 60 · Jump search · Easy
Like binary search, jump search requires the array to be:
Not quite. Try another option.
Answer: Sorted
Jump search skips ahead in fixed blocks and relies on ordering to know when the target has been passed, so the array must be sorted.
Question 6 of 60 · Sublist search · Easy
What does sublist search (list matching) determine?
Not quite. Try another option.
Answer: Whether a list is present as a contiguous part of another list
Sublist search checks if all elements of one linked list appear consecutively within another list.
Question 7 of 60 · Find the missing number · Easy
For an array containing distinct numbers from to , which formula gives the missing number using the sum approach?
Not quite. Try another option.
Answer:
The expected sum of to is ; subtracting the actual array sum yields the missing number.
Question 8 of 60 · Find the missing number · Easy
Which bitwise operation is commonly used to find a single missing number without risk of overflow?
Not quite. Try another option.
Answer: XOR
XOR-ing all indices with all elements cancels matching pairs, leaving only the missing number.
Question 9 of 60 · Search an element in a sorted and rotated array · Easy
What is the time complexity of searching an element in a sorted and rotated array using modified binary search?
Not quite. Try another option.
Answer:
A modified binary search still halves the search space each step, achieving even on rotated arrays.
Question 10 of 60 · Search an element in a sorted and rotated array · Easy
In a sorted and rotated array, at each step of modified binary search we first check:
Not quite. Try another option.
Answer: Which half is properly sorted
Identifying the sorted half lets us decide whether the target lies within it, allowing us to discard the other half.
Question 11 of 60 · Recursive function to perform substring search · Easy
What does a substring search function return when the pattern is found in the text?
Not quite. Try another option.
Answer: The starting index of the match
Substring search typically reports the position where the pattern begins within the text.
Question 12 of 60 · Recursive function to perform substring search · Easy
In a naive recursive substring search, what happens at each recursive call?
Not quite. Try another option.
Answer: The function attempts to match the pattern starting at the next position
Each recursive call shifts the starting index and tries to match the pattern against that portion of the text.
Question 13 of 60 · Find the K most frequent words from a string · Easy
Which data structure is most suitable for counting how many times each word appears in a string?
Not quite. Try another option.
Answer: Hash map (dictionary)
A hash map stores each word as a key with its count as the value, allowing efficient frequency counting.
Question 14 of 60 · Find the K most frequent words from a string · Easy
When two words have the same frequency, they are often ordered by:
Not quite. Try another option.
Answer: Lexicographical (alphabetical) order
Ties in frequency are commonly broken by sorting the words alphabetically.
Question 15 of 60 · Find a pair with a given difference · Easy
For finding a pair with a given difference in a sorted array, which technique gives an efficient solution?
Not quite. Try another option.
Answer: Two-pointer approach
Two pointers move through the sorted array adjusting based on the current difference, achieving linear time.
Question 16 of 60 · Find a pair with a given difference · Easy
To find a pair with difference , if we fix an element , which value do we search for?
Not quite. Try another option.
Answer:
A pair has difference when , so we search for .
Question 17 of 60 · Find a peak element · Easy
A peak element in an array is one that is:
Not quite. Try another option.
Answer: Greater than or equal to its neighbors
A peak is an element not smaller than its adjacent elements; boundaries consider only their single neighbor.
Question 18 of 60 · Find a peak element · Easy
What is the time complexity of finding a peak element using binary search?
Not quite. Try another option.
Answer:
By comparing the middle element with its neighbor and moving toward the higher side, a peak is found in time.
Question 19 of 60 · Length of longest subarray having sum in given range l r · Easy
Which technique is commonly combined with prefix sums to efficiently handle subarray sum range problems?
Not quite. Try another option.
Answer: Sliding window / two pointers
Prefix sums together with sliding window or two-pointer techniques let us evaluate subarray sums efficiently.
Question 20 of 60 · Print all subarrays with sum in a given range · Easy
The naive brute-force method to print all subarrays with sum in a range has what time complexity?
Not quite. Try another option.
Answer:
Considering every possible start and end index of a subarray and tracking running sums leads to pairs.
Question 21 of 60 · Iterative and recursive binary search · Medium
In an iterative binary search, the mid index is computed as mid = low + (high - low) / 2 instead of mid = (low + high) / 2. What is the primary reason for this choice?
Not quite. Try another option.
Answer: To avoid integer overflow when low and high are large
Adding low + high can exceed the integer range for large values. Writing low + (high - low) / 2 yields the same midpoint while keeping the intermediate sum within bounds.
Question 22 of 60 · Iterative and recursive binary search · Medium
A recursive binary search is called on a sorted array of elements. In the worst case, how many recursive calls (including the initial call) are made before termination?
Not quite. Try another option.
Answer:
Each call halves the search space, so the depth of recursion is about , giving calls in the worst case.
Question 23 of 60 · Jump search · Medium
For a sorted array of elements, what block (jump) size minimizes the worst-case number of comparisons in jump search?
Not quite. Try another option.
Answer:
With block size , the cost is about , which is minimized when , giving the optimal time.
Question 24 of 60 · Jump search · Medium
Jump search is being applied to an array of 100 sorted elements using the optimal block size. Approximately how many total comparisons does it need in the worst case?
Not quite. Try another option.
Answer: About 20
Optimal block size is . Worst case does about comparisons.
Question 25 of 60 · Sublist search · Medium
Sublist search checks whether a linked list of nodes appears as a contiguous sublist within a larger linked list of nodes. What is its worst-case time complexity using the naive approach?
Not quite. Try another option.
Answer:
For each of the starting positions, up to nodes are compared, giving in the worst case, similar to naive substring matching.
Question 26 of 60 · Find the missing number · Medium
An array contains distinct numbers taken from the range to . Using the sum formula, which expression correctly gives the missing number?
Not quite. Try another option.
Answer:
The sum of all numbers from to is . Subtracting the actual array sum yields the missing value.
Question 27 of 60 · Find the missing number · Medium
Which technique finds the single missing number in a range to without risking overflow from summation and works in time?
Not quite. Try another option.
Answer: XOR all array elements with all numbers from to
XOR-ing every array element with every number in to cancels all matching pairs, leaving the missing number. It avoids overflow and runs in .
Question 28 of 60 · Search an element in a sorted and rotated array · Medium
In a sorted and rotated array with no duplicates, after computing mid, how do you decide which half to search in modified binary search?
Not quite. Try another option.
Answer: Determine which half is sorted, then check if the target lies within that sorted half's range
At least one half is always sorted. Identify it, test if the target falls within its bounds, and recurse into the appropriate half, keeping the search .
Question 29 of 60 · Search an element in a sorted and rotated array · Medium
Consider the rotated array [6, 7, 8, 1, 2, 3, 4, 5]. When searching for 3, the first mid (index 3, value 1) is examined. Which half is identified as sorted?
Not quite. Try another option.
Answer: The right half [1, 2, 3, 4, 5] is sorted
Since arr[mid]=1 \le arr[high]=5, the right half is sorted. The target 3 lies in , so the search continues in the right half.
Question 30 of 60 · Recursive function to perform substring search · Medium
A recursive substring search checks if pattern P occurs in text T starting at each index. What is the base case that returns success?
Not quite. Try another option.
Answer: All characters of P have been matched (pattern index reaches its length)
A full match occurs when the recursion has compared every character of the pattern successfully, i.e., the pattern index equals its length.
Question 31 of 60 · Find the K most frequent words from a string · Medium
To find the most frequent words in a string, a min-heap of size is maintained over word frequencies. What is the overall time complexity if there are distinct words?
Not quite. Try another option.
Answer:
Each of the distinct words is pushed/popped from a heap of size , costing per operation, for a total of .
Question 32 of 60 · Find the K most frequent words from a string · Medium
When two words have the same frequency in the "K most frequent words" problem, the standard tie-breaking rule ranks them by which criterion?
Not quite. Try another option.
Answer: Lexicographical (alphabetical) order
The conventional rule breaks frequency ties by returning words in lexicographically ascending order so the output is deterministic.
Question 33 of 60 · Find a pair with a given difference · Medium
Given a sorted array, the two-pointer method finds a pair with difference . When arr[j] - arr[i] > d, what action is taken?
Not quite. Try another option.
Answer: Increment i to increase the smaller element
If the difference is too large, moving i forward increases arr[i], which shrinks the gap arr[j] - arr[i] toward the target .
Question 34 of 60 · Find a pair with a given difference · Medium
For an unsorted array, which approach finds a pair with a given difference in average time using extra space?
Not quite. Try another option.
Answer: Store elements in a hash set, then for each x check if x + d exists
Insert all values into a hash set; for each element x, checking whether x + d is present takes average time, giving overall .
Question 35 of 60 · Find a peak element · Medium
A peak element is one that is not smaller than its neighbors. Using binary search, if arr[mid] < arr[mid+1], where is a peak guaranteed to exist?
Not quite. Try another option.
Answer: In the right half, indices mid+1 to high
Since the sequence rises toward mid+1, moving right must eventually reach a peak (values can't rise forever within bounds), so search the right half.
Question 36 of 60 · Find a peak element · Medium
What is the time complexity of finding a peak element in an unsorted array using the binary search approach?
Not quite. Try another option.
Answer:
The binary search variant discards half the array each step based on the neighbor comparison, achieving even though the array is unsorted.
Question 37 of 60 · Length of longest subarray having sum in given range l r · Medium
To find the longest subarray whose sum lies in , prefix sums are used. For a fixed right index with prefix sum , the subarray sum condition becomes which inequality on an earlier prefix ?
Not quite. Try another option.
Answer:
The subarray sum is . Requiring rearranges to .
Question 38 of 60 · Print all subarrays with sum in a given range · Medium
For an array of all positive integers, why does the sliding-window technique work to enumerate subarrays with sum in a range?
Not quite. Try another option.
Answer: Because the running sum increases monotonically as the window expands, so the window can be shrunk once the sum exceeds the range in a controlled manner that never requires re-examining discarded left elements
With only positive values, expanding the window always increases the sum and shrinking it always decreases it, giving the monotonicity that sliding-window relies on.
Question 39 of 60 · Minimum time required to produce m items · Medium
Machines produce items at fixed rates. To find the minimum time to make items, binary search is applied on the answer (time ). What is checked for a candidate time ?
Not quite. Try another option.
Answer: Whether the total items produced by all machines in time is at least
Sum over all machines; if it is , time is feasible, and binary search narrows to the minimum such .
Question 40 of 60 · Minimum time required to produce m items · Medium
If the slowest machine takes max_rate minutes per item and there are machines producing items, what is a valid upper bound for the binary search on time?
Not quite. Try another option.
Answer:
Even a single slowest machine could make all items in minutes, so that is a safe upper bound for the search range.
Question 41 of 60 · Iterative and recursive binary search · Hard
In a binary search implementation, using mid = (low + high) / 2 can cause a bug for very large arrays. Why does mid = low + (high - low) / 2 fix this?
Not quite. Try another option.
Answer: It prevents integer overflow when low + high exceeds the maximum integer value
For large low and high, their sum can overflow a fixed-width integer, producing a negative or wrong index. Rewriting as low + (high - low) / 2 keeps intermediate values within range while computing the same midpoint.
Question 42 of 60 · Iterative and recursive binary search · Hard
A recursive binary search is written with base case if (low > high) return -1. If instead a programmer writes if (low >= high) return -1, what is the consequence?
Not quite. Try another option.
Answer: An element at a position where low == high may be missed, causing false negatives
When the search narrows to a single element, low == high. Using >= terminates before checking that element, so a target present only at that index is never found.
Question 43 of 60 · Jump search · Hard
For a sorted array of size , jump search uses a block size of . What block size minimizes the worst-case number of comparisons, and what is that optimal complexity?
Not quite. Try another option.
Answer: , giving
Total cost is roughly (jumps plus the linear scan within a block). Minimizing over gives , yielding .
Question 44 of 60 · Jump search · Hard
Why is jump search generally preferred over binary search on systems where jumping backward is far costlier than jumping forward (e.g., certain tape/streaming media)?
Not quite. Try another option.
Answer: Jump search only steps backward once during the final linear scan, minimizing costly reverse seeks
Jump search moves forward in blocks and only steps back once to linearly scan the identified block. Binary search jumps back and forth repeatedly, which is expensive on media where reverse access is slow.
Question 45 of 60 · Sublist search · Hard
Sublist search checks whether a linked list S appears as a contiguous sublist of list L. What is the worst-case time complexity of the straightforward approach for lengths and ?
Not quite. Try another option.
Answer:
The naive sublist search tries to match S starting from each node of L. In the worst case each of the starting positions requires up to comparisons, giving .
Question 46 of 60 · Sublist search · Hard
During sublist search, a partial match fails after matching nodes of the pattern. In the naive algorithm, from which node of the main list does the next matching attempt begin?
Not quite. Try another option.
Answer: From the node immediately after the original start of the failed attempt
The naive sublist search does not use failure-function backtracking. After a mismatch it resets the pattern pointer and restarts matching from the node right after where the current attempt began.
Question 47 of 60 · Find the missing number · Hard
An array contains distinct numbers from the range with exactly one missing. Using XOR to find the missing number, what is XORed together?
Not quite. Try another option.
Answer: XOR of all array elements XORed with XOR of all integers from to
XOR-ing all elements with all values cancels every present number (since ), leaving only the missing value. This avoids overflow risks of the sum-based method.
Question 48 of 60 · Find the missing number · Hard
For finding one missing number in , the sum formula uses . Compared to the XOR method, what is the main practical drawback of the sum approach?
Not quite. Try another option.
Answer: The sum can overflow for large , while XOR never overflows
Both are time and space, but the sum grows quadratically and may overflow fixed-width integers. XOR operates bitwise and stays bounded, avoiding overflow.
Question 49 of 60 · Search an element in a sorted and rotated array · Hard
In a sorted rotated array with no duplicates, at each step of the modified binary search you compute mid. How do you decide which half is normally sorted?
Not quite. Try another option.
Answer: If arr[low] <= arr[mid], the left half is sorted; otherwise the right half is sorted
Because rotation creates one contiguous sorted region containing low..mid or mid..high. If arr[low] <= arr[mid] the left portion is in order, so you can test whether the target lies within it and discard the other half.
Question 50 of 60 · Search an element in a sorted and rotated array · Hard
When a sorted rotated array contains duplicates, why can the worst-case time complexity of the search degrade to ?
Not quite. Try another option.
Answer: When arr[low] == arr[mid] == arr[high], neither half can be determined as sorted, forcing a linear shrink
With duplicates the equality arr[low] == arr[mid] == arr[high] gives no information about which side is sorted, so the algorithm can only advance low or shrink high by one, degrading to in the worst case.
Question 51 of 60 · Recursive function to perform substring search · Hard
A recursive substring search matches pattern P inside text T. The base cases are: pattern exhausted (return true) and text exhausted with pattern remaining (return false). What recursive relation correctly continues a match?
Not quite. Try another option.
Answer: If P[0] == T[0], recurse on P[1:] and T[1:]; else recurse on the same P with T[1:]
On a character match, advance both pointers to continue the current alignment. On mismatch, the whole pattern must be retried from the next text position, so the pattern resets while the text advances by one.
Question 52 of 60 · Recursive function to perform substring search · Hard
For a recursive naive substring search over text of length and pattern of length , what is the worst-case time complexity, and which inputs trigger it?
Not quite. Try another option.
Answer: , e.g. text "aaaa...a" with pattern "aaa...b"
Repeated near-matches force the pattern to re-match from nearly every text position. With a text of all a's and a pattern of a's ending in a mismatching char, each of positions does comparisons, giving .
Question 53 of 60 · Find the K most frequent words from a string · Hard
To find the most frequent words (ties broken by lexicographic order) from a string with distinct words, using a min-heap of size , what is the time complexity?
Not quite. Try another option.
Answer:
After counting frequencies, each of the distinct words is pushed into a min-heap capped at size ; heap operations cost . This gives , better than fully sorting at when .
Question 54 of 60 · Find the K most frequent words from a string · Hard
When using a min-heap to keep the top- frequent words, the comparator must order words so the least desirable candidate sits at the root for eviction. For equal frequencies, how should the comparator rank words?
Not quite. Try another option.
Answer: Words that are lexicographically larger should be treated as smaller so they are evicted first
In a min-heap keeping the best , the root is the weakest kept candidate. With equal counts we prefer the lexicographically smaller word, so the larger one must compare as 'smaller' to sit at the root and be evicted first.
Question 55 of 60 · Find a pair with a given difference · Hard
Given a sorted array, to find a pair with difference using two pointers i and j, how are the pointers advanced?
Not quite. Try another option.
Answer: If arr[j] - arr[i] < d increment j; if > d increment i; if == d (and i != j) report success
Both pointers start near the front. Increasing j raises the difference; increasing i lowers it. This monotonic control finds a pair in after sorting, versus per binary-search variant.
Question 56 of 60 · Find a pair with a given difference · Hard
For an unsorted array, a hash-set method finds a pair with difference by checking, for each element , whether or was seen. What subtle case must be handled when ?
Not quite. Try another option.
Answer: A number must appear at least twice; a single occurrence must not falsely report a pair
With , , so a single element would match itself and give a false positive. The algorithm must ensure the value genuinely occurs at least twice before reporting a pair.
Question 57 of 60 · Find a peak element · Hard
In an array where arr[i] != arr[i+1] for all i, a peak is an element not smaller than its neighbors. Binary search finds a peak in . Which decision rule is correct at index mid?
Not quite. Try another option.
Answer: If arr[mid] < arr[mid+1], a peak must exist to the right; else search left including mid
If the right neighbor is larger, the ascending slope guarantees a peak somewhere to the right. Otherwise mid is on a non-increasing side toward the left, where a peak is guaranteed, so we search left including mid.
Question 58 of 60 · Find a peak element · Hard
Why does the peak-finding binary search always guarantee a peak exists in the chosen half, even without global sorting?
Not quite. Try another option.
Answer: Moving toward a larger neighbor guarantees a boundary that eventually turns down, forcing a peak in that direction
If you always step toward the larger neighbor, the sequence keeps rising but is bounded, so it must eventually stop rising—that turning point is a peak. This guarantees a peak in the retained half regardless of the rest of the array.
Question 59 of 60 · Length of longest subarray having sum in given range l r · Hard
For an array with negative numbers, why does the simple sliding-window (two-pointer) technique fail to find the longest subarray with sum in range ?
Not quite. Try another option.
Answer: Prefix sums are not monotonic, so expanding or shrinking the window does not move the sum predictably
Sliding window relies on the sum increasing as the window grows. With negatives the prefix sum is non-monotonic, so shrinking the window may increase the sum—breaking the invariant. Prefix sums with ordered structures (e.g., BIT/BST) are used instead.
Question 60 of 60 · Minimum time required to produce m items · Hard
Given machines where machine produces one item every minutes, binary search on time finds the minimum to make items. What monotonic predicate is searched?
Not quite. Try another option.
Answer: , which is false for small and true for large
In time , machine makes items. Total production is monotonic non-decreasing in , so binary search finds the smallest where .
That's unit 6
Answer a few questions and your score shows up here.
Nearly through. Did unit 6 of CSE329 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 6 of CSE329 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill;
the rest comes out of a student's own pocket: the domain, the storage, and keeping
the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.