• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

Formal Definition

Two-phase hashing is the conceptual separation of hash-based problem solving into two distinct phases: Phase 1 (information storage) builds the frequency or mapping structure by processing the input, and Phase 2 (information retrieval) queries that structure to answer the specific problem. Both phases use the same underlying hash structure but with different access patterns.

Explanation

Beginners often stop after building the frequency structure, unsure what to do next. The key insight is that building the hash is only half the work — the real problem-solving happens in Phase 2, where you traverse or query the structure differently depending on the question. Different problems (most frequent, first non-repeating, anagram detection) all share Phase 1 but diverge in Phase 2.

How It Works

  1. Phase 1 (Store): Choose a structure (array or hash map), iterate the input, populate frequencies
  2. Phase 2 (Use): Query the structure — this could mean finding the max, re-traversing the input for order, or comparing two frequency maps
  3. The distinction is mental but powerful: it separates mechanical work from analytical work

Mathematical Formulation

Phase 1: Hbuild(S)where H is the hash structure\text{Phase 1: } H \leftarrow \text{build}(S) \quad \text{where } H \text{ is the hash structure}

Phase 2: answerquery(H,S)oranswerquery(H)\text{Phase 2: } \text{answer} \leftarrow \text{query}(H, S) \quad \text{or} \quad \text{answer} \leftarrow \text{query}(H)

Visual Explanation

two_phase_hashing INPUT Input String 'banana' P1 Phase 1: Store Build Frequency Structure INPUT->P1 STRUCT Frequency Map {a:3, b:1, n:2} P1->STRUCT P2 Phase 2: Use Query for Answer STRUCT->P2 MAX Most Frequent Traverse struct, track max P2->MAX FIRST First Non-Repeat Re-traverse string, check freq==1 P2->FIRST ANA Anagram Compare two structs P2->ANA PRINT Print All Traverse struct, print non-zero P2->PRINT

Semantic Network

semantic_two_phase_hashing THIS Two-Phase Hashing PRE1 Hashing Store Phase THIS--PRE1 built from PRE2 Hashing Retrieval Phase THIS--PRE2 built from OUT1 Most Frequent Character THIS--OUT1 builds into OUT2 First Non-Repeating Character THIS--OUT2 builds into OUT3 Anagram Detection THIS--OUT3 builds into CON1 Single-Pass Algorithms THIS--CON1 contrasts with REL1 Frequency Array THIS--REL1 related REL2 Unordered Map Frequency THIS--REL2 related

Key Properties

  • Universal pattern across all hash-based string problems
  • Phase 1 is nearly identical across problems — only the structure type varies
  • Phase 2 varies significantly — this is where problem-specific logic lives
  • Understanding this separation is the threshold between beginner and intermediate problem-solving
  • Enables modular thinking: change Phase 2 without modifying Phase 1

Connections

Edge Cases & Gotchas

  • Some problems can be solved in one pass (Phase 1 and 2 interleaved) — the two-phase model is conceptual, not always sequential
  • For first non-repeating character, Phase 2 re-traverses the original string, not the hash structure — a common confusion point
  • For anagram detection, Phase 1 runs twice (once per string), then Phase 2 compares — the phases apply per-string
  • Overlapping Phase 1 and 2 can be more efficient but harder to reason about