Question 21
EasyWhich technique solves 'container with most water' efficiently?
- 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
Which technique solves 'container with most water' efficiently?
Sorting the heights
Recursion over all pairs
A hash set
Two pointers starting at both ends, moving the shorter side inwards
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
s = "racecar"
i, j = 0, len(s) - 1
ok = True
while i < j:
if s[i] != s[j]:
ok = False
i += 1
j -= 1
print(ok)
False
racecar
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [4, 2, 1, 7, 8, 1]
k = 3
best = s = sum(a[:k])
for i in range(k, len(a)):
s += a[i] - a[i - k]
best = max(best, s)
print(best)
23
15
Sign up free to see the answer and a step-by-step explanation.
The Dutch national flag problem sorts an array of 0s, 1s and 2s in one pass. How many pointers does the standard solution use?
Four
Two
One
Three (low, mid and high)
Sign up free to see the answer and a step-by-step explanation.
What is the usual time complexity of the 3-sum problem using sorting plus two pointers?
O(n²)
O(n log n)
O(n³)
O(n)
Sign up free to see the answer and a step-by-step explanation.
How can the squares of a sorted array that contains negative numbers be produced in sorted order in O(n)?
It can't be done in O(n)
Use binary search for each square
Two pointers at both ends, placing the larger square at the back of the result
Square everything and use bubble sort
Sign up free to see the answer and a step-by-step explanation.
Using two pointers on the sorted array [1, 2, 3, 4, 5], how many pairs of different elements add up to 6?
1
4
2
3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a, k = [2, 1, 5, 1, 3, 2], 3
s = sum(a[:k])
best = s
for i in range(k, len(a)):
s += a[i] - a[i - k]
best = max(best, s)
print(best)
7
12
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
k = 1
for i in range(1, len(a)):
if a[i] != a[k - 1]:
a[k] = a[i]
k += 1
print(k)
2
3
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 = 0, len(h) - 1
best = 0
while i < j:
best = max(best, (j - i) * min(h[i], h[j]))
if h[i] < h[j]:
i += 1
else:
j -= 1
print(best)
36
48
Sign up free to see the answer and a step-by-step explanation.
Error
True
7
16
8
9
4
5
49
40