python-algorithms icon indicating copy to clipboard operation
python-algorithms copied to clipboard

Data structures and interview questions implemented in Python with explanations and links to further readings

Python Algorithms and Data Structures

This repository contains implementations of popular data structures and interview questions implemented in Python.

Each data structure has its own separate README with related explanations and links for further reading (including ones to YouTube videos).

This project is my attempt to document the materials I have studied on my journey to understand data structures and also prepare for interviews.



Data Structures

A data structure is a particular way of organizing and storing data in a computer so that it can be accessed and modified efficiently. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.

The explanations of the data structures below are from Oleksii Trekhleb's Project

B - Beginner, A - Advanced

  • B Linked List
  • B Circular Linked List
  • B Doubly Linked List
  • B Circular Doubly Linked List
  • B Queue
  • B Priority Queue
  • B Stack
  • B Hash Table
  • A Heap
  • A Trie
  • A Tree
    • A Binary Tree
    • A Binary Search Tree
    • A AVL Tree
  • A Graphs


Patterns For Coding Interviews

This section categorizes coding interview problems into a set of 16 patterns. Under each pattern there will be a specific category of problems to solve. The goal is to develop an understanding of the underlying pattern, so that, we can apply that pattern to solve other problems.

  • Sliding Window
  • Two Pointers
  • Fast & Slow Pointers
  • Merge Intervals
  • Cyclic Sort
  • In-Place Reversal of Linkedlist
  • Breath First Search
  • Depth First Search
  • Two Heaps
  • Subsets
  • Modified Binary Search
  • Top K Elements
  • K Way Merge
  • Topological Sort
  • Dynamic Programming Patterns


Elements Of Programming Interviews Prep

EPI is an invaluable book textbook presents a comprehensive introduction to modern competitive programming.
Below are solutions & questions found under various topics in the book.

  • Recursion

    • E Greatest Common Divisor
    • M Towers Of Hanoi
    • M Permutations
    • M Power Set
    • M Subset Of Size
    • M Kth Node in Inorder Traversal
    • M Generate Palindromic Decompositions
    • M Generate Binary Trees
    • M Compute Right Sibling Tree
  • Binary Trees

    • E Binary Tree Traversal
    • E Is Binary Tree Height Balanced
    • E Is Binary Tree Symmetric
    • M Lowest Common Ancestor
    • M Lowest Common Ancestor With Parent Pointers
    • E Sum root to leaf
    • E Root to leaf path with specified sum
    • M Root to leaf path sum (All Paths)
    • E Inorder Traversal Without Recursion
    • E Preorder Traversal Without Recursion
    • E Kth Node in Inorder Traversal
    • M Compute Successor
    • M Inorder Traversal in Constant Space
    • M Preorder Traversal in Constant Space
    • M Postorder Traversal in Constant Space
    • M Reconstruct Binary Tree
    • M Reconstruct Binary Tree
    • M Reconstruct Binary Tree 2
    • M Form a LinkedList From Leaves Of Binary Tree
    • M Form a LinkedList From Leaves Of Binary Tree 2
    • M Compute Exterior of Binary Tree
    • M Compute Right Sibling Tree
  • Binary Search Trees

    • E Search Binary Tree
    • E Check If Binary Search Tree Satisfies BST Property
    • E Check If Binary Search Tree Satisfies BST Property - Solution 2
    • E Find the First Key Greater Than A Given Value In A BST
    • E Find The K Largest Elements In A BST
    • E Find The K Largest Elements In A BST - Solution 2
    • E Find The K Largest Elements In A BST - Solution 3
    • M Compute The LCA In A BST
    • M Reconstruct A BST From Preorder Traversal Data
    • M Find The Closest Entries In Three Sorted Arrays
    • M Reconstruct A BST From Postorder Traversal Data
    • M Reconstruct A BST From Inorder Traversal Data
    • M Enumerate Numbers Of The Form a + b sqrt2
    • M Enumerate Numbers Of The Form a + b sqrt2 - Solution 2
    • M Build A Minimum Height BST From A Sorted Array
    • M Build A Minimum Height BST From A Sorted Array - Solution 2
    • M Test If Three BST Nodes Are Totally Ordered
    • M The Range Lookup Problem
    • M Add Credits
  • Heaps

    • M Merge Sorted Arrays
    • M Sort An Increasing Decreasing Array
    • M Sort An Almost Sorted Array
    • M Compute the K closest Stars
    • M Compute Median From A Stream
    • M K Largest Elements In A Max Heap
    • M K Largest Elements In A Max Heap - Solution 2
  • Linkedlist

    • E Merge Two Sorted Lists
    • E Merge Two Sorted Doubly LinkedList
    • M Reverse A Sublist
    • M Reverse A Sublist - Solution 2
    • E Reverse A Singly LinkedList
    • M Reverse Every K Sublist
    • E Test For Cyclicity
    • M Find The Start Of A Cycle In A LInkedList
    • M Find The Start Of A Cycle In A LInkedList - Solution 2
    • E Test For Overlapping List - Lists Are Cycle Free
    • M Test For Overlapping List - Lists May Have Cycles
    • E Delete Node From Singly Linkedlist
    • M Remove Kth Last Element From List
    • E Remove Duplicates From Sorted Lists
    • M Implement Cyclic Right Shift For Singly LinkedList
    • M Even Odd Merge
    • E Test If Singly LinkedList Is Palindromic
    • M Implement List Pivoting
    • M Add Two LinkedLists


