Probabilistic Analysis and Randomized Algorithms: The Hiring Problem Explained

Some algorithms make random choices during execution, and analyzing their expected behavior requires a different toolkit than worst-case analysis alone. This comprehensive guide introduces probabilistic analysis through the classic hiring problem, explains indicator random variables as a powerful analytical tool, and shows how randomization can improve an algorithm's expected performance.

Randomized AlgorithmsProbabilistic AnalysisIndicator Random Variables

~5 min read · Updated Sep 7, 2026

Why Randomness Enters Algorithm Analysis

The worst-case, best-case, and average-case running times discussed earlier in this series all assume the algorithm itself behaves deterministically, and any variation comes purely from the input. A different situation arises when either the input distribution is unknown or an algorithm deliberately makes random choices during its own execution. Both situations require the tools of Probabilistic Analysis.

The Hiring Problem: A Motivating Example

Consider a company interviewing candidates one at a time for a position, always hiring the current best candidate seen so far and firing the previous hire. Each interview costs a small amount, but each hire costs significantly more, since it involves paperwork, onboarding, and severance for the person being replaced. The question is: what is the expected total hiring cost across the entire process?

HIRE-ASSISTANT(n):
  best = candidate 0 (a placeholder, ranked worst)
  for i = 1 to n:
      interview candidate i
      if candidate i is better than best:
          best = candidate i
          hire candidate i

If candidates arrive in the worst possible order — already sorted from worst to best — every single candidate is hired, resulting in n hires, a costly worst case. But if the order of candidates is random, far fewer hires are expected on average, since a random arrival order makes it unlikely that many consecutive candidates each set a new record.

Two Approaches to Handling the Order of Inputs

There are two distinct ways to reason about this randomness, and it is important not to confuse them.

  • Probabilistic Analysis of a Deterministic Algorithm: assume the input itself comes from some probability distribution (such as a uniformly random ordering of candidates), and analyze the expected running time of a fixed, non-random algorithm over that input distribution.
  • Randomized Algorithms: the algorithm itself makes random choices during execution (such as randomly shuffling the candidate order before processing them, regardless of the order they actually arrived in), guaranteeing good expected performance for any input, since the randomness comes from the algorithm rather than an assumption about the input.

The second approach is generally more powerful and reliable in practice, since it removes any dependence on assumptions about how inputs are distributed in the real world, which may not hold. A Randomized Algorithm for the hiring problem simply permutes the candidates randomly before running the same procedure, guaranteeing the same good expected cost regardless of the input's original order.

Indicator Random Variables: A Powerful Analytical Tool

Computing an expected value directly can be complicated when many interacting events are involved. Indicator Random Variables provide an elegant technique that dramatically simplifies such calculations, especially when combined with the linearity of expectation.

For an event A, define the indicator random variable:

I{A} = 1  if A occurs
I{A} = 0  if A does not occur

Key property: E[I{A}] = Pr{A}

The expected value of an indicator variable simply equals the probability of the event it indicates. This becomes powerful when combined with Linearity of Expectation, which states that the expected value of a sum of random variables equals the sum of their expected values, regardless of whether the variables are independent.

E[X1 + X2 + ... + Xn] = E[X1] + E[X2] + ... + E[Xn]

This holds even when the Xi are NOT independent —
a crucial and often surprising fact

Applying Indicator Variables to the Hiring Problem

Let Xi be the indicator random variable for the event that candidate i is hired. The total number of hires is X = X1 + X2 + ... + Xn. By linearity of expectation:

E[X] = E[X1] + E[X2] + ... + E[Xn]
     = Σ Pr{candidate i is hired}

Candidate i is hired precisely when candidate i is the best among the first i candidates seen so far. If the candidates arrive in a uniformly random order, candidate i is equally likely to be the best, second-best, or any rank among the first i candidates, so:

Pr{candidate i is hired} = 1/i

Therefore:
E[X] = Σ (i=1 to n) 1/i = H(n)

This is the Harmonic Series, and H(n) = Θ(ln n)

This remarkable result shows that, despite there being n candidates, the expected number of hires grows only logarithmically with n, a dramatic improvement over the worst-case scenario of n hires. This calculation, made simple through indicator variables, would be considerably more complex using direct probability calculations involving joint distributions.

Why This Technique Generalizes So Widely

The indicator random variable technique is not specific to the hiring problem; it is a general tool applicable whenever a quantity of interest can be expressed as a sum of simpler zero-or-one outcomes, even when those outcomes are correlated with each other. This makes it one of the most broadly useful techniques in the probabilistic analysis of algorithms, and it reappears throughout later topics in this series wherever expected running time needs to be computed.

Why Randomization Matters for Real-World Algorithm Design

Randomized algorithms are used throughout computer science specifically because they can guarantee good expected performance without needing any assumption about the distribution of real-world inputs, protecting against adversarial or unusually structured inputs that could otherwise trigger an algorithm's worst case. A prominent example, explored in depth later in this series, is randomized quicksort, where randomly shuffling the input before sorting protects against the specific input orderings that would otherwise trigger quicksort's quadratic worst case.

Written & researched by Dr. Shahin Siami

Related Articles

Binary Search Trees: Querying, Inserting, and Deleting Efficiently

A binary search tree maintains elements in sorted order while supporting efficient search, insertion, and deletion, all in time proportional to the tree's height. This comprehensive guide covers the defining binary-search-tree property, the core query operations including search, minimum, maximum, and successor, and the more intricate insertion and deletion procedures that must carefully preserve the tree's structure.

Continue

Hash Tables Explained: From Direct Addressing to Open Addressing

Hash tables provide expected constant-time lookup, insertion, and deletion, making them one of the most widely used data structures in practice. This comprehensive guide covers the direct-addressing idea that motivates hashing, how collisions are handled through chaining, the properties of good hash functions, open addressing as a memory-efficient alternative, and practical considerations for real-world hash table implementations.

Continue

Elementary Data Structures: Stacks, Queues, Linked Lists, and Trees

Before tackling advanced data structures, mastering the elementary building blocks is essential, since nearly every complex structure is built from these fundamentals. This comprehensive guide covers array-based stacks and queues, singly and doubly linked lists, and the standard techniques for representing rooted trees, including the clever left-child right-sibling representation for trees with unbounded branching.

Continue

Finding the Median Without Fully Sorting: Linear-Time Selection Algorithms

Finding the k-th smallest element in an unsorted array does not require the full Θ(n log n) cost of sorting; it can be done in linear time. This comprehensive guide covers the trivial case of finding the minimum or maximum, an elegant randomized selection algorithm with linear expected time, and a more intricate deterministic algorithm that guarantees linear time even in the worst case.

Continue

Beating the n log n Barrier: Linear-Time Sorting Algorithms Explained

Every comparison-based sorting algorithm requires at least Ω(n log n) time in the worst case, but algorithms that avoid comparisons entirely can sort in linear time under the right conditions. This comprehensive guide proves the comparison-sort lower bound using a decision tree argument, then explains three linear-time algorithms — counting sort, radix sort, and bucket sort — along with the specific input assumptions each requires.

Continue

Quicksort: A Complete Guide to Description, Performance, and Randomization

Quicksort is one of the most widely used sorting algorithms in practice, prized for its excellent average-case performance and in-place operation, despite having a poor theoretical worst case. This comprehensive guide covers the partition-based algorithm in detail, analyzes both its worst-case and expected running time, and explains how randomization transforms it into a reliably efficient algorithm regardless of input order.

Continue