Show Menu

Tech Interviews Cheat Sheet (DRAFT) by

A cheat sheet for cracking coding interviews!!

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Hash table implem­ent­ation

int hash = (key % TABLE_SIZE)
To add if already there:
hash = (hash + 1) % TABLE_­SIZE;

Increase the size of an array

arr = Arrays.copy(arr, size*2);

Min Heap implem­ented in an Array

getParent(int pos) return (pos -2)/2;
getLeft(int pos) return (pos*2 + 1);
getRight(int pos) return (pos*2+2);
// Maintain a pointer to the bottom of the array

Detect loop in linked list

*slow pointer (one at a time)
*fast pointer (two at a time)
*if both point to same node - there is a loop
* To detect the start: reset the slow pointer, counters will meet at the start of the loop



BT Find shortest path from root to leaf

int getMinDepth(Node root) {
  if( root == null ) return 0;
  if( root.left == null && root.riht == null )  return 1;
  int leftDepth = root.left!=null?getMinDepth(root.left) : Integer MAX_VALUE;
  int rightDepth = root.right!=null?getMInDepth(root.right): Integer MIN_VALUE;
  return 1 + Math.min(leftDepth, rightDepth);