Question 51
MediumCounting pairs with difference exactly k in a sorted array of distinct numbers: what is the time with two pointers?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
Counting pairs with difference exactly k in a sorted array of distinct numbers: what is the time with two pointers?
O(n²)
O(n log n) after sorting only
O(n)
O(k)
Sign up free to see the answer and a step-by-step explanation.
In the 'minimum window substring' problem, when does the left pointer move?
Only at the end
After every step
When the window already contains all required characters, to try to make it shorter
Never
Sign up free to see the answer and a step-by-step explanation.
Which property must the array have for the 'two-sum with two pointers' approach?
It must have no negatives
It must be distinct
It must be sorted
It must have even length
Sign up free to see the answer and a step-by-step explanation.
How many passes does the 'move zeros to the end' two-pointer method make?
One pass
log n passes
Two passes always
n passes
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = sorted([-1, 0, 1, 2, -1, -4])
res = []
for i in range(len(a) - 2):
if i and a[i] == a[i - 1]:
continue
l, r = i + 1, len(a) - 1
while l < r:
s = a[i] + a[l] + a[r]
if s < 0:
l += 1
elif s > 0:
r -= 1
else:
res.append([a[i], a[l], a[r]])
while l < r and a[l] == a[l + 1]:
l += 1
l += 1
r -= 1
print(res)
[[-1, 0, 1]]
[[-1, -1, 2]]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
h = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
l, r = 0, len(h) - 1
lmax = rmax = water = 0
while l < r:
if h[l] < h[r]:
lmax = max(lmax, h[l]); water += lmax - h[l]; l += 1
else:
rmax = max(rmax, h[r]); water += rmax - h[r]; r -= 1
print(water)
6
4
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
from collections import Counter
s, t = "ADOBECODEBANC", "ABC"
need, missing = Counter(t), len(t)
l = 0
best = (0, float("inf"))
for r, ch in enumerate(s):
if need[ch] > 0:
missing -= 1
need[ch] -= 1
while missing == 0:
if r - l < best[1] - best[0]:
best = (l, r)
need[s[l]] += 1
if need[s[l]] > 0:
missing += 1
l += 1
print(s[best[0]:best[1] + 1])
ADOBEC
CODEBA
Sign up free to see the answer and a step-by-step explanation.
For which problem is a sliding window NOT directly applicable?
Maximum sum of any k consecutive elements
Longest substring without repeating characters
Maximum subarray sum when the array has negative numbers and the window size is not fixed
Smallest subarray with sum ≥ S for positive numbers
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
from collections import deque
a, k = [1, 3, -1, -3, 5, 3, 6, 7], 3
dq, out = deque(), []
for i, x in enumerate(a):
while dq and a[dq[-1]] <= x:
dq.pop()
dq.append(i)
if dq[0] <= i - k:
dq.popleft()
if i >= k - 1:
out.append(a[dq[0]])
print(out)
[7, 6, 5, 5, 3, 3]
[3, 5, 5, 6, 7, 7]
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of the sliding-window maximum algorithm that uses a deque?
O(n log k)
O(n × k)
O(k)
O(n)
Sign up free to see the answer and a step-by-step explanation.
[[-1, -1, 2], [-1, 0, 1]]
[[0, 0, 0]]
7
5
BANC
BANCE
[1, 3, 3, 5, 6, 7]
[3, 3, 5, 5, 6, 7]