Mandatory Algorithms for Coding Interview

Basic algorithms that you must know if you want to crack your coding interview. In case you are starting to prepare for coding practice then make sure you learn all of these important algorightms. Get the source code for basic algorithms in JavaScript here

Binary Search in Sorted Array

**O(log(n) time O(1) space**

Kadane’s Algorithm

Moore’s Voting Algorithm

Reverse Linked List

**O(n) time O(1) space**

Flyod’s Cycle Detection Algorithm

Sorting Algorithms

Merge sort

In Merge sort since I need 2 Aux arrays. And it requires more space to solve merge sorting problem. Therefore, I will prefer Linked List datastructure. Because, in linkedlist to store values, I do not need contigous memory slots. Hence optimizing memory usage.

**O(n log (n)) time O(1) space**

Quick Sort

**O(n log (n)) time O( log (n) ) and wrost case O(n) space**

Quicksort is best fit for sorting Arrays since it is completely in-place sorting. No auxilary arrays are requried. So no need to store contigous memory slots on computer.

Partition Logic

  • All the element lesser than pivot are pushed to the left of the partition index (use swap)

Tree Traversal

Breadth-First Traversal(BFT)

Whenever tree is Sparse use BFT

**O(n) time O(n) space**

Depth-First Traversal (DFT)

**O(n) time O(n) space**

Binary Search Tree (BST)

Insert in Binary Search Tree (BST)

**O(Log(n)) time Space O(1)**

For 1 insert operation, avg case is O(lgn) and worst case is O(n) For n insert operations, avg case is O(nlgn) and worst case is O(n²)

Remove Node from BST

**O(log n) time space O(1)**

Search in BST

O(log(n)) time | O(1)

Min Max and Height in BST

Heap implementation

In the interview no one will ask you to implement heap. This excercise is for your coding skills imporvement. In the interview, you can use array instead of heap and explain the concept.

Insert node in the Heap

**O(log(n) time O(1) space**

Revove root node from the Heap

**O(log(n) time O(1) space**

Reference

Thanks for reading my article till end. I hope you learned something special today. If you enjoyed this article then please share to your friends and if you have suggestions or thoughts to share with me then please write in the comment box.

💖 Say 👋 to me!
Rupesh Tiwari
Founder of Fullstack Master
Email: rupesh.tiwari.info@gmail.com
Website: RupeshTiwari.com

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store