Challenge - 5 Problems
Bubble Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Bubble Sort code?
Consider the following JavaScript code that sorts an array using Bubble Sort. What will be the printed output after running this code?
DSA Javascript
const arr = [5, 3, 8, 4, 2]; for (let i = 0; i < arr.length - 1; i++) { for (let j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { const temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } console.log(arr.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Remember Bubble Sort swaps adjacent elements if they are in the wrong order, pushing the largest to the end each pass.
✗ Incorrect
Bubble Sort compares pairs and swaps them if needed, so the largest numbers move to the right. After all passes, the array is sorted ascending: 2, 3, 4, 5, 8.
🧠 Conceptual
intermediate1:30remaining
How many total swaps does Bubble Sort perform on this array?
Given the array [4, 2, 7, 3], how many swaps will Bubble Sort perform to sort it in ascending order?
Attempts:
2 left
💡 Hint
Count each time two adjacent elements are swapped during the sorting process.
✗ Incorrect
Initial: [4,2,7,3]
Pass 1: swap 4 and 2 → [2,4,7,3], swap 7 and 3 → [2,4,3,7] (2 swaps)
Pass 2: swap 4 and 3 → [2,3,4,7] (1 swap)
Pass 3: no swaps
Total swaps = 3.
❓ Predict Output
advanced2:00remaining
What error does this Bubble Sort code produce?
Analyze the following Bubble Sort code snippet. What error will it raise when run?
DSA Javascript
const arr = [3, 1, 4]; for (let i = 0; i <= arr.length; i++) { for (let j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; } } } console.log(arr.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Check the loop conditions carefully for off-by-one errors causing undefined access.
✗ Incorrect
The outer loop runs from i=0 to i=3 (arr.length=3). When i=3, inner loop j < -1 does not run. No invalid array access occurs. The array is sorted correctly to [1,3,4] with no error.
🔧 Debug
advanced2:00remaining
Identify the bug in this Bubble Sort implementation
This Bubble Sort code is intended to sort an array ascending but produces incorrect output. What is the bug?
DSA Javascript
const arr = [6, 2, 9, 1]; for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr.length - 1; j++) { if (arr[j] < arr[j + 1]) { const temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } console.log(arr.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Check the comparison direction to see if it sorts ascending or descending.
✗ Incorrect
The code uses 'arr[j] < arr[j + 1]' which swaps if left is less than right, causing descending order. To sort ascending, it should be 'arr[j] > arr[j + 1]'.
🚀 Application
expert2:00remaining
How many passes does Bubble Sort need to sort this array?
Given the array [1, 2, 3, 4, 5], how many passes (outer loop iterations) will Bubble Sort perform before it stops if optimized to stop early when no swaps occur?
DSA Javascript
const arr = [1, 2, 3, 4, 5]; let n = arr.length; for (let i = 0; i < n; i++) { let swapped = false; for (let j = 0; j < n - 1 - i; j++) { if (arr[j] > arr[j + 1]) { [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; swapped = true; } } if (!swapped) break; } console.log(arr.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
If the array is already sorted, the algorithm stops after the first pass with no swaps.
✗ Incorrect
Since the array is already sorted, no swaps happen in the first pass, so the loop breaks immediately after 1 pass.