Blind 75 Questions

  • Arrays

    • E Two Sum
    • E Best Time To Buy And Sell Stock
    • E Contains Duplicate
    • M Product of Array Except Self
    • E Maximum Subarray
  • Trees

    • M Validate Binary Search Tree
    • E Same Tree
    • M Binary Tree Level Order Traversal
    • E Maximum Depth of Binary Tree
    • M Construct Binary Tree from Preorder and Inorder Traversal
    • H Binary Tree Maximum Path Sum
    • E Invert Binary Tree
    • M Kth Smallest Element in a BST
    • E Lowest Common Ancestor of a Binary Search Tree
    • H Serialize and Deserialize Binary Tree
    • E Subtree of Another Tree
  • LinkedLists

    • M Remove Nth Node From End of List
    • E Merge Two Sorted Lists
    • E Merge k Sorted Lists
    • E Linked List Cycle
    • M Reorder List
    • E Reverse Linked List


Leetcode Questions Categorized By Concept & Data Structure

E - Easy, M - Medium , H - Hard,

  • Recursion
    • M Longest Univalue Path

  • Binary Trees
    • E Binary Tree Inorder Traversal
    • E Same Tree
    • E Symmetric Tree
    • E Maximum Depth of Binary Tree
    • E Convert Sorted Array to Binary Search Tree
    • E Balanced Binary Tree
    • E Minimum Depth of Binary Tree
    • E Path Sum
    • E Binary Tree Preorder Traversal
    • E Binary Tree Postorder Traversal
    • E Invert Binary Tree
    • E Lowest Common Ancestor of a Binary Search Tree
    • E Binary Tree Paths
    • E Sum of Left Leaves
    • E Find Mode in Binary Search Tree
    • E Minimum Absolute Difference in BST
    • E Diameter of Binary Tree
    • E Binary Tree Tilt
    • E Subtree of Another Tree
    • E Construct String from Binary Tree
    • E Merge Two Binary Trees
    • E Average of Levels in Binary Tree
    • E Two Sum IV - Input is a BST
    • E Second Minimum Node In a Binary Tree
    • E Minimum Distance Between BST Nodes
    • M Unique Binary Search Trees II
    • M Unique Binary Search Trees
    • M Validate Binary Search Tree
    • M Recover Binary Search Tree
    • M Binary Tree Level Order Traversal
    • M Binary Tree Zigzag Level Order Traversal
    • M Construct Binary Tree from Preorder and Inorder Traversal
    • M Construct Binary Tree from Inorder and Postorder Traversal
    • M Binary Tree Level Order Traversal II
    • M Path Sum II
    • M Flatten Binary Tree to Linked List
    • M Populating Next Right Pointers in Each Node
    • M Populating Next Right Pointers in Each Node II
    • M Sum Root to Leaf Numbers
    • M Binary Search Tree Iterator
    • M Binary Tree Right Side View
    • M Count Complete Tree Nodes
    • M Kth Smallest Element in a BST
    • M Lowest Common Ancestor of a Binary Tree
    • M House Robber III
    • M Path Sum III
    • M Serialize and Deserialize BST
    • M Delete Node in a BST
    • M Most Frequent Subtree Sum
    • M Find Bottom Left Tree Value
    • M Find Largest Value in Each Tree Row
    • M Convert BST to Greater Tree
    • M Add One Row to Tree
    • M Find Duplicate Subtrees
    • M Maximum Binary Tree
    • M Print Binary Tree
    • M Maximum Width of Binary Tree
    • M Trim a Binary Search Tree
    • M Longest Univalue Path
    • M All Nodes Distance K in Binary Tree
    • M Smallest String Starting From Leaf
    • M Maximum Binary Tree II
    • M Construct Binary Search Tree from Preorder Traversal
    • H Binary Tree Maximum Path Sum
    • H Serialize and Deserialize Binary Tree

  • LinkedLists
    • E Merge Two Sorted Lists
    • E Remove Duplicates from Sorted List I
    • E Linked List Cycle
    • E Intersection of Two Linked Lists
    • E Remove Linked List Elements
    • E Reverse Linked List
    • E Palindrome Linked List
    • M Add Two Numbers
    • M Remove Nth Node From End of List
    • M Swap Nodes in Pairs
    • M Rotate List
    • M Remove Duplicates from Sorted List II
    • M Partition List
    • M Reverse Linked List II
    • M Convert Sorted List to Binary Search Tree
    • M Copy List with Random Pointer
    • M Linked List Cycle II
    • M Reorder List
    • M Insertion Sort List
    • M Sort List
    • M Delete Node in a Linked List
    • M Odd Even Linked List
    • M Add Two Numbers II
    • M Split Linked List in Parts
    • H Merge k Sorted Lists
    • H Reverse Nodes in k-Groups

