Overview
This page organizes all coding problems by their underlying data structure. Each problem includes multiple solution approaches with detailed complexity analysis.Source code location:
/workspace/source/data-structures/Arrays (7 Problems)
Arrays provide O(1) access but O(n) insertion and deletion. These problems explore various array manipulation techniques.Problem 1: Rotate Matrix
Problem 1: Rotate Matrix
Difficulty: MediumProblem Statement:
Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees clockwise.Example:Source:
- Solution 1: New Matrix
- Solution 2: In-Place
Approach: Create a new matrix and copy elements in rotated positions.Time Complexity: O(n²)Space Complexity: O(n²)Key Insight: For rotation, new[i][j] = old[n-1-j][i]
/workspace/source/data-structures/array/array.problems.test.ts:5-76Problem 2: Next Greater Element
Problem 2: Next Greater Element
Difficulty: MediumProblem Statement:
Given an array of integers with all distinct numbers, find the next greater element for each element. If no greater element exists to the right, return -1.Example:Source:
- Solution: Stack-Based
Approach: Use a stack to track elements awaiting their next greater element.Time Complexity: O(n)Space Complexity: O(n)Algorithm:
- Push first element to stack
- For each subsequent element:
- While stack is not empty and current > top: pair them
- Push current element
- Remaining stack elements pair with -1
/workspace/source/data-structures/array/array.problems.test.ts:78-109Problem 3: Merge Sorted Arrays
Problem 3: Merge Sorted Arrays
Difficulty: EasyProblem Statement:
Given two sorted integer arrays Source:
nums1 and nums2, merge them into nums1 in-place. nums1 has enough space (size m+n) to hold both arrays.Example:- Solution: Backward Merge
Approach: Compare elements from the end and place larger element at the back.Time Complexity: O(n)Space Complexity: O(1)Key Insight: Working backwards avoids overwriting unprocessed elements.
/workspace/source/data-structures/array/array.problems.test.ts:111-154Problem 4: Find Number of Pairs
Problem 4: Find Number of Pairs
Difficulty: MediumProblem Statement:
Given two arrays X[] and Y[], find the number of pairs such that x^y > y^x where x is from X[] and y is from Y[].Example:Source:
- Solution: Brute Force
Approach: Compare all possible pairs.Time Complexity: O(n × m)Space Complexity: O(1)
/workspace/source/data-structures/array/array.problems.test.ts:156-184Problem 5: Array Difference
Problem 5: Array Difference
Difficulty: EasyProblem Statement:
Write a function that accepts two arrays and returns a new array containing all values not present in both arrays (symmetric difference).Example:Source:
- Solution: Hash Map
Approach: Use hash maps to track elements from both arrays.Time Complexity: O(n)Space Complexity: O(n)
/workspace/source/data-structures/array/array.problems.test.ts:186-207Problem 6: Special Integer x
Problem 6: Special Integer x
Difficulty: MediumProblem Statement:
Find the special integer x such that there are exactly x integers in array k that are greater than or equal to x. Return -1 if no such integer exists.Example:Source:
- Solution: Count Down
Approach: Check each value from max down to 0.Time Complexity: O(m × n) where m is max valueSpace Complexity: O(n)
/workspace/source/data-structures/array/array.problems.test.ts:209-239Problem 7: Maximize Sum of Pair Minimums
Problem 7: Maximize Sum of Pair Minimums
Difficulty: MediumProblem Statement:
Given an array of N integers (N is even), group elements into pairs to maximize the sum of minimum values from each pair.Example:Source:
- Solution: Sort First
Approach: Sort array, then sum elements at even indices.Time Complexity: O(n log n)Space Complexity: O(1)Key Insight: After sorting, pair adjacent elements. The smaller of each pair is at even indices.
/workspace/source/data-structures/array/array.problems.test.ts:241-267Linked Lists (2 Problems)
Linked lists offer O(1) insertion/deletion but O(n) access. These problems explore pointer manipulation.Problem 1: Partition List
Problem 1: Partition List
Difficulty: MediumProblem Statement:
Partition a linked list around a value x such that all nodes less than x come before all nodes greater than or equal to x. The value x can appear anywhere in the right partition.Example:Source:
- Solution: In-Place Rearrangement
Approach: Move nodes less than x to the front while traversing.Time Complexity: O(n)Space Complexity: O(1)
/workspace/source/data-structures/lists/singly-linked-list/singly-linked-list.problems.test.ts:5-43Problem 2: Sum Lists
Problem 2: Sum Lists
Difficulty: MediumProblem Statement:
Given two numbers represented by linked lists where each node contains a single digit stored in reverse order, add them and return the sum as a linked list.Example:Source:
- Solution: Digit-by-Digit Addition
Approach: Traverse both lists, add corresponding digits, track carry.Time Complexity: O(max(n, m))Space Complexity: O(max(n, m))
/workspace/source/data-structures/lists/singly-linked-list/singly-linked-list.problems.test.ts:45-94Stacks (2 Problems)
Stacks use LIFO ordering and provide O(1) push/pop operations.Problem 1: Sort Stack
Problem 1: Sort Stack
Difficulty: MediumProblem Statement:
Sort a stack in ascending order (smallest items on top) using at most one additional stack. You may not copy elements into any other data structure.Example:Source:
- Solution: Auxiliary Stack
Approach: Use a second stack to temporarily hold sorted elements.Time Complexity: O(n²)Space Complexity: O(n)Algorithm:
- Pop element from original stack
- While aux stack top < element, move aux elements back to original
- Push element to aux stack
- Repeat until original is empty
- Move all back to original
/workspace/source/data-structures/stack/stack.problems.test.ts:5-38Problem 2: Base Converter
Problem 2: Base Converter
Difficulty: EasyProblem Statement:
Write a converter from decimal to bases between 2 and 36.Example:Source:
- Solution: Division Method
Approach: Repeatedly divide by base, push remainders to stack, then pop to build result.Time Complexity: O(log n)Space Complexity: O(log n)
/workspace/source/data-structures/stack/stack.problems.test.ts:40-69Trees (4 Problems)
Trees are hierarchical structures. These problems explore traversal, serialization, and level-based operations.Problem 1: Right View of Binary Tree
Problem 1: Right View of Binary Tree
Difficulty: MediumProblem Statement:
Given a binary tree, print the right view - the set of nodes visible when viewing the tree from the right side.Example:Source:
- Solution 1: DFS with Hash Map
- Solution 2: BFS
Approach: Use pre-order traversal, storing the last node at each level.Time Complexity: O(n)Space Complexity: O(h) where h is heightKey Insight: Since we traverse right subtree after left, the last value stored at each level is the rightmost.
/workspace/source/data-structures/trees/tree/tree.problems.test.ts:6-99Problem 2: Binary Tree Height
Problem 2: Binary Tree Height
Difficulty: EasyProblem Statement:
Calculate the height of a binary tree. Height of an empty tree is -1, height of a tree with one node is 0.Example:Source:
- Solution: Recursive DFS
Approach: Recursively find max depth of left and right subtrees.Time Complexity: O(n)Space Complexity: O(h) where h is height
/workspace/source/data-structures/trees/tree/tree.problems.test.ts:101-142Problem 3: Connect Nodes at Same Level
Problem 3: Connect Nodes at Same Level
Difficulty: MediumProblem Statement:
Connect all adjacent nodes at the same level in a binary tree using a Source:
nextRight pointer.Example:- Solution: Level Tracking
Approach: Use a hash map to track the last node at each level.Time Complexity: O(n)Space Complexity: O(h)
/workspace/source/data-structures/trees/tree/tree.problems.test.ts:144-213Problem 4: Serialize and Deserialize Binary Tree
Problem 4: Serialize and Deserialize Binary Tree
Difficulty: HardProblem Statement:
Implement functions to serialize a binary tree into a string and deserialize the string back into the tree.Example:Source:
- Solution: Pre-order Traversal
Approach: Use pre-order traversal with ’#’ for null nodes.Time Complexity: O(n) for both operationsSpace Complexity: O(n)
/workspace/source/data-structures/trees/tree/tree.problems.test.ts:215-270Queues
No queue-specific problems are currently implemented in the repository. However, queues are used extensively in BFS algorithms (see Tree Problem 1) and form the basis for many scheduling and traversal problems.
/workspace/source/data-structures/queue/queue.problems.test.ts
Graphs
No graph-specific problems are currently implemented in the repository. However, the graph data structure is fully implemented and can be used for problems like DFS, BFS, shortest path, and cycle detection.
/workspace/source/data-structures/graph/graph.problems.test.ts
Quick Reference
Arrays
7 problems covering matrix operations, searching, and optimization
Linked Lists
2 problems on partitioning and arithmetic operations
Stacks
2 problems demonstrating LIFO principles
Queues
No problems currently implemented - see Tree problems for queue usage in BFS
Trees
4 problems on traversal, serialization, and level operations
Graphs
No problems currently implemented - graph data structure available