exposedtalent / problem-solving-patterns

Different coding problem patterns implemented in Python. A brief overview of common data structures and algorithms.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Popular Coding Problem Solving Patterns

Data Structures Overview

Array

Array

  • A list of similar values
  • Can be used to store anything
  • Stores values of the same data type
  • Every item in an array is called an element
  • 3 attributes:
    • Name
    • Type
    • Size
      • set integer that is fixed upon the creation of an array
      • CAN'T be changed once created
  • Parallel arrays
    • 2 or more arrays which
      • contain the same # of elements
      • have corresponding values in the same position
  • Creating an array
    • populate with elements
    • set a specific size
    • Python - array = [1, 2, 3]
  • To get information, we use a numerical index
    • An integer which corresponds to an element within the array
  • A two-dimensional array is an array with an array at each index
Stack

Stack

  • A data structure in which we add elements and remove elements according to the LIFO (last in first out) principle
    • only one way in and one way out for the data
  • Common Stack Methods
    • push - adds an object into the top of the stack (Push(Object))
    • pop - remove an element from the top of the stack (Pop())
    • peek - allows you to get the value at the top of the list without removing it
    • contains - searching throughout the stack (Contains(Object)) - w/o having to take elements off the stack
Queue

Queue

  • The sequential access data structure which follows the FIFO methodology (First In First Out)
  • Add to the back and remove from the front
  • Queue methods
    • Enqueue - Adds element to the tail of the queue
    • Dequeue - Removes element from the head of the queue
    • Peek - returns the object that's at the forefront of the queue without removing it
    • Contains - returns whether or not the queue contains an object (boolean)

Sliding Window

S.No. Topic: Problem Solutions Python C++ Java
1 Sliding Window Minimum Size Subarray Sum ✔️
2 Sliding Window Find the maximum and minimum element in an array ✔️
3 Sliding Window Find the "Kth" max and min element of an array ✔️
4 Sliding Window Given an array which consists of only 0, 1 and 2. Sort the array without using any sorting algo ✔️
5 Sliding Window Move all the negative elements to one side of the array ✔️
6 Sliding Window Find the Union and Intersection of the two sorted arrays. ✔️
7 Sliding Window Write a program to cyclically rotate an array by one. ✔️
8 Sliding Window find Largest sum contiguous Subarray [V. IMP] ✔️
9 Sliding Window Minimise the maximum difference between heights [V.IMP] ✔️
10 Sliding Window Minimum no. of Jumps to reach end of an array ✔️


Two Pointers

S.No. Topic: Problem Solutions Python C++ Java
1 Matrix Spiral traversal on a Matrix ✔️
2 Matrix Search an element in a matriix ✔️
3 Matrix Find median in a row wise sorted matrix ✔️
4 Matrix Find row with maximum no. of 1's ✔️
5 Matrix Print elements in sorted order using row-column wise sorted matrix ✔️
6 Matrix Maximum size rectangle ✔️
7 Matrix Find a specific pair in matrix ✔️
8 Matrix Rotate matrix by 90 degrees ✔️
9 Matrix Kth smallest element in a row-cpumn wise sorted matrix ✔️
10 Matrix Common elements in all rows of a given matrix ✔️


Fast & Slow Pointers

S.No. Topic: Problem Solutions Python C++ Java
1 String Reverse a String ✔️
2 String Check whether a String is Palindrome or not ✔️
3 String Find Duplicate characters in a string ✔️
4 String Why strings are immutable in Java?
5 String Write a Code to check whether one string is a rotation of another ✔️
6 String Write a Program to check whether a string is a valid shuffle of two strings or not ✔️
7 String Count and Say problem
8 String Write a program to find the longest Palindrome in a string.[ Longest palindromic Substring]
9 String Find Longest Recurring Subsequence in String
10 String Print all Subsequences of a string.


Merge Intervals

S.No. Topic: Problem Solutions Python C++ Java
1 Searching & Sorting Find first and last positions of an element in a sorted array ✔️
2 Searching & Sorting Find a Fixed Point (Value equal to index) in a given array ✔️
3 Searching & Sorting Search in a rotated sorted array ✔️
4 Searching & Sorting square root of an integer ✔️
5 Searching & Sorting Maximum and minimum of an array using minimum number of comparisons ✔️
6 Searching & Sorting Optimum location of point to minimize total distance


Cyclic Sort

S.No. Topic: Problem Solutions Python C++ Java
1 LinkedList Write a Program to reverse the Linked List. (Both Iterative and recursive)
2 LinkedList Reverse a Linked List in group of Given Size. [Very Imp]
3 LinkedList Write a program to Detect loop in a linked list.
4 LinkedList Write a program to Delete loop in a linked list.
5 LinkedList Find the starting point of the loop.


In-place Reversal of a LinkedList

