Question 61
HardWhat is the output of this code?
a = [3, 4, -1, 1]
n = (a)
i (n):
<= a[i] <= n a[a[i] - ] != a[i]:
j = a[i] -
a[i], a[j] = a[j], a[i]
((i + i (n) a[i] != i + ))
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 = [3, 4, -1, 1]
n = (a)
i (n):
<= a[i] <= n a[a[i] - ] != a[i]:
j = a[i] -
a[i], a[j] = a[j], a[i]
((i + i (n) a[i] != i + ))
2
1
3
5
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [1, 3, 5, 2, 4, 6]
res = []
best = float("-inf")
for x in a:
best = max(best, x)
res.append(best)
print(res)
[6, 6, 6, 6, 6, 6]
[1, 3, 5, 2, 4, 6]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [2, 3, -2, 4, -1]
mx = mn = best = a[0]
for x in a[1:]:
if x < 0:
mx, mn = mn, mx
mx = max(x, mx * x)
mn = min(x, mn * x)
best = max(best, mx)
print(best)
4
6
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, 6, 7]
k = 3
def rev(l, r):
while l < r:
a[l], a[r] = a[r], a[l]
l += 1; r -= 1
n = len(a)
rev(0, n - 1); rev(0, k - 1); rev(k, n - 1)
print(a)
[5, 6, 7, 1, 2, 3, 4]
[3, 2, 1, 7, 6, 5, 4]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
out = []
t, b, l, r = 0, 2, 0, 2
while t <= b and l <= r:
out += m[t][l:r + 1]; t += 1
out += [m[i][r] for i in range(t, b + 1)]; r -= 1
if t <= b:
out += m[b][l:r + 1][::-1]; b -= 1
if l <= r:
out += [m[i][l] for i in range(b, t - 1, -1)]; l += 1
print(out)
[1, 2, 3, 6, 9, 7, 8, 4, 5]
[1, 4, 7, 8, 9, 6, 3, 2, 5]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [7, 1, 5, 3, 6, 4]
lo, best = a[0], 0
for x in a:
lo = min(lo, x)
best = max(best, x - lo)
print(best)
6
5
Sign up free to see the answer and a step-by-step explanation.
Which algorithm finds the k most frequent elements of an array in O(n) expected time?
Binary search
Sort the array
Use nested loops
Count frequencies with a hash map, then bucket the elements by frequency
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of finding the median of an unsorted array with the median-of-medians algorithm?
O(n) worst case
O(n log n)
O(n²)
O(log n)
Sign up free to see the answer and a step-by-step explanation.
An array of n integers contains every number from 1 to n + 1 except one. Which O(1)-space method finds the missing one without overflow risk?
Sort the array
Sum with 32-bit integers
Use a hash set
XOR all array elements and all numbers 1 to n + 1
Sign up free to see the answer and a step-by-step explanation.
What does Kadane's algorithm return for an array where every element is negative (in the version that tracks max(x, cur + x))?
0
The sum of all elements
The smallest element
The largest (least negative) element
Sign up free to see the answer and a step-by-step explanation.
[1, 3, 5, 5, 6, 6]
[1, 3, 5, 5, 5, 6]
24
48
[7, 6, 5, 4, 3, 2, 1]
[4, 5, 6, 7, 1, 2, 3]
[1, 2, 3, 6, 9, 8, 7, 4, 5]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
4
7