Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Linear Search Algorithm in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main idea behind the Linear Search Algorithm?
easy
A. Divide the list into halves repeatedly to find the target
B. Check each element one by one until the target is found or list ends
C. Sort the list first then search using binary steps
D. Use a hash table to find the target instantly

Solution

  1. Step 1: Understand linear search process

    Linear search checks each element in order, from start to end, to find the target.
  2. Step 2: Compare with other search methods

    Binary search divides list, hash uses extra memory, but linear search is simple sequential check.
  3. Final Answer:

    Check each element one by one until the target is found or list ends -> Option B
  4. Quick Check:

    Linear search = sequential check [OK]
Hint: Linear search means checking items one by one [OK]
Common Mistakes:
  • Confusing linear search with binary search
  • Thinking linear search requires sorted list
  • Assuming linear search is always fastest
2. Which of the following is the correct TypeScript syntax to start a linear search loop over an array arr?
easy
A. for (let i = 0; i < arr.length; i++)
B. for (let i = 1; i <= arr.length; i++)
C. for (let i = 0; i <= arr.length; i++)
D. for (let i = 1; i < arr.length; i++)

Solution

  1. Step 1: Identify correct loop start and end

    Array indices start at 0 and go up to length-1, so loop must start at 0 and run while i < length.
  2. Step 2: Check each option's loop condition

    for (let i = 0; i < arr.length; i++) uses i=0 and i < arr.length, which is correct. Others either start at 1 or use <= which causes out-of-bounds.
  3. Final Answer:

    for (let i = 0; i < arr.length; i++) -> Option A
  4. Quick Check:

    Loop from 0 to length-1 [OK]
Hint: Array loops start at 0 and run while less than length [OK]
Common Mistakes:
  • Starting loop at 1 instead of 0
  • Using <= instead of < causing index errors
  • Confusing array length with last index
3. What will be the output of this TypeScript code?
const arr = [4, 7, 1, 9];
const target = 1;
let index = -1;
for (let i = 0; i < arr.length; i++) {
  if (arr[i] === target) {
    index = i;
    break;
  }
}
console.log(index);
medium
A. 1
B. -1
C. 2
D. 3

Solution

  1. Step 1: Trace the loop and compare elements

    Check arr[0]=4 (not 1), arr[1]=7 (not 1), arr[2]=1 (match found), set index=2 and break loop.
  2. Step 2: Output the index variable

    After loop, index is 2, so console.log prints 2.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    Target found at index 2 [OK]
Hint: Stop at first match and print its index [OK]
Common Mistakes:
  • Not breaking loop and returning last index
  • Confusing index with value
  • Assuming index starts at 1
4. Find the error in this linear search code snippet and choose the fix:
const arr = [5, 3, 8];
const target = 3;
let index = -1;
for (let i = 0; i <= arr.length; i++) {
  if (arr[i] === target) {
    index = i;
    break;
  }
}
console.log(index);
medium
A. Change loop condition to i < arr.length
B. Initialize index to 0 instead of -1
C. Remove the break statement
D. Change target to 5

Solution

  1. Step 1: Identify loop boundary error

    The loop uses i <= arr.length, which causes i to reach arr.length, an invalid index (out of bounds).
  2. Step 2: Fix loop condition to prevent out-of-bounds

    Changing condition to i < arr.length ensures i stays within valid indices 0 to length-1.
  3. Final Answer:

    Change loop condition to i < arr.length -> Option A
  4. Quick Check:

    Loop must run while i < length [OK]
Hint: Use < not <= for array index loops [OK]
Common Mistakes:
  • Using <= causing runtime error
  • Changing unrelated variables
  • Removing break causing unnecessary iterations
5. You want to find the last occurrence of a target number in an array using linear search. Which modification to this code will achieve that?
const arr = [2, 5, 3, 5, 7];
const target = 5;
let index = -1;
for (let i = 0; i < arr.length; i++) {
  if (arr[i] === target) {
    index = i;
    break;
  }
}
console.log(index);
hard
A. Sort the array before searching
B. Add break after setting index to stop at first occurrence
C. Start loop from the end and break at first match
D. Remove the break statement to continue searching all elements

Solution

  1. Step 1: Understand current code behavior

    The loop checks all elements; each time target matches, index updates to current i, so last match index remains after loop.
  2. Step 2: Identify why removing break helps

    If break was present, loop would stop at first match. Removing break ensures full array is checked for last occurrence.
  3. Final Answer:

    Remove the break statement to continue searching all elements -> Option D
  4. Quick Check:

    Keep searching to find last occurrence [OK]
Hint: To find last occurrence, check all elements without break [OK]
Common Mistakes:
  • Adding break stops search too early
  • Sorting changes original order and index meaning
  • Starting loop from end requires different logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard