Skip to article content
FrontendXLesson 04 / 24

Linear search and worst-case reasoning

Start with no coding-interview background, learn to trace an algorithm by hand, measure its cost, and finish able to reason about arrays, hashing, trees, graphs, dynamic programming and the trade-offs an interviewer expects you to name.

beginner · about 20 minutes · 3 exercises

  • Predict
  • Explain
  • Debug

What you will be able to do

#
  • Implement linear search and return a sentinel when the value is absent.
  • Distinguish best, average and worst case for a scan.
  • Explain why early exit does not change the Big-O class.

Overview

#

Scan until found

Linear search checks each element in order until it finds the target or runs out of elements:

snippet.txt
function indexOf(values, target) {
  for (let i = 0; i < values.length; i++) {
    if (values[i] === target) return i;
  }
  return -1;
}

Three cases

  • Best case: the target is at index 0, so one comparison. O(1).
  • Average case: on average about n / 2 comparisons. Still O(n).
  • Worst case: the target is last or absent, so n comparisons. O(n).

Big-O usually reports the worst case, because that is the guarantee you can make. Early exit helps in the average case but does not change the worst-case class.

Returning a sentinel

When the value is absent, return -1 (or null) rather than undefined. A named sentinel makes the contract explicit and lets callers test for it deliberately. Do not return 0, because 0 is a valid index.

When linear search is the right tool

If the data is unsorted and you search once, linear search is optimal: you cannot do better than reading each element once, because any unread element could be the target. Sorting first only pays off when you search many times.

Practice next

  • Count comparisons in the worst case for a list of length 10.
  • Explain when it is worth sorting before searching.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    How many comparisons does linear search perform for target 5 on [1, 2, 3, 4, 5]?

    starter.javascript
    for (let i = 0; i < values.length; i++) {
      if (values[i] === target) return i;
    }
    return -1;

    Reveal the reference answer
    reference.javascript
    It compares 1, 2, 3, 4 and 5 in order, so five comparisons before returning index 4.

    Authored expected output: 5

    The target is last, which is the worst case for this input.

  2. Exercise 2Debug

    This function is supposed to return -1 when the target is absent, but callers treat a returned 0 as not found. Identify the bug.

    starter.javascript
    function indexOf(values, target) {
      for (let i = 0; i < values.length; i++) {
        if (values[i] === target) return i;
      }
      return 0;
    }

    Reveal the reference answer
    reference.javascript
    function indexOf(values, target) {
      for (let i = 0; i < values.length; i++) {
        if (values[i] === target) return i;
      }
      return -1;
    }

    Authored expected output: -1 when the target is absent

    Returning 0 collides with the valid index 0. A distinct sentinel such as -1 removes the ambiguity.

  3. Exercise 3Explain

    Explain why linear search cannot be beaten for a single search in an unsorted array.

    Reveal the reference answer
    reference.text
    Any element you have not inspected could be the target, so a correct algorithm must be prepared to inspect every element in the worst case. That lower bound is n reads, which linear search achieves.

    Authored expected output: Every element might be the target, so n reads is a lower bound that linear search meets.

    This kind of argument, called an adversary argument, is a common way to show an algorithm is optimal.

Common misconceptions

#
  • Early exit makes the algorithm faster than O(n). It improves the constant and the average case, not the worst-case class.
  • Returning 0 means not found. 0 is a valid index; use -1 or null.
  • Linear search is always bad. For a single unsorted search it is optimal.

Follow-up practice

#
  • dsa-from-zero-binary-search

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.