Skip to content

Instantly share code, notes, and snippets.

@sahilrajput03
Last active November 29, 2022 16:18
Show Gist options
  • Save sahilrajput03/1298a80f3097a56b6304bffde8853287 to your computer and use it in GitHub Desktop.
Save sahilrajput03/1298a80f3097a56b6304bffde8853287 to your computer and use it in GitHub Desktop.

image

image

image

image

image

image

image

image

image

image

image

image

image

image

  • Auxiliary space complexity: Its the space required by the algorithm only, not taken by the inputs.

image

image

image

image

image

image

image

image

image

object.hasOwnProperty() Source: MDN - Click here

image

image

image

image

image

image

  • Array.slice() => O(n)

image

  • Array.splice() => O(n)

image

image

image

image

image

image

image

image

  • Why pseduo code is important? Becoz often times problems asked in interview are not supposed to be solved but they want to know the approach you have formulated so just in case you run of time atleast the interviewer know where you're heading.

image

image

image

image

image

image

More short solution:

image

FYI: You can state in interview that in some cases regular expression can be more expensive as we can see the stats for it in below case study:

image

  • so we can use charcodeAt() fn to re-write that fn to make it more efficient like that:

image

  • Commonly Patterns

image

image

image

image

Anagrams Problem can be solved by use of Frequency Counter Patterns as well:

image

Solution of Anagrams Question:

image

image

image

image

image

  • Solution with Two Pointer (O(n)) and another O(n) solution from author:

image

image

Question: image

image

Solution with "Sliding Window" pattern:

image

image

Divide and Conquer Pattern:

image

Question:

image

Naive Solution:

image

Solution with Divide and Conquer Pattern:

image

  • Recursion:

image

image

image

  • current function called is always on top of the "Call Stack":

image

image

image

image

image

image

  • Sample question without recursion:

image

Same above sample question with recursion:

image

image

Sum Range function wth recursion:

image

Iterative (i.e, using iterations or Non-recursive) Solution for finding factorial:

image

Recursive solution for finding factorial:

image

image

image

  • collectOddValues() fn with recursion pattern:

image

  • Pure Recusion (no nested recursive fn)

image

image

image

image

image

image

Linear Search:

image

image

image

image

  • Binary Search Pseudocode:

image

image

image

Time Complexity for binary search?

image

image

Naive String Search: In main string, we find the nummber of match found for sample string.

Pseudeo Code:

image

From author:

image

Sorting:

image

image

image

image

image

image

image

Bubble Sort: (Also you can browser website: visualgo.net)

image

So two pairs of items are compared and larger one is switched to right side if thats the case. So this makes the sorting done for the last item, i.e, the last item is definitely the largest number in the array after the first (n-1) iterations over the array. Next we do it again starting from start again.

image

Bubble Sort Pseudocode:

image

image

Selection Sort

image

Pseudo code for Selection sort:

image

image

Insertion Sort - Really good when we want to put new item into the already sorted array becoz it knows exactly where to put the element.

FYI: Insertion and Bubble sort performs very good when data is nearly sorted. See here.

image

image

Learn Insertion sort here by ~ Tekena: Insertion sort in 2 minutes: Click here

image

My simplified way using i as key index for incrementor:

image

My Demonstration with vscode breakpoints:

insertion_sort.mp4

image

image

image

image

image

image

image

Merging Arrays Pseudo code:

image

Merge arrays code:

image

image

image

Callstack for merge sort:

image

image

image

  • Quick Sort - Intro (NEED TO REVIEW BACK)

image

image

image

image

image

image

image

image

image

  • Radix Sort - TODO: Need to do this. 17. Radix Sort

Folder sizes:

image

Data Structures:

image

image

image

Array:

image

Singly Linkedlist:

image

Hash table:

image

Binary Heap:

image

Binary Search Tree:

image

Graph - Unweighted Undirected Graph

image

Graph - Unweighted Directed Graph

image

image

Popular Quetions in Interviews:

  • reverse a singly linkedlist
  • implement a priority queue
  • balance a binary tree

image

Gps data suits graph data structure becoz its easy find the shortest path distance b/w two values:

image

image

image

image

ES2015 class syntax

image

image

image

image

image

image

Singly Lined Lists

image

image

image

image

image

image

image

Doubly linkedlist

image

image

image

image

image

image

BigO of Doubly Linked List:

image

image

stack and queue are collections

  • stack is LIFO (Last in first out)
  • queue is FIFO (first in first out)

image

image

image

image

There are more than one way of implementing the STACK:

  1. Array Implementation
  2. Linked List implementation

Builtin Array data structure in javascript uses STACK data structure principle:

image

Builtin Arrays also works on QUEUE principles as well as far as FIFO is concerned: (But internally compiler needs to re-index the rest of the items in the array when you insert/remove at the beginnign and that is very costly as we studied earlier becoz of re-indexing of those items):

image

Big O of STACK:

image

image

Queue: First in first out. Example: Airport line when you're buying tickets. So the first person in the line gets the ticket first.

Queue implementation with builtin arrays(east to use but not the best implementation for queue as removing/adding from top is quite costly in builin array methods i.e, shift and push):

image

Queue using Unshift/pop

image

image

BigO Queue:

image

Tree

image

image

image

image

image

Use of tree data structure:

image

Kinds of trees

image

image

image

image

image

BST

image

Inserting node in BST:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment