Challenge - 5 Problems
First and Last Occurrence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Find first and last occurrence indexes
What is the output of the following JavaScript code that finds the first and last occurrence of the number 3 in the array?
DSA Javascript
const arr = [1, 3, 5, 3, 7, 3, 9]; const target = 3; function firstLastOccurrence(array, x) { let first = -1; let last = -1; for (let i = 0; i < array.length; i++) { if (array[i] === x) { if (first === -1) first = i; last = i; } } return { first, last }; } console.log(firstLastOccurrence(arr, target));
Attempts:
2 left
💡 Hint
Remember array indexes start at 0 and check all positions where the target appears.
✗ Incorrect
The number 3 appears at indexes 1, 3, and 5. The first occurrence is at index 1 and the last at index 5.
🧠 Conceptual
intermediate2:00remaining
Understanding output when element is absent
What will be the output of the function when the target element is not present in the array?
DSA Javascript
const arr = [2, 4, 6, 8]; const target = 5; function firstLastOccurrence(array, x) { let first = -1; let last = -1; for (let i = 0; i < array.length; i++) { if (array[i] === x) { if (first === -1) first = i; last = i; } } return { first, last }; } console.log(firstLastOccurrence(arr, target));
Attempts:
2 left
💡 Hint
Check what the initial values of first and last are and if they change when the element is not found.
✗ Incorrect
Since the target 5 is not in the array, the loop never updates first or last, so they remain -1.
❓ Predict Output
advanced2:00remaining
Output of recursive first occurrence search
What is the output of this recursive function that finds the first occurrence of a target in an array?
DSA Javascript
function firstOccurrenceRecursive(arr, target, index = 0) { if (index === arr.length) return -1; if (arr[index] === target) return index; return firstOccurrenceRecursive(arr, target, index + 1); } const array = [4, 2, 7, 2, 9]; console.log(firstOccurrenceRecursive(array, 2));
Attempts:
2 left
💡 Hint
The function checks elements from the start and returns the first index where the target is found.
✗ Incorrect
The first 2 appears at index 1, so the function returns 1.
❓ Predict Output
advanced2:00remaining
Output of last occurrence using reverse loop
What is the output of this code that finds the last occurrence of the target 5 in the array by looping backwards?
DSA Javascript
const arr = [5, 1, 5, 3, 5, 7]; const target = 5; function lastOccurrence(array, x) { for (let i = array.length - 1; i >= 0; i--) { if (array[i] === x) return i; } return -1; } console.log(lastOccurrence(arr, target));
Attempts:
2 left
💡 Hint
The loop starts from the end and returns the first matching index it finds.
✗ Incorrect
The last 5 is at index 4, so the function returns 4.
🚀 Application
expert3:00remaining
Count elements between first and last occurrence
Given an array and a target element, what is the output of the code that counts how many elements are between the first and last occurrence of the target (exclusive)?
DSA Javascript
const arr = [1, 3, 5, 3, 7, 3, 9]; const target = 3; function countBetweenFirstLast(array, x) { let first = -1; let last = -1; for (let i = 0; i < array.length; i++) { if (array[i] === x) { if (first === -1) first = i; last = i; } } if (first === -1 || last === -1 || first === last) return 0; return last - first - 1; } console.log(countBetweenFirstLast(arr, target));
Attempts:
2 left
💡 Hint
Count the number of elements strictly between the first and last occurrence indexes.
✗ Incorrect
First occurrence of 3 is at index 1, last at index 5. Elements between are at indexes 2,3,4 which is 3 elements. The formula last - first - 1 = 3.