AIUnlimited
๐ŸŒณ

AI Foundations

๐ŸŒฑ
AI Seeds

Start from zero

๐ŸŒฟ
AI Sprouts

Build foundations

๐ŸŒณ
AI Branches

Apply in practice

๐Ÿ•๏ธ
AI Canopy

Go deep

๐ŸŒฒ
AI Forest

Master AI

๐Ÿ”จ

AI Mastery

โœ๏ธ
AI Sketch

Start from zero

๐Ÿชจ
AI Chisel

Build foundations

โš’๏ธ
AI Craft

Apply in practice

๐Ÿ’Ž
AI Polish

Go deep

๐Ÿ†
AI Masterpiece

Master AI

๐Ÿ“˜

AI Practice

๐Ÿ“–
Understanding Open-Source Models

Fundamentals and resources for open-source models

๐ŸŽฏ
From Problem to Model Task

Converting business problems to model tasks

โšก
Running Your First Model

See your first results in 30 minutes

๐Ÿ”ง
Fine-Tuning and Evaluation

Fine-tune models and evaluate performance

๐Ÿš€
Application Systems

Build real-world AI applications

๐ŸŽจ
Generative AI

Explore open-source AIGC models

๐Ÿค–
Agents

Learn Agent frameworks and MCP tools

๐Ÿ“
Supplementary Fundamentals

LLM basics and evaluation

๐ŸŽ“

Claude Academy

๐Ÿค–
Claude 101

Learn AI basics with Claude

๐Ÿ’ป
Claude Code 101

Code with Claude as your pair programmer

๐Ÿค
Introduction to Claude Cowork

Collaborate with Claude on complex projects

โš™๏ธ
Claude Platform 101

Build apps with the Claude API

Lab

7 experiments loaded
๐ŸงฌNeural Network Sandbox๐Ÿค–AI or Human?๐Ÿฅ‹Prompt Engineering Dojo๐ŸAlgorithm Race๐Ÿง AI Trivia Challenge๐Ÿ—๏ธSystem Design Canvas
๐ŸŽฏMock InterviewEnter the Labโ†’
๐Ÿš€

Career Development

๐Ÿš€
Interview Launchpad

Start your journey

๐ŸŒŸ
Behavioral Mastery

Master soft skills

๐Ÿ’ป
Technical Interviews

Ace the coding round

๐Ÿค–
AI & ML Interviews

ML interview mastery

๐Ÿ†
Offer & Beyond

Land the best offer

Get Started
AIUnlimited

MIT Licence.

ๆฒชICPๅค‡18025655ๅท-11

Learn

  • AI Basics
  • AI Practice
  • Claude Academy
  • Lab
  • Career Development

Community

  • About
  • FAQ

Support

  • Terms of Service
  • Privacy Policy
  • Contact
AI & Engineering Academicsโ€บโœ๏ธ AI Sketchโ€บLessonsโ€บArrays and Hash Maps
๐Ÿ“ฆ
AI Sketch โ€ข Intermediateโฑ๏ธ 15 min read

Arrays and Hash Maps

Why Data Storage Matters in AI

Every AI system needs to store and retrieve data - quickly. Whether it's a list of pixel values in an image, a vocabulary of 50,000 words, or millions of user preferences, the choice of data structure determines how fast your AI can think.

Two structures dominate: arrays and hash maps. Master these, and you've got the foundation for nearly every AI pipeline.

Arrays - Ordered Lists with Instant Access

An array is simply a numbered list of items stored side by side in memory. Each item has an index - its position in the list, starting from zero.

index:   0       1       2       3       4
value: ["cat", "dog", "bird", "fish", "frog"]

Because items sit next to each other, you can jump straight to any position. Want item 3? Done - no searching required. That's O(1) access, meaning it takes the same time whether the array holds 10 items or 10 million.

An array of five elements with indices 0 through 4, showing direct access to index 3
Arrays let you jump directly to any position using its index.

