Prefix sum on LeetCode is one equation wearing nine costumes. Once you write down that the sum of a slice equals one running total minus another, every problem below turns into a question about what to store and what to look up. Nine of them are here: 1480, 303, 304, 560, 974, 325, 1524, 525, and 523. All the code here compiles and runs against the sample cases plus the edge cases listed at the bottom. More problems: the LeetCode series. The nine problems at a glance # Problem Difficulty Family 1480 Running Sum of 1d Array Easy Build the READ MORE
Category: C++
Sliding Window Pattern – Part 5: Variable-Size Windows
This is the biggest shift in the entire sliding window series. Every problem in Parts 1–4 had a fixed window size. You were always told: look at every subarray of size k. The window slid one step at a time — add one element, remove one element — like clockwork. The challenge was in what you tracked inside the window, not in the window itself. In this post, there is no k. The window size is not given. Instead, the window expands and contracts dynamically, and finding the right size is the core of the problem. How Variable-Size Windows Work READ MORE
Sliding Window Pattern – Part 4: When the Window Gets Smarter
In Parts 1–3, every sliding window problem followed a single template: you were given a window size k, you maintained a running sum or count, and you slid one step at a time. The only variation was the evaluation logic — track the max, count matches, or maintain a monotonic deque. The window itself was always dumb: it just held a number. Starting with this post, the pattern evolves. The state inside the window gets richer. Instead of a single integer (sum, count), the window now tracks a full frequency distribution or a sorted data structure. The window still slides READ MORE
Sliding Window Pattern – Part 3: The Monotonic Deque
In Part 1 and Part 2, every problem had a clean O(1) update per window slide — add an element, remove an element, done. That worked because we were tracking sums or counts, which are trivially updatable. But what if you need the maximum of each window? You can’t just subtract the outgoing element from “the max” — the max might not change, or it might have been the element you just removed. A naive approach would scan the entire window for the new max each time, giving O(nk). For large inputs, that’s too slow. This is where the monotonic READ MORE
Sliding Window Pattern – Part 2: Variations with Conditions
In Part 1, we covered the core fixed-size sliding window pattern: maintain a running sum, slide one element at a time, evaluate each window in O(1). The three problems we solved were almost identical in structure. Now things get more interesting. The four problems in this post still use a fixed-size window, but each one adds a twist to what you track or how you evaluate the window. The underlying mechanics remain the same — the challenge is in decomposing the problem to recognize where the sliding window fits. Problem 4: Diet Plan Performance (LC 1176) Difficulty: Easy Problem: A READ MORE
Sliding Window Pattern – Part 1: The Core Pattern and Basic Problems
If you’re preparing for coding interviews or just want to sharpen your algorithm skills, the sliding window technique is one of the most important patterns to master. It shows up everywhere — from easy warm-ups to hard interview favorites — and once you see the pattern, you’ll recognize it instantly. In this 3-part series, I’ll walk you through 8 LeetCode problems that all use sliding window, progressing from the simplest form to more advanced variations. By the end, you’ll have a clear mental model for when and how to apply this technique. What Is the Sliding Window Pattern? Imagine you READ MORE
Priority Queues & Binary Heaps Explained: How They Work Under the Hood
Imagine a hospital emergency room. Patients don’t get seen in the order they arrived — they get seen based on the severity of their condition. A patient with a heart attack jumps ahead of someone with a sprained ankle, regardless of who checked in first. That’s exactly the problem a priority queue solves in software. In this post, we’ll cover what priority queues are, how the binary heap makes them efficient, and how the core operations — insertion, extraction, and heapify — actually work. What Is a Priority Queue? A priority queue is a specialized version of a regular queue. READ MORE
Tree Data Structures Explained: Binary Trees, BSTs & Tree Traversals
Arrays and hash tables are great for storing flat collections of data. But what about data that’s naturally hierarchical — like a company org chart, a file system, or the rules in a decision-making process? That’s where trees come in. Trees are one of the most widely used data structures in computer science, and a solid understanding of them will help you with everything from database internals to coding interviews. In this post, we’ll cover what trees are, the most important types, and how to traverse them in four different ways. What Is a Tree? A tree is a hierarchical READ MORE
Hash Tables Explained: How Hashmaps Work Under the Hood
If you’ve ever used a dictionary in Python, an object in JavaScript, or an unordered_map in C++, you’ve already used a hash table. It’s one of the most important data structures in programming — and once you understand how it works internally, a lot of things in software development start to click. In this post, we’ll cover everything from the basics of key-value pairs to hash functions, collision handling, and the trade-offs behind different implementations. What Is a Hash Table? A hash table (also called a hashmap) is a data structure that stores key-value pairs and allows you to look READ MORE
Dijkstra’s Algorithm, Minimum Spanning Trees & Union Find Explained
In Part 1 we covered what graphs are and how to represent them. In Part 2 we explored BFS and DFS — the two ways to traverse a graph. Now it’s time for the advanced algorithms that solve real engineering problems: finding the shortest weighted path, connecting a network at minimum cost, and efficiently grouping nodes. This is Part 3 of a 3-part series. Start from Part 1 if you’re new to graphs. Dijkstra’s Algorithm — Shortest Path in a Weighted Graph The Problem BFS finds the shortest path in an unweighted graph (fewest edges). But what if edges have READ MORE