Data Structures & AlgorithmsModerateTCS NQT · sorting
Problem: Using the bubble sort rule, what is the array after exactly one left-to-right pass over [3, 1, 2]? Run the code.
Problem: Using the bubble sort rule, what is the array after exactly one left-to-right pass over [3, 1, 2]? Run the code.
a = [3, 1, 2]
for j in range(len(a) - 1):
if a[j] > a[j + 1]:
a[j], a[j + 1] = a[j + 1], a[j]
print(a)What is printed?- A. [1, 3, 2]
- B. [1, 2, 3]
- C. [3, 1, 2]
- D. [2, 1, 3]
Correct answer: B. [1, 2, 3]
The first pass swaps 3 and 1, then 3 and 2, pushing the largest value to the end and giving [1, 2, 3]. Stopping after one comparison gives the trap [1, 3, 2].
More Coding Interview Problems questions
- Problem: How many legal moves does a knight have from the corner (0,0) of an 8 x 8 board? Run the code. python moves = [(1, 2), (2, 1)…
- Problem: What is 7^222 mod 10? Run the code. python print(pow(7, 222, 10)) What is printed?
- Problem: How many integers in [1, 500] are divisible by 4 or by 6? Run the code. python print(sum(1 for x in range(1, 501) if x % 4 == 0…
- Problem: How many ways can [1, 2, 3, 4] be split into two non-empty contiguous parts with equal sums? Run the code. python a = [1, 2, 3…
- Problem: Which algorithm finds all articulation points (cut vertices) of an undirected graph in O(V + E) time using DFS low-link values?
- Problem: Which statement about Kosaraju's algorithm for strongly connected components is correct?