Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    DSADSA

    Foundations: measuring code

    • Big-O: how code grows
    • Space and the time–memory trade-off
    • Arrays and strings under the hood
    • Hash maps and sets
    • How to approach any interview problem

    Array patterns

    • Two pointers
    • Sliding window
    • Prefix sums
    • Binary search, done right
    • Sorting: merge sort and quicksort

    Stacks, queues and linked lists

    • Stacks and monotonic stacks
    • Queues and deques
    • Linked lists

    Recursion and trees

    • Recursion without fear
    • Backtracking: try and undo
    • Binary trees
    • Binary search trees
    • Heaps and priority queues

    Graphs and dynamic programming

    • Graphs: BFS and DFS
    • Shortest paths, topological sort and union-find
    • Dynamic programming: the idea
    • DP on grids and strings
    • Greedy: the best choice right now
    DSA

    Foundations: measuring code · Lesson 4 of 23

    Hash maps and sets

    The most useful interview tool: O(1) lookups and inserts, counting and grouping.

    12 minutes3 games

    A hash map (JavaScript's Map) stores key → value pairs. Instead of scanning an array, it computes a "drawer number" (a hash) from the key and goes straight there, so lookup, insert and delete are O(1) on average.

    A Set is the same idea with keys only: "is this here?".

    Key "mona"

    Hash function

    Bucket 3

    Value 340

    The same key always lands in the same bucket, so there's no searching.

    JavaScript
    // Counting: the most common hash-map pattern
    const words = ["sea", "code", "sea", "learn", "code", "sea"]
    const count = new Map()
    for (const w of words) count.set(w, (count.get(w) || 0) + 1)
    console.log(count.get("sea"), count.get("learn"), count.has("java"))
    
    // Grouping: words with the same letters share a sorted key
    const groups = new Map()
    for (const w of ["eat", "tea", "tan", "ate", "nat"]) {
      const key = [...w].sort().join("")
      if (!groups.has(key)) groups.set(key, [])
      groups.get(key).push(w)
    }
    console.log([...groups.values()])

    Map or object?

    A plain object {} can act as a hash map, but Map is better for interviews: keys can be any type (real numbers, not strings), it has size, keeps insertion order, and has no inherited keys like toString.

    Important

    That O(1) is an average. If many keys share a bucket (collisions), lookups slow down; the theoretical worst case is O(n). Libraries avoid it by growing the table as it fills. Also note that arrays and objects compare by reference, so set.has([1, 2]) is false even after adding [1, 2]; use a string key like "1,2".

    What's the output?

    const seen = new Set()
    seen.add([1, 2])
    seen.add("1,2")
    console.log(seen.has([1, 2]), seen.has("1,2"), seen.size)

    What will this code print?

    Order the lines

    Order the lines of a hash-map two-sum (O(n)).

    1. Click the lines in the right order

    Interview question

    How does a hash table handle collisions?

    Exercise

    Write firstUnique(s) returning the index of the first non-repeating letter, or -1. It must print 3 -1.

    Expected output: 3 -1

    JavaScript
    function firstUnique(s) {
      
    }
    
    console.log(firstUnique("aabcb"), firstUnique("aabb"))

    Practise on real problems

    Practice problems that use what you learned here. Solve them in JavaScript, Python or C++.

    • Pair with target sumEasy
    • Is it an anagram?Easy
    • Group the anagramsMedium
    • Top k most frequentMedium
    • Longest consecutive runMedium
    • First unique characterEasy
    • Isomorphic stringsEasy

    Learn more from

    • MDN: Mapdeveloper.mozilla.org
    • Princeton Algorithms: Hash Tablesalgs4.cs.princeton.edu
    • VisuAlgo: Hash Tablevisualgo.net

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousArrays and strings under the hoodNextHow to approach any interview problem
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.