Skip to content
Tech Interview Prep
Technical interview guide

Sequence Alignment (Needleman-Wunsch, Smith-Waterman, BLAST)

The algorithms for comparing DNA, RNA, and protein sequences: global vs. local alignment, and the heuristics that make searching billions of bases practical.

Minutes to read
3
Practice MCQs
25
Interview Q&A
25
Edition
v8
Editorial status
Reviewed

Scope: Needleman-Wunsch and Smith-Waterman algorithms; NCBI BLAST+ documentation and statistical guidance; BLOSUM and PAM source publications reviewed 2026-09-04.

Overview

Curated: · Written: · Reviewed:

Sequence Alignment (Needleman-Wunsch, Smith-Waterman, BLAST)

In one line

An alignment is a model-based claim about which residues correspond, under a scoring model, a gap model and a search space. Use Needleman-Wunsch for end-to-end global comparison, Smith-Waterman for the best local segment, and BLAST when you must search a large database and need E-values.

Key definitions

  • Global alignment: every residue of both sequences is aligned, end gaps included.
  • Local alignment: the best-scoring pair of subsequences, with each DP cell floored at zero.
  • Substitution matrix: log-odds score per residue pair. BLOSUM62 is the blastp default.
  • Affine gap: one opening cost plus an extension cost per further residue. Needs three DP states (Gotoh).
  • Bit score: raw score normalized by the scoring system. Comparable across searches that use the same matrix and gap costs.
  • E-value: expected number of chance alignments at or above that score in the effective search space.

Formulas and complexity

S′=λS−ln⁡Kln⁡2S' = \frac{\lambda S - \ln K}{\ln 2} E=m′n′2−S′E = m' n' 2^{-S'} gap cost(k)=open+(k−1) extend\text{gap cost}(k) = \text{open} + (k-1)\,\text{extend}

Full DP: O(nm) time and space. Hirschberg: O(nm) time, O(min(n,m)) space, roughly double the work. Banded: roughly O(n w) for band width w.

How it works

  • Needleman-Wunsch fills each cell with the maximum of diagonal, up and left moves. Boundaries carry cumulative gap penalties, and the answer is the bottom-right cell.
  • Smith-Waterman floors each cell at zero. Traceback starts at the highest cell and stops at a zero.
  • The matrix stores scores, not alignments. Traceback needs stored pointers or recomputation, and ties need a fixed order.
  • BLAST indexes query words (blastp uses word size 3), finds seed hits, requires two nearby seeds on one diagonal, extends ungapped with an X-drop, then refines with a gapped alignment.
no yes Query words of length 3 above threshold Scan database for seed hits Two seeds on same diagonal? Ungapped extension with X-drop Gapped refinement with X-drop Bit score and E-value

When to use and when not to

SituationUseAvoid
Similar-length proteins, homologous end to endNeedleman-WunschSmith-Waterman, which returns a fragment
One shared domain in otherwise unrelated proteinsSmith-Waterman or BLAST local HSPNeedleman-Wunsch, which forces poor end alignment
Search over millions of residuesBLAST with E-valuesFull Smith-Waterman, exact but slow
Chromosome-scale pairBanded or HirschbergFull matrix, 9 x 10^12 cells for 3 Mb by 3 Mb
Protein query, unannotated nucleotide genometblastnblastp, which finds no protein database to search
Very close homologsBLOSUM80 or BLOSUM90BLOSUM45
Distant homologsBLOSUM45 or PAM250BLOSUM80

Traps interviewers probe

  1. Treating percent identity as significance. Fix: report the E-value, the threshold and the database together, and read identity with alignment length and coverage.
  2. Reading an E-value as the probability that a hit is correct, or as proof of homology or function. Fix: it is an expected count of chance alignments. Homology is common ancestry. Function needs domain architecture and catalytic residues.
  3. Assuming a bigger database changes the bit score. Fix: the bit score is fixed. E scales with the search space, so doubling the database doubles E for the same hit.
  4. Using Needleman-Wunsch when only a domain is shared. Fix: use local alignment, since global alignment forces gaps across the ends.
  5. Reading a missing hit as absence. Fix: BLAST is a heuristic. Word size, masking, matrix and gap costs all change what it finds.

Interview tips

Open with the question type and the model: global or local, matrix, gap model, search space and threshold.

Expected follow-ups: what happens when the database doubles (E doubles, bit score unchanged); why BLOSUM62 versus BLOSUM80 or BLOSUM45; why Needleman-Wunsch cannot run on genomes; why two correct aligners show different alignments (tied optima); why a reciprocal best hit is not enough for orthology.

30-second outline: state the question and the model. Name the algorithm and why it fits. Give the statistic and the threshold, and say what a missing hit would not mean. Name the evidence still needed before claiming orthology or function.

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.