Question 71
HardWhat is the time complexity of finding the smallest subarray with sum at least S when all numbers are positive?
- A.
O(n log n)
- B.
O(S)
- C.
O(n²)
- D.
O(n)
Sign up free to see the answer and a step-by-step explanation.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the time complexity of finding the smallest subarray with sum at least S when all numbers are positive?
O(n log n)
O(S)
O(n²)
O(n)
Sign up free to see the answer and a step-by-step explanation.
Why can't the variable-size sliding window solve 'smallest subarray with sum ≥ S' when negative numbers are allowed?
It still works
Adding an element may decrease the sum, so shrinking/growing decisions are no longer monotonic
Negative numbers can't be stored
The array must be sorted
Sign up free to see the answer and a step-by-step explanation.
How do two pointers check whether a linked list has a cycle?
Reverse the list
A slow pointer moves 1 step and a fast pointer 2; they meet if there is a cycle
Both move 1 step
Count nodes
Sign up free to see the answer and a step-by-step explanation.
How do you find the k closest elements to x in a sorted array in O(log n + k)?
Scan the whole array
Binary search for x's position, then expand two pointers outward
Use a max-heap of size k over all elements
Sort by distance
Sign up free to see the answer and a step-by-step explanation.
Merging intervals after sorting by start uses which idea?
A single pointer to the last merged interval, extended while the next start ≤ its end
Two heaps
Binary search
Backtracking
Sign up free to see the answer and a step-by-step explanation.
What does the 'Dutch national flag' partition use?
One pointer
A hash map
Three pointers: low, mid and high
A stack
Sign up free to see the answer and a step-by-step explanation.
How can you check whether a string is a palindrome after removing at most one character?
Two pointers from both ends; at the first mismatch, try skipping either character and check the rest
Reverse it
Use a stack only
Check every possible removal
Sign up free to see the answer and a step-by-step explanation.
Which technique finds the longest subarray with sum k when all elements are non-negative?
Sorting
A sliding window
A max-heap
Binary search tree
Sign up free to see the answer and a step-by-step explanation.
What is the 'meet in the middle' technique for subset-sum?
Check every subset
Greedy picking
Use a hash of each element
Split the set in two, list all subset sums of each half, sort one and match with two pointers or binary search
Sign up free to see the answer and a step-by-step explanation.
Squares of a sorted array (which may contain negatives) can be produced in sorted order in O(n) by:
Comparing absolute values at both ends and filling the result from the back
Binary search
Squaring and sorting
A heap
Sign up free to see the answer and a step-by-step explanation.