Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the first occurrence index of a target element in an array.
DSA Typescript
function firstOccurrence(arr: number[], target: number): number {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === [1]) {
return i;
}
}
return -1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i' instead of 'target' in the comparison.
Comparing arr[i] with arr instead of target.
✗ Incorrect
We compare each element arr[i] with the target to find the first occurrence index.
2fill in blank
mediumComplete the code to find the last occurrence index of a target element in an array.
DSA Typescript
function lastOccurrence(arr: number[], target: number): number {
for (let i = arr.length - 1; i >= 0; i--) {
if (arr[i] === [1]) {
return i;
}
}
return -1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'arr' instead of 'target' in the comparison.
Looping forward instead of backward.
✗ Incorrect
We compare each element arr[i] with the target starting from the end to find the last occurrence index.
3fill in blank
hardFix the error in the binary search code to find the first occurrence of target in a sorted array.
DSA Typescript
function firstOccurrenceBinarySearch(arr: number[], target: number): number {
let low = 0;
let high = arr.length - 1;
let result = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === target) {
result = mid;
high = [1];
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting high = mid + 1 which moves right instead of left.
Setting low incorrectly after finding target.
✗ Incorrect
To find the first occurrence, after finding target at mid, we move high to mid - 1 to search left side.
4fill in blank
hardFill both blanks to complete the binary search code to find the last occurrence of target in a sorted array.
DSA Typescript
function lastOccurrenceBinarySearch(arr: number[], target: number): number {
let low = 0;
let high = arr.length - 1;
let result = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === target) {
result = mid;
low = [1];
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = [2];
}
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping low and high updates.
Not updating low or high correctly after finding target.
✗ Incorrect
To find last occurrence, after finding target at mid, move low to mid + 1 to search right side; else adjust high to mid - 1.
5fill in blank
hardFill all three blanks to create a function that returns both first and last occurrence indices of target in a sorted array using binary search.
DSA Typescript
function firstAndLastOccurrence(arr: number[], target: number): {first: number, last: number} {
let low = 0, high = arr.length - 1;
let first = -1, last = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === target) {
first = mid;
high = [1];
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
low = 0;
high = arr.length - 1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === target) {
last = mid;
low = [2];
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = [3];
}
}
return {first, last};
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up low and high updates in the two searches.
Not resetting low and high before second search.
✗ Incorrect
For first occurrence, move high to mid - 1; for last occurrence, move low to mid + 1; else adjust high to mid - 1.