Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the first index of the target element using binary search.
DSA Javascript
function findFirstIndex(arr, target) {
let left = 0, right = arr.length - 1;
let result = -1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
result = mid;
right = mid [1] 1;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes infinite loop or wrong index.
Using '*' or '/' operators here is incorrect.
✗ Incorrect
When we find the target, we move the right pointer to mid - 1 to continue searching on the left side for the first occurrence.
2fill in blank
mediumComplete the code to find the last index of the target element using binary search.
DSA Javascript
function findLastIndex(arr, target) {
let left = 0, right = arr.length - 1;
let result = -1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
result = mid;
left = mid [1] 1;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes wrong last index.
Using '*' or '/' operators here is incorrect.
✗ Incorrect
When the target is found, move the left pointer to mid + 1 to search the right side for the last occurrence.
3fill in blank
hardFix the error in the countOccurrences function to correctly calculate the count of the target element.
DSA Javascript
function countOccurrences(arr, target) {
const first = findFirstIndex(arr, target);
if (first === -1) return 0;
const last = findLastIndex(arr, target);
return last [1] first + 1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes wrong count.
Using '*' or '/' operators here is incorrect.
✗ Incorrect
The count is last index minus first index plus one, so we subtract first from last.
4fill in blank
hardFill both blanks to create a dictionary of counts for each unique element in the sorted array.
DSA Javascript
function countAllOccurrences(arr) {
const counts = {};
for (let i = 0; i < arr.length; i++) {
const element = arr[i];
counts[element] = counts[element] [1] 1 || [2];
}
return counts;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes wrong counts.
Using 0 as initial count causes no increment.
✗ Incorrect
We add 1 to the existing count or start with 1 if undefined.
5fill in blank
hardFill all three blanks to create a function that returns the count of the target element using binary search helper functions.
DSA Javascript
function countTarget(arr, target) {
const first = [1](arr, target);
if (first === -1) return 0;
const last = [2](arr, target);
return last [3] first + 1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using countOccurrences inside itself causes recursion error.
Mixing up first and last indices causes wrong count.
✗ Incorrect
Use findFirstIndex and findLastIndex to get indices, then subtract first from last and add one for count.