Question 51
MediumWhat is the minimum number of comparisons needed to find both the maximum and minimum of n elements, using pairs?
- 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
What is the minimum number of comparisons needed to find both the maximum and minimum of n elements, using pairs?
About 3n/2
n log n
2n − 2
n − 1
Sign up free to see the answer and a step-by-step explanation.
What is a sparse matrix usually stored as to save space?
A full 2D array
A string
A linked list of all entries
Only its non-zero entries, with their row and column (e.g. triplets or CSR)
Sign up free to see the answer and a step-by-step explanation.
Which approach finds the 'next greater element' for every element of an array in O(n)?
A min-heap
Binary search
A monotonic stack
Nested loops
Sign up free to see the answer and a step-by-step explanation.
Trapping rain water: heights [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]. How many units of water are trapped?
6
5
8
7
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]
n = len(a)
res = [1] * n
p = 1
for i in range(n):
res[i] = p
p *= a[i]
p = 1
for i in range(n - 1, -1, -1):
res[i] *= p
p *= a[i]
print(res)
[24, 12, 8, 6]
[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 = [2, 0, 2, 1, 1, 0]
lo, mid, hi = 0, 0, len(a) - 1
while mid <= hi:
if a[mid] == 0:
a[lo], a[mid] = a[mid], a[lo]; lo += 1; mid += 1
elif a[mid] == 1:
mid += 1
else:
a[mid], a[hi] = a[hi], a[mid]; hi -= 1
print(a)
[0, 1, 0, 1, 2, 2]
[0, 0, 1, 1, 2, 2]
Sign up free to see the answer and a step-by-step explanation.
What is the best time complexity for finding the k-th smallest element in an unsorted array, on average?
O(n²)
O(n) with Quickselect
O(log n)
O(n log n) with sorting only
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [4, 1, 2, 1, 4]
r = 0
for x in a:
r ^= x
print(r)
2
4
Sign up free to see the answer and a step-by-step explanation.
How can you find the duplicate in an array of n + 1 integers in the range 1..n without modifying it and using O(1) extra space?
Sort the array
Use a hash set
Use a nested loop with a counter array
Treat values as 'next' pointers and use Floyd's cycle detection
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]]
print([list(r) for r in zip(*m)])
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[3, 6, 9], [2, 5, 8], [1, 4, 7]]
Sign up free to see the answer and a step-by-step explanation.
[6, 8, 12, 24]
[24, 24, 24, 24]
[0, 1, 2, 0, 1, 2]
[2, 2, 1, 1, 0, 0]
3
1
[[7, 4, 1], [8, 5, 2], [9, 6, 3]]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]