View on GitHub

Coding Practice

Coding practice

Index

Table of Contents

Sorting

Insertion sort

Bubble sort

Merge sort

Code for merge sort

Quick sort

Code for quick sort

Data structures

Binary Heap (min-heap)

Insertion

Building

Deletion

Binary Search Tree

Insertion

Deletion

Trie

Graphs

Graph vs Tree

Mathematical concepts

Problems list

Divide and Conquer

  1. A very good problem: Longest Substring with At Least K Repeating Characters

Monotonic queue

  1. Shortest Unsorted Continuous Subarray
  2. Sliding Window Maximum
  3. Remove duplicate numbers Leetcode
  4. Largest Rectangle in a histogram - solution
  5. Remove k digits Leetcode

Binary search problems folder

  1. You have to find not the exact match, but the least absolute distance to the elements in the array. heaters problem
Binary search over solution space
  1. Split Array Largest Sum

Two pointers

Sometimes, you may have to sort the data when using this. Also this can be extended to three pointers and more.

  1. Three sum problem
  2. Next Permutation
  3. Longest substring without repeating characters: O(n) solution
  4. Minimum window substring
  5. Minimum size subarray sum This problem can be solved using cumulative sum method too. But it takes O(n^2) time.
  6. Longest Repeating Character Replacement. Also watch the YT video here.
  7. Longest substring with at least k repeating characters

Diff array technique

  1. Corporate flight bookings - solution

Dynamic Programming (DP)

  1. Maximal Square - Difficult to come up with the logic.
  2. Largest magic square
  3. Edit Distance
  4. Maximum Profit in Job Scheduling
  5. Palindrome partitioning
  6. Longest Valid Parentheses
  7. A little tough: Best time to buy and sell stock IV Leetcode
  8. Bursting balloons
  9. LIS variant: First sort the data based on one field, then find the LIS based on the second field. Russian doll envelopes
  10. LIS variant: first sort and then check LIS property based on whether one is divisible by another. Largest divisble subset
  11. Similar to knapsack: Partition Equal Subset Sum

Using original indexes array

  1. Couples holding hands - solution

Bit manipulation

This can be used in a lot of places. Some are:

  1. Find non-duplicate number without extra space

Hashing

  1. Group anagrams - solution
  2. Longest Consecutive Sequence

Sorting

  1. Group anagrams - solution
  2. My Calendar
  3. Sometimes numbers can be converted to strings and sorted. Here a custom comparator is used to figure which type of concatenation is better (a+b or b+a) - Largest Number Leetcode
  4. A variation of merge sort - Count of smaller numbers after self

Recursion

  1. House Robber Three
  2. Longest substring with at least k repeating characters

Binary Tree

  1. House Robber Three

Heap - Priority Queue

  1. Leetcode IPO
  2. Asked in an interview Maximum jobs in non-consecutive towns

Sliding window

  1. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold

No or constant extra space

See if you can manipulate the existing array/DS itself. For example, you can change the sign of the element etc to remember wehther visited or not.

  1. First Missing Positive

Graphs

  1. Bipartite graph
Union Find
  1. Asked in an interview: Group nodes into a network and find sum after each network merge
Topological sort
  1. Course schedule two
  2. Course Schedule
  3. Alien dictionary The key is to understand that a dictionary problem is nothing but topological sort.
Breadth First Search (BFS)
  1. Word ladder II
Depth First Search (DFS)
  1. Pacific Atlantic Water Flow