0
0
DSA Typescriptprogramming~10 mins

First and Last Occurrence of Element in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aarr
Bi
Ctarget
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i' instead of 'target' in the comparison.
Comparing arr[i] with arr instead of target.
2fill in blank
medium

Complete 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'
Atarget
Barr
Ci
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'arr' instead of 'target' in the comparison.
Looping forward instead of backward.
3fill in blank
hard

Fix 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'
Amid + 1
Blow + 1
Chigh - 1
Dmid - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Setting high = mid + 1 which moves right instead of left.
Setting low incorrectly after finding target.
4fill in blank
hard

Fill 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'
Amid + 1
Bmid - 1
Chigh - 1
Dlow + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping low and high updates.
Not updating low or high correctly after finding target.
5fill in blank
hard

Fill 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'
Amid - 1
Bmid + 1
Chigh - 1
Dlow + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up low and high updates in the two searches.
Not resetting low and high before second search.