S.No. Topic: Problem Solutions Python C++ Java
1 Binary Trees level order traversal
2 Binary Trees Reverse Level Order traversal
3 Binary Trees Height of a tree
4 Binary Trees Diameter of a tree
5 Binary Trees Mirror of a tree
6 Binary Trees Inorder Traversal of a tree both using recursion and Iteration


Tree Breadth First Search

S.No. Topic: Problem Solutions Python C++ Java
1 Binary Search Trees Fina a value in a BST
2 Binary Search Trees Deletion of a node in a BST
3 Binary Search Trees Find min and max value in a BST
4 Binary Search Trees Find inorder successor and inorder predecessor in a BST
5 Binary Search Trees Check if a tree is a BST or not
6 Binary Search Trees Populate Inorder successor of all nodes
7 Binary Search Trees Find LCA of 2 nodes in a BST
8 Binary Search Trees Construct BST from preorder traversal
9 Binary Search Trees Convert Binary tree into BST
10 Binary Search Trees Convert a normal BST into a Balanced BST
11 Binary Search Trees Merge two BST [ V.V.V>IMP ]
12 Binary Search Trees Find Kth largest element in a BST
13 Binary Search Trees Find Kth smallest element in a BST
14 Binary Search Trees Count pairs from 2 BST whose sum is equal to given value "X"
15 Binary Search Trees Find the median of BST in O(n) time and O(1) space
16 Binary Search Trees Count BST ndoes that lie in a given range
17 Binary Search Trees Replace every element with the least greater element on its right


Tree Depth First Search

S.No. Topic: Problem Solutions Python C++ Java
1 Greedy Activity Selection Problem
2 Greedy Job SequencingProblem
3 Greedy Huffman Coding
4 Greedy Water Connection Problem
5 Greedy Fractional Knapsack Problem
6 Greedy Greedy Algorithm to find Minimum number of Coins
7 Greedy Maximum trains for which stoppage can be provided
8 Greedy Minimum Platforms Problem
9 Greedy Buy Maximum Stocks if i stocks can be bought on i-th day
10 Greedy Find the minimum and maximum amount to buy all N candies
11 Greedy Minimize Cash Flow among a given set of friends who have borrowed money from each other
12 Greedy Minimum Cost to cut a board into squares
13 Greedy Check if it is possible to survive on Island
14 Greedy Find maximum meetings in one room
15 Greedy Maximum product subset of an array
16 Greedy Maximize array sum after K negations
17 Greedy Maximize the sum of arr[i]*i
18 Greedy Maximum sum of absolute difference of an array
19 Greedy Maximize sum of consecutive differences in a circular array
20 Greedy Minimum sum of absolute difference of pairs of two arrays
21 Greedy Program for Shortest Job First (or SJF) CPU Scheduling
22 Greedy Program for Least Recently Used (LRU) Page Replacement algorithm
23 Greedy Smallest subset with sum greater than all other elements
24 Greedy Chocolate Distribution Problem
25 Greedy DEFKIN -Defense of a Kingdom
26 Greedy DIEHARD -DIE HARD
27 Greedy GERGOVIA -Wine trading in Gergovia
28 Greedy Picking Up Chicks
29 Greedy CHOCOLA –Chocolate
30 Greedy ARRANGE -Arranging Amplifiers


Two Heaps

S.No. Topic: Problem Solutions Python C++ Java
1 BackTracking Rat in a maze Problem
2 BackTracking Printing all solutions in N-Queen Problem
3 BackTracking Word Break Problem using Backtracking


Subsets

S.No. Topic: Problem Solutions Python C++ Java
1 Stacks & Queues Implement Stack from Scratch
2 Stacks & Queues Implement Queue from Scratch
3 Stacks & Queues Implement 2 stack in an array
4 Stacks & Queues find the middle element of a stack
5 Stacks & Queues Implement "N" stacks in an Array
6 Stacks & Queues Check the expression has valid or Balanced parenthesis or not.


Modified Binary Search

S.No. Topic: Problem Solutions Python C++ Java
1 Heap Implement a Maxheap/MinHeap using arrays and recursion.
2 Heap Sort an Array using heap. (HeapSort)
3 Heap Maximum of all subarrays of size k.
4 Heap “k” largest element in an array
5 Heap Kth smallest and largest element in an unsorted array
6 Heap Merge “K” sorted arrays. [ IMP ]
7 Heap Merge 2 Binary Max Heaps


Bitwise XOR

S.No. Topic: Problem Solutions Python C++ Java
1 Graph Create a Graph, print it
2 Graph Implement BFS algorithm
3 Graph Implement DFS Algo


Top K Elements

S.No. Topic: Problem Solutions Python C++ Java
1 Trie Construct a trie from scratch
2 Trie Find shortest unique prefix for every word in a given list
3 Trie Word Break Problem | (Trie solution)
4 Trie Given a sequence of words, print all anagrams together
5 Trie Implement a Phone Directory
6 Trie Print unique rows in a given boolean matrix


