Data Structures & Memory
Learn essential arrays, linked lists, stacks, queues, and binary search trees through interactive coding.
Reverse Singly Linked List In-Place
Given the `head` of a singly linked list, reverse the list in-place by mutating node pointers and return the new head pointer.
Min Stack with O(1) Auxiliary Retrieval
Design a stack structure supporting `push(val)`, `pop()`, `top()`, and `getMin()`, where all operations execute in constant O(1) time.
Queue Implementation via Two Stacks
Implement a first-in first-out (FIFO) queue using only two standard LIFO stacks. Support `push`, `pop`, `peek`, and `empty`.
Array Circular Right Shift In-Place
Given an integer array `nums`, rotate the array to the right by `k` steps in-place using constant auxiliary space.
Floyd's Cycle Detection & Loop Node Search
Given the `head` of a linked list, return the node where the cycle begins. If there is no cycle, return `null`.
Merge Two Sorted Linked Lists In-Place
You are given the heads of two sorted linked lists `list1` and `list2`. Merge the two lists into one sorted list by splicing together nodes in-place.
Remove Nth Node From End of Singly Linked List
Given the `head` of a linked list, remove the `n`-th node from the end of the list and return its head in a single pass.
Intersection Node of Two Linked Lists
Given the heads of two singly linked lists `headA` and `headB`, return the node at which the two lists intersect. If the two linked lists have no intersection, return `null`.
Palindrome Linked List Pointer Verification
Given the `head` of a singly linked list, return `true` if it is a palindrome or `false` otherwise. Solve in O(N) time and O(1) space.
LRU Cache O(1) Doubly-Linked List + Hash Map
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement `get(key)` and `put(key, value)` both running in average O(1) time complexity.
Fixed-Size Memory Pool Allocator
Implement a fixed-size memory pool allocator that manages a contiguous block of memory divided into equal-sized slots. Support `allocate()` which returns a pointer to a free slot in O(1), and `free(ptr)` which releases a slot back to the pool in O(1). Use a free-list embedded within the slots themselves to avoid auxiliary heap allocations.
Best Time to Buy and Sell Stock (Single Transaction)
You are given an array `prices` where `prices[i]` is the price of a given stock on the `i`-th day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return `0`.
Valid Parentheses Expression Parser
Given a string `s` containing just the characters `'('`, `')'`, `'{'`, `'}'`, `'['` and `']'`, determine if the input string is valid. An input string is valid if open brackets are closed by the same type of brackets, in the correct LIFO order.
Container With Most Water Dual Pointers
You are given an integer array `height` of length `n`. There are `n` vertical lines drawn such that the two endpoints of the `i`-th line are `(i, 0)` and `(i, height[i])`. Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store.
Three Sum Zero Triplet Search
Given an integer array `nums`, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j`, `i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`. Notice that the solution set must not contain duplicate triplets.
Climbing Stairs Dynamic Programming
You are climbing a staircase. It takes `n` steps to reach the top. Each time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?
Course Schedule Topological Cycle Check
There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [a, b]` indicates that you must take course `b` first if you want to take course `a`. Return `true` if you can finish all courses. Otherwise, return `false`.
Word Ladder Shortest Transformation BFS
A transformation sequence from word `beginWord` to word `endWord` using a dictionary `wordList` is a sequence of words `beginWord -> s1 -> s2 -> ... -> sk` such that every adjacent pair differs by exactly one letter, and `sk == endWord`. Return the number of words in the shortest transformation sequence from `beginWord` to `endWord`, or `0` if no such sequence exists.
Binary Tree Level-Order Traversal
Given the `root` of a binary tree, return the level order traversal of its nodes' values (i.e., from left to right, level by level).
Sliding Window Maximum Monotonic Deque
You are given an array of integers `nums`, there is a sliding window of size `k` which is moving from the very left of the array to the very right. You can only see the `k` numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.
Search Insert Position Binary Search
Given a sorted array of distinct integers `nums` and a target value `target`, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with `O(log N)` runtime complexity.
Best Time to Buy and Sell Stock Single Pass
You are given an array `prices` where `prices[i]` is the price of a given stock on the `i`-th day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return `0`.
Valid Parentheses Matching Stack
Given a string `s` containing just the characters `'('`, `')'`, `'{'`, `'}'`, `'['` and `']'`, determine if the input string is valid. An input string is valid if: 1. Open brackets must be closed by the same type of brackets. 2. Open brackets must be closed in the correct order. 3. Every close bracket has a corresponding open bracket of the same type.
Container With Most Water Two Pointers
You are given an integer array `height` of length `n`. There are `n` vertical lines drawn such that the two endpoints of the `i`-th line are `(i, 0)` and `(i, height[i])`. Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store.
Three Sum Zero Triplet Search Two Pointers
Given an integer array `nums`, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j`, `i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`. Notice that the solution set must not contain duplicate triplets.
Climbing Stairs Dynamic Programming
You are climbing a staircase. It takes `n` steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Course Schedule Topological Graph Cycle
There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [a, b]` indicates that you must take course `b` first if you want to take course `a`. Return `true` if you can finish all courses. Otherwise, return `false`.
Word Ladder Shortest Transformation BFS
Given two words, `beginWord` and `endWord`, and a dictionary `wordList`, return the number of words in the shortest transformation sequence from `beginWord` to `endWord`, or `0` if no such sequence exists. Every adjacent pair of words must differ by exactly one letter.
Serialize and Deserialize Binary Tree Preorder BFS
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer. Design an algorithm to serialize a binary tree to a string and deserialize a string back to a binary tree.
Sliding Window Maximum Monotonic Deque
You are given an array of integers `nums`, there is a sliding window of size `k` which is moving from the very left of the array to the very right. You can only see the `k` numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.
Target Value Search & Index Bounds (Variant 4)
Given a sorted array of N numbers, find target value index using binary boundary checks. [Rigor Variation 4: Enforces strict boundary checks and memory limits].
Maximum Profit Stock Trading Window (Variant 4)
Calculate maximum profit achievable from single buy/sell transaction sequence over N days. [Rigor Variation 4: Enforces strict boundary checks and memory limits].
Valid Parentheses Expression Parser (Variant 4)
Determine if input string containing brackets '()[]{}' is valid using a LIFO stack. [Rigor Variation 4: Enforces strict boundary checks and memory limits].
Container With Most Water Dual Pointers (Variant 4)
Find two lines that together with x-axis form a container holding the most water. [Rigor Variation 4: Enforces strict boundary checks and memory limits].
Three Sum Zero Triplet Search (Variant 4)
Find all unique triplets in array [a,b,c] such that a + b + c = 0. [Rigor Variation 4: Enforces strict boundary checks and memory limits].
Climbing Stairs Dynamic Programming (Variant 4)
Count distinct ways to climb N stairs taking 1 or 2 steps per turn. [Rigor Variation 4: Enforces strict boundary checks and memory limits].
Course Schedule Topological Cycle Check (Variant 4)
Determine if you can finish all V courses given prerequisite pair requirements. [Rigor Variation 4: Enforces strict boundary checks and memory limits].
Word Ladder Shortest Transformation BFS (Variant 4)
Find shortest transformation sequence length from startWord to endWord using wordList. [Rigor Variation 4: Enforces strict boundary checks and memory limits].
Serialize and Deserialize Binary Tree (Variant 4)
Design an algorithm to serialize a binary tree to string and deserialize back to tree. [Rigor Variation 4: Enforces strict boundary checks and memory limits].
Sliding Window Maximum Deque Filter (Variant 4)
Find maximum element in sliding window of size K moving from left to right. [Rigor Variation 4: Enforces strict boundary checks and memory limits].
Target Value Search & Index Bounds (Variant 5)
Given a sorted array of N numbers, find target value index using binary boundary checks. [Rigor Variation 5: Enforces strict boundary checks and memory limits].
Maximum Profit Stock Trading Window (Variant 5)
Calculate maximum profit achievable from single buy/sell transaction sequence over N days. [Rigor Variation 5: Enforces strict boundary checks and memory limits].
Valid Parentheses Expression Parser (Variant 5)
Determine if input string containing brackets '()[]{}' is valid using a LIFO stack. [Rigor Variation 5: Enforces strict boundary checks and memory limits].
Container With Most Water Dual Pointers (Variant 5)
Find two lines that together with x-axis form a container holding the most water. [Rigor Variation 5: Enforces strict boundary checks and memory limits].
Three Sum Zero Triplet Search (Variant 5)
Find all unique triplets in array [a,b,c] such that a + b + c = 0. [Rigor Variation 5: Enforces strict boundary checks and memory limits].
Climbing Stairs Dynamic Programming (Variant 5)
Count distinct ways to climb N stairs taking 1 or 2 steps per turn. [Rigor Variation 5: Enforces strict boundary checks and memory limits].
Course Schedule Topological Cycle Check (Variant 5)
Determine if you can finish all V courses given prerequisite pair requirements. [Rigor Variation 5: Enforces strict boundary checks and memory limits].
Word Ladder Shortest Transformation BFS (Variant 5)
Find shortest transformation sequence length from startWord to endWord using wordList. [Rigor Variation 5: Enforces strict boundary checks and memory limits].
Serialize and Deserialize Binary Tree (Variant 5)
Design an algorithm to serialize a binary tree to string and deserialize back to tree. [Rigor Variation 5: Enforces strict boundary checks and memory limits].
Sliding Window Maximum Deque Filter (Variant 5)
Find maximum element in sliding window of size K moving from left to right. [Rigor Variation 5: Enforces strict boundary checks and memory limits].
Target Value Search & Index Bounds (Variant 6)
Given a sorted array of N numbers, find target value index using binary boundary checks. [Rigor Variation 6: Enforces strict boundary checks and memory limits].
Maximum Profit Stock Trading Window (Variant 6)
Calculate maximum profit achievable from single buy/sell transaction sequence over N days. [Rigor Variation 6: Enforces strict boundary checks and memory limits].
Valid Parentheses Expression Parser (Variant 6)
Determine if input string containing brackets '()[]{}' is valid using a LIFO stack. [Rigor Variation 6: Enforces strict boundary checks and memory limits].
Container With Most Water Dual Pointers (Variant 6)
Find two lines that together with x-axis form a container holding the most water. [Rigor Variation 6: Enforces strict boundary checks and memory limits].
Three Sum Zero Triplet Search (Variant 6)
Find all unique triplets in array [a,b,c] such that a + b + c = 0. [Rigor Variation 6: Enforces strict boundary checks and memory limits].
Climbing Stairs Dynamic Programming (Variant 6)
Count distinct ways to climb N stairs taking 1 or 2 steps per turn. [Rigor Variation 6: Enforces strict boundary checks and memory limits].
Course Schedule Topological Cycle Check (Variant 6)
Determine if you can finish all V courses given prerequisite pair requirements. [Rigor Variation 6: Enforces strict boundary checks and memory limits].
Word Ladder Shortest Transformation BFS (Variant 6)
Find shortest transformation sequence length from startWord to endWord using wordList. [Rigor Variation 6: Enforces strict boundary checks and memory limits].
Serialize and Deserialize Binary Tree (Variant 6)
Design an algorithm to serialize a binary tree to string and deserialize back to tree. [Rigor Variation 6: Enforces strict boundary checks and memory limits].
Sliding Window Maximum Deque Filter (Variant 6)
Find maximum element in sliding window of size K moving from left to right. [Rigor Variation 6: Enforces strict boundary checks and memory limits].
Target Value Search & Index Bounds (Variant 7)
Given a sorted array of N numbers, find target value index using binary boundary checks. [Rigor Variation 7: Enforces strict boundary checks and memory limits].
Maximum Profit Stock Trading Window (Variant 7)
Calculate maximum profit achievable from single buy/sell transaction sequence over N days. [Rigor Variation 7: Enforces strict boundary checks and memory limits].
Valid Parentheses Expression Parser (Variant 7)
Determine if input string containing brackets '()[]{}' is valid using a LIFO stack. [Rigor Variation 7: Enforces strict boundary checks and memory limits].
Container With Most Water Dual Pointers (Variant 7)
Find two lines that together with x-axis form a container holding the most water. [Rigor Variation 7: Enforces strict boundary checks and memory limits].
Three Sum Zero Triplet Search (Variant 7)
Find all unique triplets in array [a,b,c] such that a + b + c = 0. [Rigor Variation 7: Enforces strict boundary checks and memory limits].
Climbing Stairs Dynamic Programming (Variant 7)
Count distinct ways to climb N stairs taking 1 or 2 steps per turn. [Rigor Variation 7: Enforces strict boundary checks and memory limits].
Course Schedule Topological Cycle Check (Variant 7)
Determine if you can finish all V courses given prerequisite pair requirements. [Rigor Variation 7: Enforces strict boundary checks and memory limits].
Word Ladder Shortest Transformation BFS (Variant 7)
Find shortest transformation sequence length from startWord to endWord using wordList. [Rigor Variation 7: Enforces strict boundary checks and memory limits].
Serialize and Deserialize Binary Tree (Variant 7)
Design an algorithm to serialize a binary tree to string and deserialize back to tree. [Rigor Variation 7: Enforces strict boundary checks and memory limits].
Sliding Window Maximum Deque Filter (Variant 7)
Find maximum element in sliding window of size K moving from left to right. [Rigor Variation 7: Enforces strict boundary checks and memory limits].
Target Value Search & Index Bounds (Variant 8)
Given a sorted array of N numbers, find target value index using binary boundary checks. [Rigor Variation 8: Enforces strict boundary checks and memory limits].
Maximum Profit Stock Trading Window (Variant 8)
Calculate maximum profit achievable from single buy/sell transaction sequence over N days. [Rigor Variation 8: Enforces strict boundary checks and memory limits].
Valid Parentheses Expression Parser (Variant 8)
Determine if input string containing brackets '()[]{}' is valid using a LIFO stack. [Rigor Variation 8: Enforces strict boundary checks and memory limits].
Container With Most Water Dual Pointers (Variant 8)
Find two lines that together with x-axis form a container holding the most water. [Rigor Variation 8: Enforces strict boundary checks and memory limits].
Three Sum Zero Triplet Search (Variant 8)
Find all unique triplets in array [a,b,c] such that a + b + c = 0. [Rigor Variation 8: Enforces strict boundary checks and memory limits].
Climbing Stairs Dynamic Programming (Variant 8)
Count distinct ways to climb N stairs taking 1 or 2 steps per turn. [Rigor Variation 8: Enforces strict boundary checks and memory limits].
Course Schedule Topological Cycle Check (Variant 8)
Determine if you can finish all V courses given prerequisite pair requirements. [Rigor Variation 8: Enforces strict boundary checks and memory limits].
Word Ladder Shortest Transformation BFS (Variant 8)
Find shortest transformation sequence length from startWord to endWord using wordList. [Rigor Variation 8: Enforces strict boundary checks and memory limits].
Serialize and Deserialize Binary Tree (Variant 8)
Design an algorithm to serialize a binary tree to string and deserialize back to tree. [Rigor Variation 8: Enforces strict boundary checks and memory limits].
Sliding Window Maximum Deque Filter (Variant 8)
Find maximum element in sliding window of size K moving from left to right. [Rigor Variation 8: Enforces strict boundary checks and memory limits].
Target Value Search & Index Bounds (Variant 9)
Given a sorted array of N numbers, find target value index using binary boundary checks. [Rigor Variation 9: Enforces strict boundary checks and memory limits].
Maximum Profit Stock Trading Window (Variant 9)
Calculate maximum profit achievable from single buy/sell transaction sequence over N days. [Rigor Variation 9: Enforces strict boundary checks and memory limits].
Valid Parentheses Expression Parser (Variant 9)
Determine if input string containing brackets '()[]{}' is valid using a LIFO stack. [Rigor Variation 9: Enforces strict boundary checks and memory limits].
Container With Most Water Dual Pointers (Variant 9)
Find two lines that together with x-axis form a container holding the most water. [Rigor Variation 9: Enforces strict boundary checks and memory limits].
Three Sum Zero Triplet Search (Variant 9)
Find all unique triplets in array [a,b,c] such that a + b + c = 0. [Rigor Variation 9: Enforces strict boundary checks and memory limits].
Climbing Stairs Dynamic Programming (Variant 9)
Count distinct ways to climb N stairs taking 1 or 2 steps per turn. [Rigor Variation 9: Enforces strict boundary checks and memory limits].
Course Schedule Topological Cycle Check (Variant 9)
Determine if you can finish all V courses given prerequisite pair requirements. [Rigor Variation 9: Enforces strict boundary checks and memory limits].
Word Ladder Shortest Transformation BFS (Variant 9)
Find shortest transformation sequence length from startWord to endWord using wordList. [Rigor Variation 9: Enforces strict boundary checks and memory limits].
Serialize and Deserialize Binary Tree (Variant 9)
Design an algorithm to serialize a binary tree to string and deserialize back to tree. [Rigor Variation 9: Enforces strict boundary checks and memory limits].
Sliding Window Maximum Deque Filter (Variant 9)
Find maximum element in sliding window of size K moving from left to right. [Rigor Variation 9: Enforces strict boundary checks and memory limits].
Target Value Search & Index Bounds (Variant 10)
Given a sorted array of N numbers, find target value index using binary boundary checks. [Rigor Variation 10: Enforces strict boundary checks and memory limits].
Maximum Profit Stock Trading Window (Variant 10)
Calculate maximum profit achievable from single buy/sell transaction sequence over N days. [Rigor Variation 10: Enforces strict boundary checks and memory limits].
Valid Parentheses Expression Parser (Variant 10)
Determine if input string containing brackets '()[]{}' is valid using a LIFO stack. [Rigor Variation 10: Enforces strict boundary checks and memory limits].
Container With Most Water Dual Pointers (Variant 10)
Find two lines that together with x-axis form a container holding the most water. [Rigor Variation 10: Enforces strict boundary checks and memory limits].
Three Sum Zero Triplet Search (Variant 10)
Find all unique triplets in array [a,b,c] such that a + b + c = 0. [Rigor Variation 10: Enforces strict boundary checks and memory limits].
Climbing Stairs Dynamic Programming (Variant 10)
Count distinct ways to climb N stairs taking 1 or 2 steps per turn. [Rigor Variation 10: Enforces strict boundary checks and memory limits].
Course Schedule Topological Cycle Check (Variant 10)
Determine if you can finish all V courses given prerequisite pair requirements. [Rigor Variation 10: Enforces strict boundary checks and memory limits].
Word Ladder Shortest Transformation BFS (Variant 10)
Find shortest transformation sequence length from startWord to endWord using wordList. [Rigor Variation 10: Enforces strict boundary checks and memory limits].
Serialize and Deserialize Binary Tree (Variant 10)
Design an algorithm to serialize a binary tree to string and deserialize back to tree. [Rigor Variation 10: Enforces strict boundary checks and memory limits].
Sliding Window Maximum Deque Filter (Variant 10)
Find maximum element in sliding window of size K moving from left to right. [Rigor Variation 10: Enforces strict boundary checks and memory limits].