When Arrays Shine

  • Feature vectors: An image might be represented as an array of 784 numbers (28ร—28 pixels), each holding a brightness value.
  • Embeddings: Language models store word meanings as arrays of hundreds of floating-point numbers.
  • Batch processing: Training data is loaded into arrays so the GPU can process thousands of examples simultaneously.

The Catch

Inserting or removing items in the middle is expensive. Every item after the change must shuffle along. That's O(n) - the more items, the longer it takes.

๐Ÿค”
Think about it:

If you had a playlist of 10,000 songs and wanted to insert a new track at position 5, every song from position 5 onwards would need to shift. How might a streaming service handle this without slowing down?

Hash Maps - Instant Lookups by Name

A hash map (also called a dictionary or hash table) stores data as key-value pairs. Instead of accessing items by index number, you access them by a meaningful key.

word_counts = {
  "hello": 42,
  "world": 37,
  "AI": 156
}
Lesson 1 of 100% complete
โ†Back to program

Discussion

Sign in to join the discussion

Need the count for "AI"? The hash map uses a hash function to convert the key into an index behind the scenes. The result? O(1) average lookup time - just like arrays, but using names instead of numbers.

๐Ÿคฏ

Python's dictionaries are hash maps. When ChatGPT counts word frequencies during training, it uses hash-map-like structures to track billions of word occurrences across the entire internet's text.

Real AI Uses for Hash Maps

  • Vocabulary mapping: Turning words like "brilliant" into token ID 8921 for a language model.
  • Frequency counting: How often does each word appear in a dataset? Hash maps answer this in one pass.
  • Caching results: If an AI has already computed a prediction for input X, store it so you never recompute.

Time Complexity Made Simple

| Operation | Array | Hash Map | |-----------|-------|----------| | Access by index | O(1) โšก | N/A | | Access by key | O(n) ๐Ÿข | O(1) โšก | | Insert at end | O(1) โšก | O(1) โšก | | Insert in middle | O(n) ๐Ÿข | N/A | | Search for value | O(n) ๐Ÿข | O(1) โšก |

Think of O(1) as "instant, no matter the size" and O(n) as "slower the bigger the data."

๐Ÿง Quick Check

You have 100,000 user profiles and need to find a user by their username. Which structure is fastest?

Common Pattern: Frequency Counting

One of the most useful patterns in both interviews and AI is counting occurrences. Here's the idea in pseudocode:

counts = {}
for each word in text:
    if word in counts:
        counts[word] = counts[word] + 1
    else:
        counts[word] = 1

This single pass through the text gives you every word's frequency. Language models use exactly this approach (at massive scale) to understand which words matter most.

Common Pattern: The Two-Sum Problem

Given an array of numbers and a target, find two numbers that add up to the target. The naive approach checks every pair - O(nยฒ). The clever approach uses a hash map:

seen = {}
for each number in array:
    complement = target - number
    if complement in seen:
        return [seen[complement], current_index]
    seen[number] = current_index

One pass, O(n) time. The hash map remembers what you've already seen.

๐Ÿง Quick Check

Why is the hash map approach to two-sum faster than checking every pair?

When to Choose Which

  • Use arrays when order matters, you access items by position, or you need to process everything sequentially (like pixels in an image).
  • Use hash maps when you need fast lookups by a key, want to count occurrences, or need to check if something exists quickly.
๐Ÿค”
Think about it:

A recommendation engine needs to check whether a user has already watched a film before suggesting it. Would you store the user's watch history in an array or a hash map? What trade-offs come to mind?

๐Ÿง Quick Check

An AI model stores word embeddings as arrays of 300 numbers each. Why are arrays a good choice here?

Key Takeaways

  • Arrays give O(1) access by index and are the backbone of numerical AI (images, embeddings, tensors).
  • Hash maps give O(1) access by key and are essential for lookups, counting, and caching.
  • Choosing the right structure can turn an O(nยฒ) algorithm into an O(n) one - a difference that matters when processing millions of data points.
  • In practice, most AI pipelines use both: arrays for numerical computation and hash maps for metadata lookups.