Challenge - 5 Problems
Linear Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Linear Search on Array
What is the output of the following TypeScript code that performs a linear search for the number 7?
DSA Typescript
function linearSearch(arr: number[], target: number): number {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i;
}
}
return -1;
}
const numbers = [3, 5, 7, 9, 11];
console.log(linearSearch(numbers, 7));Attempts:
2 left
💡 Hint
Remember that array indexes start at 0.
✗ Incorrect
The number 7 is at index 2 in the array [3, 5, 7, 9, 11]. The linear search returns the index where the target is found.
❓ Predict Output
intermediate2:00remaining
Linear Search Result When Target Not Found
What does the following code print when searching for 10 in the array?
DSA Typescript
function linearSearch(arr: number[], target: number): number {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i;
}
}
return -1;
}
const numbers = [1, 3, 5, 7, 9];
console.log(linearSearch(numbers, 10));Attempts:
2 left
💡 Hint
If the target is not in the array, what does the function return?
✗ Incorrect
The function returns -1 when the target is not found in the array.
🔧 Debug
advanced2:00remaining
Find the Bug in Linear Search Implementation
The following code is intended to perform a linear search but does not work correctly. What is the bug?
DSA Typescript
function linearSearch(arr: number[], target: number): number {
let i = 0;
while (i <= arr.length) {
if (arr[i] === target) {
return i;
}
i++;
}
return -1;
}Attempts:
2 left
💡 Hint
Check the loop boundary to avoid accessing outside the array.
✗ Incorrect
Using i <= arr.length causes an out-of-bounds access when i equals arr.length, which is invalid. The correct condition is i < arr.length.
🧠 Conceptual
advanced1:30remaining
Time Complexity of Linear Search
What is the worst-case time complexity of the linear search algorithm on an array of size n?
Attempts:
2 left
💡 Hint
Think about how many elements might be checked in the worst case.
✗ Incorrect
In the worst case, linear search checks every element once, so the time complexity is O(n).
🚀 Application
expert3:00remaining
Linear Search on a Linked List
Given a singly linked list, which of the following TypeScript code snippets correctly performs a linear search for a target value and returns true if found, false otherwise?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } function linearSearchLinkedList(head: Node | null, target: number): boolean { // Fill in the correct code here }
Attempts:
2 left
💡 Hint
Remember to check the current node's value, not the next node's value.
✗ Incorrect
Option A correctly iterates through the list checking each node's value and returns true if the target is found. Option A misses the last node because it stops when current.next is null. Option A causes an error if head is null because do-while runs at least once. Option A incorrectly compares current.next to target instead of current.value.