Question 31
MediumWhat is the output of this code?
a, target = [2, 3, 1, , , ],
l = s =
best = ()
r, x (a):
s += x
s >= target:
best = (best, r - l + )
s -= a[l]
l +=
(best)
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 output of this code?
a, target = [2, 3, 1, , , ],
l = s =
best = ()
r, x (a):
s += x
s >= target:
best = (best, r - l + )
s -= a[l]
l +=
(best)
1
2
3
4
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [0, 1, 0, 3, 12]
k = 0
for x in a:
if x != 0:
a[k] = x
k += 1
for i in range(k, len(a)):
a[i] = 0
print(a)
[1, 12, 3, 0, 0]
[0, 0, 1, 3, 12]
Sign up free to see the answer and a step-by-step explanation.
Why does the two-pointer pair-sum method need the array to be sorted?
Sorting removes duplicates
Moving a pointer must predictably increase or decrease the sum
It doesn't need sorting
Unsorted arrays can't be indexed
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [1, 2, 3, 4, 5]
i, j = 0, len(a) - 1
while i < j:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1
print(a)
[1, 2, 3, 4, 5]
[1, 4, 3, 2, 5]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [1, 2, 4, 7, 11, 15]
t = 15
i, j = 0, len(a) - 1
while i < j:
s = a[i] + a[j]
if s == t:
break
if s < t:
i += 1
else:
j -= 1
print(a[i], a[j])
4 11
2 15
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [1, 1, 2, 2, 2, 3, 4, 4]
k = 1
for i in range(1, len(a)):
if a[i] != a[k - 1]:
a[k] = a[i]
k += 1
print(k, a[:k])
8 [1, 1, 2, 2, 2, 3, 4, 4]
4 [1, 2, 3, 4]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
h = [1, 8, 6, 2, 5, 4, 8, 3, 7]
i, j, best = 0, len(h) - 1, 0
while i < j:
best = max(best, min(h[i], h[j]) * (j - i))
if h[i] < h[j]:
i += 1
else:
j -= 1
print(best)
56
64
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [2, 3, 1, 2, 4, 3]
target = 7
left, s, best = 0, 0, len(a) + 1
for right, x in enumerate(a):
s += x
while s >= target:
best = min(best, right - left + 1)
s -= a[left]
left += 1
print(best)
2
3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [-4, -1, 0, 3, 10]
r = sorted(x * x for x in a)
i, j, out = 0, len(a) - 1, []
while i <= j:
if abs(a[i]) > abs(a[j]):
out.append(a[i] ** 2); i += 1
else:
out.append(a[j] ** 2); j -= 1
print(out[::-1] == r, out[-1])
True 16
True 0
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of the 3-sum problem (find triplets summing to 0) using sorting plus two pointers?
O(n³)
O(n)
O(n²)
O(n log n)
Sign up free to see the answer and a step-by-step explanation.
[1, 3, 12, 0, 0]
[12, 3, 1, 0, 0]
[5, 2, 3, 4, 1]
[5, 4, 3, 2, 1]
1 15
7 11
3 [1, 2, 3]
4 [1, 2, 2, 3]
49
40
1
4
True 100
False 0