A collection of coding interview problems with my solutions. From various sources including Leet-Code, Interview experiences etc. List of questions at the end of this readme file. #100DaysOfCode
Other Resources:
- TopCoder Blog - GitHub Repositories With Competitive Programming Libraries [link]
- Problem Solving with Algorithms and Data Structures using Python [link]
- RosettaCode [link]
- Problem Solving with Algorithms and Data Structures using C++ [link]
- Algs4cs Princeton [link]
- Union Find - HackerEarth [link]
- Teach Yourself CS [link]
- LeetCode Patterns [link]
Arrays/Lists/Strings:
- Two pointers (left/right, slow/fast)
- Ordered Dictionaries in Python (LRU Cache, LFU Cache)
- Zero Sum Clique Reduction (Optimal Account Balancing, Minimum Cash Flow)
- Merge Intervals, Insert Interval (based on Sorting)
- Largest Number (Custom Comparator operator, Python's map function)
- Implement strStr() (Two pointers, Rabin Karp: Rolling Hash)
- Game of Life
Trie / Prefix Search
- Add and Search Word
Stacks & Queues:
- Design Snake Game (Queues)
- Remove K Digits (Stacks and Greedy algorithm)
- Simplify Path (Stacks)
- Decode String
- Moving Average from Data Stream (Queue)
Backtracking:
- Letter Combinations of a Phone Number
- Word Search
- Add and Search Word - Data structure design
- Permutations II
- Palindrome Partitioning
- Restore IP Addresses
Dynamic Programming
- Bomb Enemy
- Coin Change
Greedy Approach
- Wiggle Subsequence
- Remove K Digits
Ad Hoc:
- Fraction to Recurring Decimal
- Integer to English Word
- Spiral Matrix (yield keyword, generator perspective)
Graphs/Trees:
- DFS (Iterative and Recursive) / O(V+E)
- Clone Graph (recursive)
- Coin Change
- Detect Cycle in a directed graph / Course Schedule
- Distribute Coins in a Binary Tree
- Pacific Atlantic Water Flow
- Robot Room Cleaner
- BFS (Iterative and Recursive)
- Surrounded Regions
- Word Ladder (Bi-directional BFS)
- Optimal Account Balancing
- Clone Graph (iterative)
- Binary Tree Level Order Traversal
- Populating Next Right Pointers in Each Node
- Inorder Traversal (Iterative and Recursive)
- Preorder Traversal (Iterative and Recursive)
- Postorder Traversal (Iterative and Recursive)
- Kosaraju's Algorithm for Strongly Connected Components
- Union-Find Algorithm for Cycle Detection in Undirected Graphs
- Cycle detection in directed graphs using Colors / DFS stack
- Bridges in a graph
- Prim's Algorithm, Kruskal Algorithm: Minimum spanning tree
- Dijkstra's Algorithm - Shortest path from origin to every other node in graph / Greedy / O(ELogV) / only non-negative weights
- Bellman Ford Algorithm - Shortest path from origin to every other node in graph / DP / O(VE)
- Topological Sort for a directed graph
- Alien Dictionary
- Floyd Warshall Algorithm - All pairs shortest path
- Kirchhoff’s Theorem - Calculating number of Spanning trees Of a Graph
Python
- List Comprehensions (Bomb Enemy, Permutations II)
- zip(*(grid)) generator for transpose (Bomb Enemy, Alien Dictionary)
- Iterators / Ordered Dict (LRU Cache)
- Generator objects
- xor (^) (Adding Two Numbers, Fraction Recurring Decimal, Single Number_2)
- heapq package (Employee Free Time, Kth Largest Element)
- bisect package - helps with binary search (Reverse Pairs, Three Sum, Longest Increasing Subsequence, Find First and Last Position of Element in Sorted Array)
- reverse a list a[::-1]
- zip function (Compare Version Numbers)
- @lru_cache in functools (Minimum cost for tickets)
C++
- Shortforms [link]
- LeetCode (Medium) - Design Tic Tac Toe [link]
- LeetCode (Medium) - Longest substring without repeating characters [link]
- Codility (Hard) - Tree Trip [link]
- LeetCode (Medium) - Add Two Numbers [link]
- LeetCode (Hard) - Median of Two Sorted Arrays [link]
- LeetCode (Medium) - Three Sum [link]
- LeetCode(Medium) - Set Matrix Zeroes [link]
- LeetCode (Medium) - Group Anagrams [link]
- LeetCode (Medium) - Longest Palindromic Substring [link] (Manacher's Algorithm)
- LeetCode (Medium) - Increasing Triplet Subsequence [link]
- LeetCode (Medium) - Missing Ranges [link]
- LeetCode (Easy) - Reverse LinkedList [link]
- LeetCode (Hard) - Reverse LinkedList in k groups [link]
- LeetCode (Easy) - Sqrt [link]
- LeetCode (Easy) - Rectangle Overlap [link]
- LeetCode (Hard) - Interleaving String [link]
- LeetCode (Medium) - Merge Intervals [link]
- LeetCode (Medium) - Generate Parentheses [link]
- LeetCode (Hard) - LRU Cache [link]
- LeetCode (Medium) - Search in Rotated Sorted Array [link]
- LeetCode (Medium) - Rotate Image [link]
- LeetCode (Easy) - Maximum Subarray [link]
- LeetCode (Easy) - Two Sum [link]
- LeetCode (Medium) - Kth Largest Element in an Array [link]
- LeetCode (Medium) - UTF-8 Validation [link]
- LeetCode (Easy) - Count Primes [link]
- LeetCode (Easy) - Climbing Stairs [link]
- LeetCode (Hard) - Maximal Rectangle [link]
- LeetCode (Medium) - Product of Array Except Self [link]
- LeetCode (Easy) - Sum of Two Numbers [link]
- LeetCode (Medium) - Validate Binary Search Tree [link]
- LeetCode (Easy) - Add Strings [link]
- LeetCode (Hard) - Trapping Rain Water [link]
- LeetCode (Medium) - Number of Islands [link]
- LeetCode (Medium) - Reverse Words in a String [link]
- LeetCode (Medium) - Binary Tree Zigzag Level Order Traversal [link]
- LeetCode (Medium) - WordBreak [link]
- LeetCode (Hard) - WordBreak II [link]
- LeetCode (Hard) - Concatenated Words [link]
- LeetCode (Medium) - Partition Equal Subset Sum [link]
- LeetCode (Medium) - Partition to K Equal Sum Subset [link] #TODO
- LeetCode (Easy) - Reverse Bits [link]
- LeetCode (Medium) - Subsets [link]
- LeetCode (Easy) - Single Number [link]
- LeetCode (Medium) - Single Number II [link]
- LeetCode (Easy) - Move Zeroes [link]
- LeetCode (Easy) - Add Binary Numbers [link]
- LeetCode (Easy) - Intersection of Two Arrays II [link]
- LeetCode (Easy) - Valid Palindrome [link]
- LeetCode (Easy) - Valid Palindrome II [link]
- LeetCode (Medium) - ZigZag Conversion [link]
- LeetCode (Easy) - Palindrome Number [link]
- LeetCode (Hard) - Regular Expression Matching [link]
- LeetCode (Medium) - Container with Most Water [link]
- LeetCode (Easy) - Roman to Integer [link]
- LeetCode (Easy) - Longest Common Prefix [link]
- LeetCode (Medium) - Interval List Intersections [link]
- LeetCode (Easy) - Squares of a Sorted Array [link]
- LeetCode (Easy) - K Closest points to the origin [link]
- LeetCode (Medium) - Shortest Bridge [link]
- LeetCode (Medium) - Random Pick with Weight [link]
- LeetCode (Hard) - Consecutive Numbers Sum [link]
- LeetCode (Hard) - Bus Routes [link]
- LeetCode (Hard) - Reaching Points [link]
- LeetCode (Easy) - Construct Quad Tree [link]
- LeetCode (Hard) - Serialize and Deserialize N-ary Tree [link]
- LeetCode (Hard) - Employee Free Time [link]
- LeetCode (Hard) - Cherry Pickup [link]
- LeetCode (Medium) - Max Area of Island [link]
- LeetCode (Hard) - 24 Game [link]
- LeetCode (Medium) - Find K Closest Elements [link]
- LeetCode (Medium) - Print Binary Tree [link]
- LeetCode (Medium) - Exclusive Time of Functions [link]
- LeetCode (Medium) - Task Scheduler [link]
- LeetCode (Medium) - 01 Matrix [link]
- LeetCode (Hard) - Expression Add Operators [link]
- LeetCode (Medium) - The Maze [link]
- LeetCode (Medium) - Validate IP Addreess [link]
- LeetCode (Medium) - Sort Characters By Frequency [link]
- LeetCode (Easy) - Path Sum III [link]
- LeetCode (Medium) - Reconstruct Itineraries [link]
- LeetCode (Medium) - Flatten a Multilevel Doubly Linked List [link] (DFS)
- LeetCode (Hard) - Reverse Pairs [link] (Fenwick Trees / BIT Trees)
- LeetCode (Hard) - Optimal Account Balancing [link] (Settling Debts Paper/ 3-partition problem - Optimal, Greedy, Clique Reduction/BFS, Fastest)
- LeetCode (Hard) - LFU Cache [link] (OrderedDict used as a queue)
- LeetCode (Hard) - Basic Calculator III [link] (See discussion_basic_Calculator.pdf, python3 iter)
- LeetCode (Easy) - Logger Rate Limiter [link]
- LeetCode (Medium) - Encode and Decode TinyURL [link]
- LeetCode (Hard) - Number of Islands II [link]
- LeetCode (Easy) - Shortest Word Distance [link]
- LeetCode (Easy) - Intersection of Two Arrays [link]
- LeetCode (Medium) - Decode Ways [link]
- LeetCode (Hard) - Longest Consecutive Sequence [link] (UnionFind, Ad-hoc)
- LeetCode (Medium) - Remove Nth Node From End Of List [link] (Two Pointers)
- LeetCode (Hard) - Integer to English Words [link] (Divide and Conquer)
- LeetCode (Medium) - Fraction to Recurring Decimal [link]
- LeetCode (Medium) - Surrounded Regions [link] (BFS)
- LeetCode (Medium) - Word Ladder [link] (Bi-Directional BFS)
- LeetCode (Medium) - Spiral Matrix [link]
- LeetCode (Medium) - Clone Graph [link]
- LeetCode (Medium) - Design Snake Game [link]
- LeetCode (Medium) - Multiply Strings [link]
- LeetCode (Easy) - Linked List Cycle [link]
- LeetCode (Easy) - Excel Sheet Column Number [link]
- LeetCode (Medium) - Letter Combinations of a Phone Number [link] (Backtracking)
- LeetCode (Hard) - Insert Interval [link]
- LeetCode (Easy) - Jewels and Stones [link]
- LeetCode (Medium) - Friend Circles [link] (Union-Find)
- LeetCode (Medium) - Add and Search Word - Data structure design [link] (Trie)
- LeetCode (Medium) - Bomb Enemy [link] (Dynamic Programming)
- LeetCode (Medium) - Coin Change [link] (Dynamic Programming, DFS)
- LeetCode (Medium) - Longest Increasing Subsequence [link] (Dynamic Programming, Binary Search)
- LeetCode (Medium) - Wiggle Subsequence [link] (Greedy Algorithm)
- LeetCode (Medium) - Course Schedule [link] (Detect cycle in a directed graph)
- LeetCode (Medium) - Course Schedule II [link] (Topological Sort, Cycle Detection in 114.)
- LeetCode (Medium) - Word Search [link] (Backtracking)
- LeetCode (Medium) - Permutations II [link] (Backtracking)
- LeetCode (Medium) - Palindrome Partitioning [link] (Backtracking)
- LeetCode (Medium) - Compare Version Numbers [link] (String Manipulation)
- LeetCode (Medium) - Largest Number [link] (Sorting, String/Arrays, custom comparator op)
- LeetCode (Medium) - Nth Highest Salary [link] (SQL)
- LeetCode (Medium) - Remove K Digits [link] (Greedy Algorithm, Stacks, Monotonically Increasing Sequence)
- LeetCode (Medium) - Pow(x,n) [link] (Divide and Conquer-ish)
- LeetCode (Medium) - Simplify Path [link] (Stacks)
- LeetCode (Easy) - Implement strStr() [link] (String, Rabin Karp Algorithm)
- LeetCode (Medium) - Distribute Coins in Binary Tree [link] (Depth First Search)
- LeetCode (Medium) - Decode String [link] (String/Stacks)
- LeetCode (Medium) - Game Of Life [link] (Matrix/Game/Array)
- LeetCode (Hard) - Word Ladder II [link] (BFS, all shortest paths between two nodes in undirected graph)
- LeetCode (Hard) - Max Points on a Line [link] (Array iteration, defining a line, duplicates resolution, checking collinearity)
- LeetCode (Medium) - Copy List with Random Pointer [link] (Similar to Clone Graph problem)
- LeetCode (Medium) - Next Greater Element III [link]
- LeetCode (Medium) - Reorder List [link] (Suturing a LinkedList, Stack)
- LeetCode (Medium) - Restore IP Addresses [link] (Backtracking)
- LeetCode (Hard) - Sliding Window Maximum [link] (Dynamic Programming)
- LeetCode (Medium) - Partition List [link] (LinkedList)
- LeetCode (Medium) - Pacific Atlantic Water Flow [link] (DFS, Similar to Longest Increasing Path in a Matrix)
- LeetCode (Hard) - Longest Increasing Path in a Matrix [link] (DFS, DP/memoization)
- LeetCode (Easy) - Sum of Root To Leaf Binary Numbers [link] (DFS, Bit Manipulation)
- LeetCode (Medium) - Next Greater Node In Linked List [link] (Stack, Arrays)
- LeetCode (Hard) - Subarrays with K Different Integers [link] (Sliding Window)
- LeetCode (Easy) - Pairs of Songs With Total Durations Divisible by 60 [link] (Hash Table)
- LeetCode (Easy) - Find the Town Judge [link] (Sets)
- LeetCode (Easy) - Rotting Oranges [link] (BFS)
- LeetCode (Easy) - Cousins in Binary Tree [link] (BFS)
- LeetCode (Medium) - Vertical Order Traversal of a Binary Tree [link] (BFS, interesting use of lambda/defaultdict and reported order)
- LeetCode (Medium) - Minimum Cost For Tickets [link] (Dynamic Programming)
- LeetCode (Hard) - Unique Paths III [link] (DFS, Dynamic Programming/Bit Manipulation)
- LeetCode (Medium) - Longest Turbulent Subarray [link] (Arrays)
- LeetCode (Medium) - Prison Cells After N Days [link] (Bit Manipulation)
- LeetCode (Medium) - Inorder Successor of BST [link] (Inorder Traversal, BST, Stack)
- LeetCode (Medium) - Find First and Last Position of Element in Sorted Array [link] (Python's bisect package, Binary Search)
- LeetCode (Medium) - Minesweeper [link] (recursive DFS)
- LeetCode (Easy) - Count and Say [link] (Recursion)
- LeetCode (Easy) - Delete node in a linked list [link] (Trick on LL/ copy the next element to current and delete the next one)
- LeetCode (Easy) - Merge two sorted lists [link] (Recursion)
- LeetCode (Easy) - Palindrome Linked List [link] (O(1) space and O(n) time)
- LeetCode (Easy) - High Five [link] (Heap)
- LeetCode (Medium) - Max Increase to Keep City Skyline [link] (Smart Iteration)
- LeetCode (Hard) - Robot Room Cleaner [link] (Depth First Search)
- LeetCode (Medium) - Rotate List [link] (Linked List)
- LeetCode (Hard) - Number of Atoms [link] (Hash Table)
- LeetCode (Medium) - Reverse Words in a String II [link] (in-place transformation)
- LeetCode (Easy) - Valid Parentheses [link] (Stack)
- LeetCode (Medium) - Add Two Numbers II [link] (Stack, reverse linked list)
- LeetCode (Hard) - Merge k Sorted Lists [link] (Accumulate in list and sort instead of recursion)
- LeetCode (Easy) - Intersection of Two Linked Lists [link] (Stack, Two pointers)
- LeetCode (Medium) - Binary Tree Inorder Traversal [link] (Iterative/recursive inorder)
- LeetCode (Medium) - Binary Tree Level Order Traversal [link] (BFS)
- LeetCode (Medium) - Populating Next Right Pointers in Each Node [link] (BFS, perfect binary tree constraint)
- LeetCode (Medium) - Populating Next Right Pointers in Each Node II [link] (BFS)
- LeetCode (Easy) - Lowest Common Ancestor of a Binary Search Tree [link] (Recursion)
- LeetCode (Medium) - Lowest Common Ancestor of a Binary Tree [link] (Iterative post order traversal using stack)
- LeetCode (Medium) - Construct Binary Tree from Preorder and Inorder Traversal [link] (Recursion)
- LeetCode (Hard) - Alien Dictionary [link] (Topological Sort for directed graph)
- LeetCode (Medium) - Grumpy Bookstore Owner [link]