Part 3: Data Structures Fundamentals

Why These Data Structures Matter

In my early interviews, I struggled because I didn't deeply understand the data structures. I could explain what an array was, but I couldn't leverage its properties to solve problems efficiently. This part covers the fundamental data structures that appear in 70% of coding interviews.

Arrays and Strings

My Relationship with Arrays

Arrays seemed simple until I faced interview problems. The key insight I learned: arrays enable two powerful techniques—two pointers and sliding window.

Core Array Patterns

Pattern 1: Two Pointers (Opposite Directions)

Problem: Reverse a string in-place

def reverse_string(s: list[str]) -> None:
    """
    Reverse string in-place using two pointers
    
    My approach: Swap characters from both ends
    Time: O(n), Space: O(1)
    """
    left, right = 0, len(s) - 1
    
    while left < right:
        # Swap characters
        s[left], s[right] = s[right], s[left]
        left += 1
        right -= 1

# Test
chars = ['h', 'e', 'l', 'l', 'o']
reverse_string(chars)
print(chars)  # ['o', 'l', 'l', 'e', 'h']

Problem: Container With Most Water (Medium)

This problem taught me to think about what to optimize:

Pattern 2: Two Pointers (Same Direction)

Problem: Remove Duplicates from Sorted Array

Pattern 3: Sliding Window (Fixed Size)

Problem: Maximum Sum Subarray of Size K

Pattern 4: Sliding Window (Variable Size)

Problem: Longest Substring Without Repeating Characters

This was asked in 3 of my interviews:

String-Specific Problems

Problem: Valid Palindrome

Problem: Group Anagrams

Hash Tables (Dictionaries)

When I Reach for Hash Tables

Hash tables became my go-to for:

  • Counting frequencies

  • Detecting duplicates

  • Finding complements

  • Caching results

Core Hash Table Patterns

Pattern 1: Frequency Counter

Problem: First Unique Character

Pattern 2: Complement Finding

Problem: Two Sum (Revisited with Explanation)

Pattern 3: Detecting Duplicates

Problem: Contains Duplicate

Problem: Valid Anagram

Linked Lists

My Linked List Journey

Linked lists intimidated me initially. The key breakthrough: draw the pointers before writing code.

Linked List Node Definition

Core Linked List Patterns

Pattern 1: Two Pointers (Fast & Slow)

Problem: Middle of Linked List

Problem: Linked List Cycle

Pattern 2: Reverse Linked List

This appears in SO many variations:

Problem: Reverse Between Positions

Pattern 3: Merge Operations

Problem: Merge Two Sorted Lists

Stacks

When to Use Stacks

From my experience, stacks are perfect for:

  • Matching pairs (parentheses, brackets)

  • Tracking previous elements

  • Undo operations

  • Expression evaluation

Core Stack Problems

Problem: Valid Parentheses (Complete Solution)

Problem: Daily Temperatures

This problem taught me monotonic stacks:

Queues

Queue vs Stack

  • Stack: LIFO (Last In, First Out) - like plates

  • Queue: FIFO (First In, First Out) - like a line

Implementation in Python

Queue Problem Example

Problem: Moving Average from Data Stream

Key Takeaways

From solving 100+ data structure problems:

  1. Arrays: Master two pointers and sliding window

  2. Strings: Often similar to arrays, watch for edge cases

  3. Hash Tables: Default choice for O(1) lookup needs

  4. Linked Lists: Draw the pointers before coding

  5. Stacks: Perfect for matching pairs and previous elements

  6. Queues: FIFO processing and sliding windows

Practice Problems

Problems I recommend solving:

Arrays:

  • Best Time to Buy and Sell Stock

  • Product of Array Except Self

  • 3Sum

Strings:

  • Longest Palindromic Substring

  • Minimum Window Substring

  • Valid Anagram

Hash Tables:

  • Top K Frequent Elements

  • Subarray Sum Equals K

Linked Lists:

  • Remove Nth Node From End

  • Reorder List

  • Palindrome Linked List

Stacks/Queues:

  • Min Stack

  • Implement Queue using Stacks

  • Largest Rectangle in Histogram

What's Next?

In Part 4: Advanced Data Structures, we'll explore:

  • Binary trees and BST operations

  • Graph traversal (BFS, DFS)

  • Heaps and priority queues

  • Trie data structures

These appear in 30% of interviews and separate good candidates from great ones!


This part is based on my experience solving 200+ array, string, and linked list problems.

Last updated