In the last 15 years, I have interviewed 800+ Software Engineers across Google, Paytm, Amazon & various startups. Here are the most actionable tips I can give you on how to approach solving coding problems in Interviews (My DMs are always flooded with this particular question) 1. Use a Heap for K Elements - When finding the top K largest or smallest elements, heaps are your best tool. - They efficiently handle priority-based problems with O(log K) operations. - Example: Find the 3 largest numbers in an array. 2. Binary Search or Two Pointers for Sorted Inputs - Sorted arrays often point to Binary Search or Two Pointer techniques. - These methods drastically reduce time complexity to O(log n) or O(n). - Example: Find two numbers in a sorted array that add up to a target. 3. Backtracking - Use Backtracking to explore all combinations or permutations. - They’re great for generating subsets or solving puzzles. - Example: Generate all possible subsets of a given set. 4. BFS or DFS for Trees and Graphs - Trees and graphs are often solved using BFS for shortest paths or DFS for traversals. - BFS is best for level-order traversal, while DFS is useful for exploring paths. - Example: Find the shortest path in a graph. 5. Convert Recursion to Iteration with a Stack - Recursive algorithms can be converted to iterative ones using a stack. - This approach provides more control over memory and avoids stack overflow. - Example: Iterative in-order traversal of a binary tree. 6. Optimize Arrays with HashMaps or Sorting - Replace nested loops with HashMaps for O(n) solutions or sorting for O(n log n). - HashMaps are perfect for lookups, while sorting simplifies comparisons. - Example: Find duplicates in an array. 7. Use Dynamic Programming for Optimization Problems - DP breaks problems into smaller overlapping sub-problems for optimization. - It's often used for maximization, minimization, or counting paths. - Example: Solve the 0/1 knapsack problem. 8. HashMap or Trie for Common Substrings - Use HashMaps or Tries for substring searches and prefix matching. - They efficiently handle string patterns and reduce redundant checks. - Example: Find the longest common prefix among multiple strings. 9. Trie for String Search and Manipulation - Tries store strings in a tree-like structure, enabling fast lookups. - They’re ideal for autocomplete or spell-check features. - Example: Implement an autocomplete system. 10. Fast and Slow Pointers for Linked Lists - Use two pointers moving at different speeds to detect cycles or find midpoints. - This approach avoids extra memory usage and works in O(n) time. - Example: Detect if a linked list has a loop. 💡 Save this for your next interview prep!
Optimization Tricks for Competitive Programming
Explore top LinkedIn content from expert professionals.
-
-
There are over 3500+ questions on Leetcode and many people are not able to see how DSA patterns keep overlapping even after solving 200-300+ problems. With this pattern recognition cheatsheet you will be able to solve more than 80% of the problems: Think of it as: 1. Identify input type 2. Ask 2 to 3 questions 3. Land on a small set of likely approaches ● Arrays / Strings - Ask first: is it sorted or can I sort it cheaply. - Sorted → Binary Search, Two Pointers, Prefix Sums. - Not sorted → look at intent of the question. ➸ Optimization (max, min, count ways, min cost) - Choices depend on previous choices → Dynamic Programming. - Choices mostly independent with a clear local rule → Greedy, but be ready to justify. ➸Feasibility or existence questions - “Can we do this”, “Is there a solution” → Backtracking, DFS with pruning, or Binary Search on the answer. ➸ String heavy problems - Prefix or suffix logic → Trie, prefix function, rolling hash. - Complex edits, parentheses, next greater element → Stack or Monotonic Stack, sometimes Deque. ➸ Frequent lookups, duplicates, uniqueness - Use Hash Map, Hash Set, or counting array. ➸ Window that grows and shrinks - “Longest subarray”, “smallest substring that” → Sliding Window with Two Pointers, Deque, or a counting map. ➸ Frequent min or max with updates - Use Heap or Monotonic Queue. - Many range queries on big arrays → Segment Tree or Fenwick Tree. ● Graphs - First clarify: directed or undirected, weighted or unweighted, how input is given. ➸ Pathfinding and traversals - Shortest path or fewest steps, unweighted → BFS. - Weighted positive edges → Dijkstra. - Explore all nodes, count components, basic connectivity → DFS, BFS, or Union Find. ➸ Order and dependencies - Course schedule, build order, prerequisites → Topological sort with DFS or Kahn algorithm. - Cycle detection directed → DFS with visited and recursion stack. - Cycle detection undirected → DFS or Union Find. ➸ Global optimisation on graph - Connect all nodes with minimum cost → Minimum Spanning Tree, Kruskal or Prim. - Critical links, bridges, articulation points → Tarjan style algorithms. ● Trees (often binary trees) ➸ Traversals and depths - Level wise questions, distance from root, LCA via parent depth → BFS or DFS with depth. - Combine results from children, like height, path sum, balanced or not → Postorder DFS. ➸ Ordering or special properties - Binary Search Tree hints → Use BST property, inorder, or binary search style traversal. - Many range queries or updates on an array or tree like structure → Segment Tree or Fenwick Tree. ● Linked Lists ➸ Cycle detection - Use fast and slow pointers. ➸ Reversals and structural edits - Reverse list or sublist → Track prev, curr, next. - Insert or delete near head → Use a dummy node to simplify edge cases. ➸ Position based tasks - Find middle, kth from end, intersection → Fast and slow pointers, or length difference trick.
-
Cracking Leetcode problems fast during coding interviews = Knowing & recognizing the patterns. I struggled with coding interviews until I understood and implemented these 20 essential coding patterns. (Sample problems in comments) 1/ Sliding Window - Used when handling input data in a fixed window size. - Helps optimize problems that involve subarrays or substrings. - Avoids brute force by moving the window dynamically. 2/ Islands (Matrix Traversal) - Used for traversing 2D grids or matrices efficiently. - Commonly applies DFS or BFS for searching connected components. - Helps solve problems related to islands, clusters, or paths. 3/ Two Pointers - Uses two pointers to traverse data, often from opposite directions. - Helps optimize problems involving pairs, sorting, or merging. - Reduces time complexity compared to brute force. 4/ Fast & Slow Pointers - Also known as the Hare & Tortoise Algorithm. - Uses two pointers moving at different speeds to detect cycles. - Commonly used for linked lists and circular arrays. 5/ Merge Intervals - Deals with overlapping intervals. - Used for scheduling, reservations, and range merging. - Sorting intervals first usually helps in solving these problems. 6/ Cyclic Sort - Used when elements fall within a fixed range. - Helpful for finding missing or duplicate numbers in arrays. - Uses in-place sorting instead of extra space. 7/ In-Place Reversal of a LinkedList - Used to reverse the links between a set of nodes in-place. - Often needs to be done without using extra memory. - Works well for problems involving modifying linked list structure. 8/ Breadth-First Search (BFS) - Traverses trees or graphs level by level. - Used for finding shortest paths or exploring neighbors first. - Implemented using a queue. 9/ Depth-First Search (DFS) - Traverses deep into trees or graphs before backtracking. - Used for recursion-based traversal and path-related problems. - Implemented using stack or recursion. 10/ Two Heaps - Divides data into two halves: one for small elements, one for large ones. - Uses Min-Heap and Max-Heap for quick median or priority access. - Commonly used in streaming data or priority-based queries. 11/ Subsets - Used for problems requiring combinations or permutations. - Applies backtracking or BFS to generate all possible subsets. - Useful for problems involving decision trees. 12/ Modified Binary Search - Optimizes searching within a sorted dataset. - Used for problems involving rotated arrays, peak elements, or range queries. - Often leads to O(log N) complexity instead of brute force. 13/ Bitwise XOR - Uses the XOR operator for bit manipulation problems. - Great for finding missing or unique elements efficiently. - Works without extra space or sorting. Continued In Comments ↓
-
🚀 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗣𝗮𝘁𝘁𝗲𝗿𝗻𝘀 𝗜 𝗙𝗼𝗰𝘂𝘀 𝗼𝗻 𝗳𝗼𝗿 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄𝘀 Over time, I’ve noticed that most problems follow certain patterns. Identifying the right pattern early makes solving problems much easier. Here are the ones I focus on the most: ✅ 𝗜𝗳 𝘁𝗵𝗲 𝗶𝗻𝗽𝘂𝘁 𝗮𝗿𝗿𝗮𝘆 𝗶𝘀 𝘀𝗼𝗿𝘁𝗲𝗱, 𝘁𝗵𝗲𝗻: Binary search (O(log N) for efficient searching) Two pointers (Optimizing sum/pair problems) ✅ 𝗜𝗳 𝗮𝘀𝗸𝗲𝗱 𝗳𝗼𝗿 𝗮𝗹𝗹 𝗽𝗲𝗿𝗺𝘂𝘁𝗮𝘁𝗶𝗼𝗻𝘀/𝘀𝘂𝗯𝘀𝗲𝘁𝘀, 𝘁𝗵𝗲𝗻: Backtracking (Explore all possible solutions) ✅ 𝗜𝗳 𝗴𝗶𝘃𝗲𝗻 𝗮 𝘁𝗿𝗲𝗲, 𝘁𝗵𝗲𝗻: DFS (Depth-first for deep traversal) BFS (Level-order traversal) ✅ 𝗜𝗳 𝗴𝗶𝘃𝗲𝗻 𝗮 𝗴𝗿𝗮𝗽𝗵, 𝘁𝗵𝗲𝗻: DFS (Finding connected components, cycles) BFS (Shortest path, traversals) ✅ 𝗜𝗳 𝗴𝗶𝘃𝗲𝗻 𝗮 𝗹𝗶𝗻𝗸𝗲𝗱 𝗹𝗶𝘀𝘁, 𝘁𝗵𝗲𝗻: Two pointers (Fast & slow for cycle detection, middle node, etc.) ✅ 𝗜𝗳 𝗿𝗲𝗰𝘂𝗿𝘀𝗶𝗼𝗻 𝗶𝘀 𝗯𝗮𝗻𝗻𝗲𝗱, 𝘁𝗵𝗲𝗻: Stack (Simulate recursion iteratively) ✅ 𝗜𝗳 𝗺𝘂𝘀𝘁 𝘀𝗼𝗹𝘃𝗲 𝗶𝗻-𝗽𝗹𝗮𝗰𝗲, 𝘁𝗵𝗲𝗻: Swap corresponding values (For sorting or reordering) Store multiple values in the same pointer (Memory optimization) ✅ 𝗜𝗳 𝗮𝘀𝗸𝗲𝗱 𝗳𝗼𝗿 𝗺𝗮𝘅𝗶𝗺𝘂𝗺/𝗺𝗶𝗻𝗶𝗺𝘂𝗺 𝘀𝘂𝗯𝗮𝗿𝗿𝗮𝘆/𝘀𝘂𝗯𝘀𝗲𝘁/𝗼𝗽𝘁𝗶𝗼𝗻𝘀, 𝘁𝗵𝗲𝗻: Dynamic Programming (DP) (Break into overlapping subproblems) ✅ 𝗜𝗳 𝗮𝘀𝗸𝗲𝗱 𝗳𝗼𝗿 𝘁𝗼𝗽/𝗹𝗲𝗮𝘀𝘁 𝗞 𝗶𝘁𝗲𝗺𝘀, 𝘁𝗵𝗲𝗻: Heap (Efficient priority-based extraction) QuickSelect (Optimized selection of Kth smallest/largest) ✅ 𝗜𝗳 𝗮𝘀𝗸𝗲𝗱 𝗳𝗼𝗿 𝗰𝗼𝗺𝗺𝗼𝗻 𝘀𝘁𝗿𝗶𝗻𝗴𝘀, 𝘁𝗵𝗲𝗻: Map (HashMap) (Quick lookup & frequency counting) Trie (Efficient prefix-based searching) ✅ 𝗘𝗹𝘀𝗲: Map/Set (O(1) lookup, O(N) space) Sort input (O(N log N) time, O(1) space) These patterns help break down complex problems and quickly guide me toward an optimal solution. 🚀 💬 Which pattern do you find most useful in interviews? Let’s discuss! #LeetCode #DSA #JobSearch #CodingInterview #FAANG #MAANG #Amazon #Meta #Google #Microsoft #Netflix #Apple #Blind75 #Grind75 #TechInterview #SoftwareEngineering #Algorithms #DataStructures #InterviewPreparation #ProblemSolving #CrackingTheCodingInterview #Careers #SystemDesign #CompetitiveProgramming #FullStackDeveloper #DevCommunity #CodeNewbie #TechCareers #JobSearch #Python #Java #CodingLife #TechIndustry
-
During my Meta DSA rounds, I used arrays. During my Google DSA rounds, I used arrays. During my Amazon DSA rounds, I used arrays. At each company I interviewed, especially in the DSA rounds, I've used two pointers a lot. When you get used to it, you unlock solutions to dozens of common problems. Here's everything you need to know about two pointers: ↳ What It Is: Two pointers is a technique where you maintain two references to positions in a data structure, usually an array or string. You move these pointers based on specific conditions to solve the problem efficiently. It transforms O(n²) brute force solutions into O(n) linear time. ↳ When To Use It – You're working with sorted arrays or strings. – The problem asks about pairs, triplets, or subarrays. – You need to find elements that meet certain conditions. – You're dealing with palindromes or string reversal. – You need to remove duplicates or partition data. ↳ Core Patterns 1. Opposite Ends (Converging) Start one pointer at the beginning, one at the end. Move them toward each other based on conditions. Perfect for: Two Sum (sorted), container with most water, valid palindrome. Example: Finding pairs that sum to a target in a sorted array. 2. Same Direction (Fast and Slow) Both pointers start at the same position. One moves faster than the other. Perfect for: Removing duplicates, detecting cycles, finding middle element. Example: Remove duplicates from sorted array in place. 3. Sliding Window One pointer marks the window start, the other marks the end. Expand or contract the window based on conditions. Perfect for: Longest substring problems, maximum sum subarrays, minimum window. Example: Longest substring without repeating characters. 4. Partition (Dutch National Flag) Use multiple pointers to partition data into sections. Common in sorting and array rearrangement problems. Perfect for: Sort colors, move zeros, partition arrays. Example: Move all zeros to the end while maintaining order. ↳ Practice Problems (Easiest to Hardest) Beginner: - Valid Palindrome - Remove Duplicates from Sorted Array - Move Zeroes - Reverse String Intermediate: - Two Sum II (sorted array) - Container With Most Water - 3Sum - Sort Colors Advanced: - Trapping Rain Water - Minimum Window Substring - Longest Substring Without Repeating Characters - Subarray Product Less Than K
-
This article explores practical methods to speed up execution, including inline functions, loop unrolling, bit manipulation, DMA utilization, and data structure optimization. Real-world code examples accompany each technique to illustrate its impact.
-
I’ve sat down with more than 300+ candidates for technical interviews for SDE roles in the last 6 years of my career in different leadership positions. Here are the 10 best tips I can give you from my experience on how you should approach to solve a problem during an interview: 1. Use a Heap for K Elements - Rule: When dealing with top/maximum/minimum/closest K elements among N elements, use a Heap. - Example Scenario: Finding the top 3 largest numbers in an array. 2. Binary Search or Two Pointers for Sorted Inputs - Rule: If the input is a sorted array or list, use Binary Search or the Two Pointers strategy. - Example Scenario: Finding a pair of numbers that sum up to a target in a sorted array. 3. Backtracking or BFS for Combinations - Rule: For trying all combinations or permutations of the input, use Backtracking or Breadth-First Search (BFS). - Example Scenario: Generating all subsets of a given set. 4. BFS or DFS for Trees and Graphs - Rule: Most Tree or Graph questions can be solved using BFS or DFS. - Example Scenario: Finding the shortest path in a graph. 5. Convert Recursion to Iteration with a Stack - Rule: Every recursive solution can be rewritten as an iterative one using a Stack. - Example Scenario: Converting recursive tree traversal to iterative using a stack. 6. Optimize Array Problems with HashMap or Sorting - Rule: If the array solution takes O(n²) time, try using HashMap/Set for O(n) time or sorting for O(n log n) time. - Example Scenario: Finding duplicates in an array. 7. Use Dynamic Programming for Optimization Problems - Rule: If the problem involves maximization or minimization, use Dynamic Programming. - Example Scenario: Solving the knapsack problem. 8. HashMap or Trie for Common Substrings - Rule: For finding common substrings among multiple strings, use a HashMap or a Trie. - Example Scenario: Finding the longest common prefix among strings. 9. Trie for String Search and Manipulation - Rule: When searching or manipulating a collection of strings, a Trie is the best fit. - Example Scenario: Implementing autocomplete functionality. 10. Fast & Slow Pointers for Linked Lists - Rule: For Linked List problems, especially when extra space isn't allowed, use the Fast & Slow Pointer approach. - Example Scenario: Detecting a cycle in a linked list. ♻️ Repost if you found this helpful!
-
📚 𝐃𝐒𝐀 𝐅𝐨𝐫𝐦𝐮𝐥𝐚𝐬 𝐄𝐯𝐞𝐫𝐲 𝐃𝐞𝐯𝐞𝐥𝐨𝐩𝐞𝐫 𝐒𝐡𝐨𝐮𝐥𝐝 𝐇𝐚𝐯𝐞 𝐁𝐨𝐨𝐤𝐦𝐚𝐫𝐤𝐞𝐝 Whether you're prepping for interviews or just sharpening your fundamentals, having the core formulas in one place saves serious time. Sharing a quick cheat sheet covering: 🔹 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 — from O(1) to O(n!), with real examples (binary search, merge sort, recursive Fibonacci) so you actually remember *why* each complexity applies, not just the notation. 🔹 𝐀𝐫𝐢𝐭𝐡𝐦𝐞𝐭𝐢𝐜 𝐒𝐞𝐫𝐢𝐞𝐬 — sum of first n numbers, squares, and cubes. Simple formulas that show up constantly in optimization and competitive programming. 🔹 𝐁𝐢𝐧𝐚𝐫𝐲 𝐒𝐞𝐚𝐫𝐜𝐡 — the mid-point formula that prevents integer overflow, plus a full dry-run walkthrough on when (and when not) to use it. 🔹 𝐀𝐫𝐫𝐚𝐲𝐬 — subarray/pair counting, prefix sums, rotations, and the two-pointer technique that comes up in almost every array-based interview question. 💡 Key takeaway: mastering these isn't about memorization for its own sake — it's about pattern recognition. Once you internalize 'why' O(n log n) beats O(n²), or how prefix sums turn O(n) range queries into O(1), you start seeing these patterns everywhere in real problems. Saving this as a quick revision sheet before your next interview or contest. 🚀 #DSA #DataStructures #Algorithms #CodingInterview #SoftwareEngineering #TechCommunity #Programming #100DaysOfCode