Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the binary search with the correct left pointer.
DSA Javascript
function findMin(nums) {
let left = [1];
let right = nums.length - 1;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting left at 1 or -1 causes missing the first element.
Starting left at nums.length is out of bounds.
✗ Incorrect
The left pointer should start at index 0 to cover the entire array.
2fill in blank
mediumComplete the code to calculate the middle index correctly.
DSA Javascript
function findMin(nums) {
let left = 0;
let right = nums.length - 1;
while (left < right) {
let mid = [1];
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using (left + right) / 2 without floor gives a float index.
Using Math.ceil can cause infinite loops in some cases.
✗ Incorrect
Using Math.floor ensures mid is an integer index within bounds.
3fill in blank
hardFix the error in the condition to decide which side to search next.
DSA Javascript
function findMin(nums) {
let left = 0;
let right = nums.length - 1;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] [1] nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or <= reverses the logic and causes wrong results.
Using >= can cause infinite loops.
✗ Incorrect
If nums[mid] is greater than nums[right], the minimum is in the right half.
4fill in blank
hardFill both blanks to correctly update pointers based on comparison.
DSA Javascript
function findMin(nums) {
let left = 0;
let right = nums.length - 1;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] [1] nums[right]) {
left = [2];
} else {
right = mid;
}
}
return nums[left];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > reverses the search direction.
Updating left to mid - 1 can skip the minimum element.
✗ Incorrect
If nums[mid] > nums[right], move left to mid + 1 to search right half.
5fill in blank
hardFill all three blanks to create a dictionary of indices to values for elements less than or equal to 3.
DSA Javascript
function filterValues(nums) {
return nums.reduce((acc, num, index) => {
if (num [1] 3) {
acc[[2]] = [3];
}
return acc;
}, {});
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of <= causes wrong filtering.
Using num as key or index as value swaps keys and values.
✗ Incorrect
We check if num <= 3, then store acc[index] = num.