LeetCode Queue Design Patterns in C++: Put the Answer at an Edge LeetCode Queue Design Patterns in C++: Put the Answer at an Edge nycomdorics.com · preview of the WordPress post · source deck by Joseph Choi A binary heap will hand you its smallest element instantly and tell you nothing whatsoever about the second smallest. A std::vector will append and remove at the back in constant time and charge you a full shift to remove from the front. Every structure in this family is generous at its edges and useless in its interior. So the design job is always READ MORE
Tag: Algorithms
Decrease and Conquer in C++: Twelve LeetCode Problems That Throw the Search Space Away
Decrease and conquer on LeetCode is the habit of proving that a whole block of candidates cannot possibly hold the answer, and then deleting it without looking. Twelve problems here run on that one move: 167, 11, 42, 121, 53, 1014, 169, 240, 277, 280, plus two that are worth including precisely because one of them isn’t this pattern at all and the other should be. 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 twelve problems at a glance # Problem Difficulty Shape READ MORE
Prefix Sum in C++: Nine LeetCode Problems and One Equation
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
Cycle Sort in C++: Six LeetCode Array Problems That Are the Same Problem
Cycle sort on LeetCode is the pattern behind a group of array problems that look unrelated on the problem list and turn out to be the same eight lines of C++ with a different last paragraph. Six of them are below: 268, 448, 287, 645, 442, and 41. If you can write the skeleton from memory, you can solve all six, and the Hard one stops being a Hard. 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 six problems at a glance # 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
BFS vs DFS Explained: Graph Traversal Algorithms with Code Examples
Every graph algorithm starts with the same fundamental question: how do we visit every node? Answering that question efficiently is the job of the two core traversal algorithms — Breadth-First Search (BFS) and Depth-First Search (DFS). By the end of this post you’ll understand how both work, when to use each, and how DFS extends naturally into topological sort — one of the most useful tools in any developer’s toolkit. This is Part 2 of a 3-part series. Start with Part 1 if you haven’t yet. The Node Status Model Before writing any traversal code, it helps to think of READ MORE
Graph Data Structures Explained: A Beginner’s Guide to Nodes, Edges & Representations
If you’ve ever used Google Maps, browsed a social network, or watched a package get routed across the internet — you’ve already interacted with a graph without knowing it. Graphs are one of the most powerful and widely-used data structures in computer science, and understanding them will level up the way you think about problems. In this first post of a three-part series, we’ll cover everything you need to get started: what graphs are, the vocabulary you’ll need to know, and how to represent them in code. What Is a Graph? A graph is a collection of two things: That’s READ MORE