Skip to content
All work
Data Structures & AlgorithmsIn development · 2026

Data Structures & Algorithms

38 problems solved in Python and grouped by topic. Many are solved twice: first the simple brute-force way, then the optimal way, with the time and space cost noted beside each.

  • Python
  • Big-O analysis
  • LeetCode
  • GeeksforGeeks

Scope note — This is a learning repository, shown as a record of progress. It grows as I keep practising.

Overview

This repository is my record of learning data structures and algorithms. Each folder holds one problem. I usually write the simple solution first, then improve it, and note how fast each version is and how much memory it uses. Seeing both versions side by side shows exactly why the faster one is faster.

Problem

Knowing the name of a data structure is not the same as knowing when to use it. Interview questions and real code both need you to spot the pattern: this needs a hash map, this needs two pointers, this needs backtracking.

The only way to build that instinct is to solve many problems and compare the approaches.

Why it matters

The same patterns show up in production code. A hash map turns a slow nested loop into a single pass. Binary search finds one row in a million in about 20 steps instead of a million.

My system design work builds directly on these basics. The Bloom filter and cursor pagination in Scale Cart are the same ideas, applied at a larger scale.

Approach

Start with brute force. It is easy to get right, and it gives a correct answer to test the faster version against.

Find the waste. The slow version usually repeats work, such as checking every pair or searching the whole list again.

Remove the waste with the right tool: a hash map to remember what was already seen, two pointers to replace a nested loop, or binary search on sorted data.

Write down the cost. Each version gets a comment with its time complexity (how the run time grows with input size) and space complexity (how much extra memory it needs).

DSA concepts

Each concept below has a plain explanation and the problems in the repository that use it.

Arrays and two pointers

Two pointers
Two indexes move through the array together, replacing a nested loop with one pass. Used in 3Sum (LeetCode 15), Move Zeroes (283), reversing an array, and merging two sorted arrays without duplicates.
In-place rotation
Rotate an array by k places using three reversals, with no extra array. k is reduced with k % n first, so very large values of k still work.
Single-pass tracking
Keep a running best while scanning once: largest and second-largest element, max consecutive ones (485), and best time to buy and sell stock (121).
Kadane's algorithm
Maximum subarray sum (LeetCode 53) in O(n) time and O(1) space: keep a running total and reset it to zero whenever it goes negative.

Binary search

Classic binary search
Halve the search range at every step, so the cost is O(log n) instead of O(n). Used for search insert position (LeetCode 35).
Lower and upper bounds
Find the first and last position of a value, and the floor and ceiling of a target, in a sorted array.
Rotated sorted arrays
Search a rotated sorted array (LeetCode 33) and find its smallest element, by checking which half is still in order.

Hashing

Hash map lookups
Two Sum (LeetCode 1) in one pass: for each number, check whether its partner was already seen. O(n²) becomes O(n).
Frequency counting
Count how often each value appears in a dictionary to find the one number that appears only once.
From hash map to math
Missing number (LeetCode 268) solved three ways: a nested scan, then a hash map, then the formula n(n+1)/2 minus the sum, which needs no extra memory.

Matrices

Rotate image
Rotate a matrix 90 degrees in place (LeetCode 48): flip it along its diagonal, then reverse each row. No second matrix is needed.
Set matrix zeroes
Record which rows and columns contain a zero in a first pass, then clear them in a second pass (LeetCode 73).

Stacks

Min stack
Each entry stores its value and the smallest value so far, so reading the minimum is O(1).
Asteroid collision
A stack holds the asteroids moving right. Each one moving left collides with the top of the stack until one of them is destroyed.
Postfix and prefix evaluation
Evaluate expressions such as "2 3 4 * +" by pushing numbers onto a stack and applying each operator to the top two.
Next greater element
For each number, find the first larger number to its right.

Linked lists and heaps

Singly linked list
Nodes connected by "next" pointers, with insertion at any position, including at the head.
Heapify down
Restore the max-heap rule by swapping a node with its larger child until it settles. This O(log n) step powers heap sort and priority queues.

Recursion and backtracking

Subsequences
Generate every subset with a "take it or skip it" choice at each position, giving 2ⁿ results.
Pruning
Subsequences with a target sum stop exploring as soon as the running sum passes the target, which skips whole branches of work.
Valid brackets and binary strings
Build only valid strings: add a closing bracket only when it keeps the string balanced, and place a 1 only when the previous digit was 0.
Rat in a maze
Explore every open path to the exit. Mark each cell as visited on the way in, and unmark it on the way back so other paths can use it.
N-Queens
Place one queen per column. Three lookup arrays, one for rows and one for each diagonal, make each safety check O(1) instead of scanning the board.

Architecture

  1. Foundations

    Arrays

    Two pointers · rotation · Kadane

    Matrices

    Rotate · set zeroes

  2. Searching

    Binary search

    Bounds · floor · ceil

    Rotated arrays

    Search · minimum

  3. Lookup

    Hash maps

    Two Sum · frequency count

    Math shortcuts

    Missing number

  4. Structures

    Stack

    Min stack · postfix · collisions

    Linked list

    Insert at position

    Heap

    Heapify down

  5. Recursion

    Subsequences

    Take or skip

    Backtracking

    N-Queens · rat in a maze

How the repository is organised: from array basics, through searching and lookup, to data structures and recursion.

Implementation

Every problem lives in its own folder with a single Python file, grouped by topic: arrays, matrices, stacks, linked lists, heaps and recursion.

Where I improved a solution, both versions stay in the file, brute force first, so the change in complexity is easy to see.

Complexity is written as a comment beside the code, for example "TC is O(n) and SC is O(1)": time complexity and space complexity.

Engineering challenges

  • Seeing the pattern, not just the problem

    Two Sum and 3Sum look alike but need different tools: a hash map for one, sorting plus two pointers for the other. Solving them side by side made the difference clear.

  • Recursion that stays small

    Backtracking tries many paths. Stopping early when a path cannot work, and undoing each choice on the way back, keeps the search both small and correct.

  • Off-by-one errors in binary search

    Getting the low and high bounds slightly wrong is the most common binary search bug. Working through lower bound, upper bound, floor and ceiling built a template I can rely on.

Key decisions

Brute force first, every time
A slow but correct answer proves the problem is understood, and gives something to check the fast version against.
One problem per folder
Easy to find, easy to review, and progress is visible at a glance, topic by topic.
Python for practice
Its short syntax keeps the focus on the algorithm rather than on boilerplate.

Results

Problems solved
38
Topic areas
8
Language
Python

Technology

Language

  • Python

Practice sources

  • LeetCode
  • GeeksforGeeks

Method

  • Brute force first
  • Then optimal
  • Time and space complexity notes

Contact

Have an AI product or workflow worth building?

Send me the problem — not the spec. If it’s a fit I’ll tell you how I’d approach it; if it isn’t, I’ll say so.

Faisalabad, Pakistan · PKT (UTC+5) · Working remotely