K-Way Merge

S.No. Topic: Problem Solutions Python C++ Java
1 Dynamic Programming Coin ChangeProblem
2 Dynamic Programming Knapsack Problem
3 Dynamic Programming Binomial CoefficientProblem
4 Dynamic Programming Permutation CoefficientProblem
5 Dynamic Programming Program for nth Catalan Number
6 Dynamic Programming Matrix Chain Multiplication
7 Dynamic Programming Edit Distance
8 Dynamic Programming Subset Sum Problem
9 Dynamic Programming Friends Pairing Problem
10 Dynamic Programming Gold Mine Problem
11 Dynamic Programming Assembly Line SchedulingProblem
12 Dynamic Programming Painting the Fenceproblem
13 Dynamic Programming Maximize The Cut Segments
14 Dynamic Programming Longest Common Subsequence
15 Dynamic Programming Longest Repeated Subsequence
16 Dynamic Programming Longest Increasing Subsequence
17 Dynamic Programming Space Optimized Solution of LCS
18 Dynamic Programming LCS (Longest Common Subsequence) of three strings
19 Dynamic Programming Maximum Sum Increasing Subsequence
20 Dynamic Programming Count all subsequences having product less than K
21 Dynamic Programming Longest subsequence such that difference between adjacent is one
22 Dynamic Programming Maximum subsequence sum such that no three are consecutive
23 Dynamic Programming Egg Dropping Problem
24 Dynamic Programming Maximum Length Chain of Pairs
25 Dynamic Programming Maximum size square sub-matrix with all 1s
26 Dynamic Programming Maximum sum of pairs with specific difference
27 Dynamic Programming Min Cost PathProblem
28 Dynamic Programming Maximum difference of zeros and ones in binary string
29 Dynamic Programming Minimum number of jumps to reach end
30 Dynamic Programming Minimum cost to fill given weight in a bag
31 Dynamic Programming Minimum removals from array to make max –min <= K
32 Dynamic Programming Longest Common Substring
33 Dynamic Programming Count number of ways to reacha given score in a game
34 Dynamic Programming Count Balanced Binary Trees of Height h
35 Dynamic Programming LargestSum Contiguous Subarray [V>V>V>V IMP ]
36 Dynamic Programming Smallest sum contiguous subarray
37 Dynamic Programming Unbounded Knapsack (Repetition of items allowed)
38 Dynamic Programming Word Break Problem
39 Dynamic Programming Largest Independent Set Problem
40 Dynamic Programming Partition problem
41 Dynamic Programming Longest Palindromic Subsequence
42 Dynamic Programming Count All Palindromic Subsequence in a given String
43 Dynamic Programming Longest Palindromic Substring
44 Dynamic Programming Longest alternating subsequence
45 Dynamic Programming Weighted Job Scheduling
46 Dynamic Programming Coin game winner where every player has three choices
47 Dynamic Programming Count Derangements (Permutation such that no element appears in its original position) [ IMPORTANT ]
48 Dynamic Programming Maximum profit by buying and selling a share at most twice [ IMP ]
49 Dynamic Programming Optimal Strategy for a Game
50 Dynamic Programming Optimal Binary Search Tree
51 Dynamic Programming Palindrome PartitioningProblem
52 Dynamic Programming Word Wrap Problem
53 Dynamic Programming Mobile Numeric Keypad Problem [ IMP ]
54 Dynamic Programming Boolean Parenthesization Problem
55 Dynamic Programming Largest rectangular sub-matrix whose sum is 0
56 Dynamic Programming Largest area rectangular sub-matrix with equal number of 1’s and 0’s [ IMP ]
57 Dynamic Programming Maximum sum rectangle in a 2D matrix
58 Dynamic Programming Maximum profit by buying and selling a share at most k times
59 Dynamic Programming Find if a string is interleaved of two other strings
60 Dynamic Programming Maximum Length of Pair Chain


Bit Manipulation

S.No. Topic: Problem Solutions Python C++ Java
1 Bit Manipulation Count set bits in an integer ✔️
2 Bit Manipulation Find the two non-repeating elements in an array of repeating elements ✔️
3 Bit Manipulation Count number of bits to be flipped to convert A to B ✔️
4 Bit Manipulation Count total set bits in all numbers from 1 to n ✔️

5 Bit Manipulation Program to find whether a no is power of two ✔️
6 Bit Manipulation Find position of the only set bit ✔️
7 Bit Manipulation Copy set bits in a range ✔️
8 Bit Manipulation Divide two integers without using multiplication, division and mod operator ✔️
9 Bit Manipulation Calculate square of a number without using *, / and pow() ✔️
10 Bit Manipulation Power Set ✔️


About

Different coding problem patterns implemented in Python. A brief overview of common data structures and algorithms.


Languages

Language:Python 100.0%