Algorithms

A list of popular algorithms asked during Interviews

  • Graph Algorithms
  • Greedy Algorithms
  • Sorting Algorithms
  • General Algorithms


Recursion Crash Course

A simple crash course to get you up and started with recursion

  • Recursion With Strings
  • Recursion With Numbers
  • Divide & Conquer


Useful Information

References

▶ Data Structures and Algorithms on YouTube


Big O Notation

Big O notation is used to classify algorithms according to how their running time or space requirements grow as the input size grows. On the chart below you may find most common orders of growth of algorithms specified in Big O notation.

Big O graphs

Source: Big O Cheat Sheet.



Below is the list of some of the most used Big O notations and their performance comparisons against different sizes of the input data.

Big O Notation Computations for 10 elements Computations for 100 elements Computations for 1000 elements
O(1) 1 1 1
O(log N) 3 6 9
O(N) 10 100 1000
O(N log N) 30 600 9000
O(N^2) 100 10000 1000000
O(2^N) 1024 1.26e+29 1.07e+301
O(N!) 3628800 9.3e+157 4.02e+2567


Data Structure Operations Complexity

Data Structure Access Search Insertion Deletion Comments
Array 1 n n n
Stack n n 1 1
Queue n n 1 1
Linked List n n 1 n
Hash Table - n n n In case of perfect hash function costs would be O(1)
Binary Search Tree n n n n In case of balanced tree costs would be O(log(n))
B-Tree log(n) log(n) log(n) log(n)
Red-Black Tree log(n) log(n) log(n) log(n)
AVL Tree log(n) log(n) log(n) log(n)
Bloom Filter - 1 1 - False positives are possible while searching


Array Sorting Algorithms Complexity

Name Best Average Worst Memory Stable Comments
Bubble sort n n2 n2 1 Yes
Insertion sort n n2 n2 1 Yes
Selection sort n2 n2 n2 1 No
Heap sort n log(n) n log(n) n log(n) 1 No
Merge sort n log(n) n log(n) n log(n) n Yes
Quick sort n log(n) n log(n) n2 log(n) No Quicksort is usually done in-place with O(log(n)) stack space
Shell sort n log(n) depends on gap sequence n (log(n))2 1 No
Counting sort n + r n + r n + r n + r Yes r - biggest number in array
Radix sort n * k n * k n * k n + k Yes k - length of longest key