Field note / fundamentals

publishedupdated 2026-05-04#algorithms#arrays#hashing#interview#practice

Algorithms Practice: Arrays & Hashing

The array and hash-map patterns I practice first for interview speed: lookup, grouping, counting, prefix ideas, and duplicate detection.

TL;DR

  • Arrays and hashing are the highest-return interview patterns to practice early.
  • I look for membership checks, frequency counting, grouping, and “have I seen this before?” signals.
  • The usual upgrade is from nested loops to one pass plus a map or set.
  • I practice these until the time-space trade-off feels automatic.

Context

When I warm back up for interviews, arrays and hashing are where I start. They build speed fast, and they reinforce the habit of spotting data-structure leverage instead of brute-forcing everything with loops.

The Approach

Patterns I want ready:

  • contains duplicate
  • two-sum lookup
  • anagram / frequency match
  • grouping by key
  • prefix aggregation
function containsDuplicate(values: number[]): boolean {
  const seen = new Set<number>()

  for (const value of values) {
    if (seen.has(value)) return true
    seen.add(value)
  }

  return false
}
function twoSum(nums: number[], target: number): number[] {
  const indexByValue = new Map<number, number>()

  for (let index = 0; index < nums.length; index += 1) {
    const complement = target - nums[index]
    if (indexByValue.has(complement)) {
      return [indexByValue.get(complement)!, index]
    }

    indexByValue.set(nums[index], index)
  }

  return []
}

Trade-offs

  • Hashing often buys time complexity, but it spends memory.
  • Array-based approaches are simple, but they become slow if I repeat linear scans unnecessarily.
  • I only reach for sorting first when order itself helps solve the problem.

References