0
0
DSA Typescriptprogramming~20 mins

Linear Search Algorithm in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linear Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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));
A-1
B3
C2
D1
Attempts:
2 left
💡 Hint
Remember that array indexes start at 0.
Predict Output
intermediate
2: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));
A0
B5
C4
D-1
Attempts:
2 left
💡 Hint
If the target is not in the array, what does the function return?
🔧 Debug
advanced
2: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;
}
AThe variable i should be initialized to 1 instead of 0
BThe loop condition should be i < arr.length, not i <= arr.length
CThe target comparison should use != instead of ===
DThe return value should be i + 1 instead of i
Attempts:
2 left
💡 Hint
Check the loop boundary to avoid accessing outside the array.
🧠 Conceptual
advanced
1:30remaining
Time Complexity of Linear Search
What is the worst-case time complexity of the linear search algorithm on an array of size n?
AO(n)
BO(log n)
CO(n log n)
DO(1)
Attempts:
2 left
💡 Hint
Think about how many elements might be checked in the worst case.
🚀 Application
expert
3: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
}
A
let current = head;
while (current !== null) {
  if (current.value === target) {
    return true;
  }
  current = current.next;
}
return false;
B
let current = head;
while (current.next !== null) {
  if (current.value === target) {
    return true;
  }
  current = current.next;
}
return false;
C
let current = head;
do {
  if (current.value === target) {
    return true;
  }
  current = current.next;
} while (current !== null);
return false;
D
let current = head;
while (current !== null) {
  if (current.next === target) {
    return true;
  }
  current = current.next;
}
return false;
Attempts:
2 left
💡 Hint
Remember to check the current node's value, not the next node's value.