23
My 2 Cents of Preparing Coding Interview

Algorithm hierarchy

Embed Size (px)

DESCRIPTION

My 2 Cents of Preparing Coding Interview 1. Basic Algorithm 2. Space–time tradeoff 3. Pruning Algorithm 4. Optimized Algorithm 5. Big data Algorithm

Citation preview

Page 1: Algorithm hierarchy

My 2 Cents of Preparing Coding Interview

Page 2: Algorithm hierarchy

Introduction �  In most experience of Coding Interview,

we have noticed more and more interview questions are focusing on algorithms.

�  To tell whether algorithms are better or worse, there is complexity BigO to measure the performance in theory.

�  Today, I would like to introduce Algorithm Hierarchy to organize thinking way.

Page 3: Algorithm hierarchy

Not only Algorithm

� Remember: Not only is Algorithm one key part of coding interview, but also working attitude, communication skills, big picture thinking and so on are more considered etc.

Page 4: Algorithm hierarchy

Algorithm Hierarchy in thought

�  1. Basic Algorithm �  2. Space–time tradeoff �  3. Pruning Algorithm �  4. Optimized Algorithm �  5. Big data Algorithm

� Remember: As hierarchy is like pyramid, you would better working more on foundation then looking forward.

Page 5: Algorithm hierarchy

1. Basic Algorithm

�  (completed and easy reading –cy) � Recursion, Backtracking, Blind

searching and sorting like DFS, BFS, merge sort and qsort etc.

Page 6: Algorithm hierarchy

2. Space–time tradeoff

�  (completed and fast by additional space –cy)

�  Iteration, Dynamic Programming, Hash, Priority queue etc.

Page 7: Algorithm hierarchy

3. Pruning Algorithm

�  (completed and fast via shortcuts –cy) � Binary search in rotated array, K largest,

Single Linked List cycle detect, Matrix multiplication etc.

Page 8: Algorithm hierarchy

4. Optimized Algorithm

�  (faster and almost completed –cy) � Estimated value, Hill climbing, Greedy

Heuristic search like A*, D* etc.

Page 9: Algorithm hierarchy

5. Big data Algorithm

�  (fastest and almost completed –cy) � Divide and Conquer (Cloud, Cluster)

and Machine learning, (Genetic, Ant Colony), Artificial intelligence (Alpha-Beta, MCTS) etc.

Page 10: Algorithm hierarchy

Example of Algorithm Hierarchy

�  Let me explain hierarchy by calculating Fibonacci sequence.

�  0, 1, 1, 2, 3, 5, 8, …

� Now we need to calculate nTh number in Fibonacci sequence.

Page 11: Algorithm hierarchy

Fibonacci sequence (1)

�  1. Basic Algorithm : Complexity O(n!)

�  long long f1(int n) { �  return n < 2 ? n : (f1(n-1) + f1(n-2)); �  }

Page 12: Algorithm hierarchy

Fibonacci sequence (2) �  2. Space–time tradeoff : Complexity O(n) �  long long f2(int n){ �  long long f[2] = {0, 1}; �  while (--n>=1) { �  f[0]=f[0]+f[1]; �  swap(f[0], f[1]); �  } �  return f[n+1]; �  }

Page 13: Algorithm hierarchy

Fibonacci sequence (3)

�  3. Probing Algorithm : Complexity O(log n)

�  long long f3(int n){ �  return (n < 2)? n :

MatrixPower(n-1).m_00; //power of matrix { {1,1}, {1, 0} }

�  }

Page 14: Algorithm hierarchy

Fibonacci sequence (4)

�  4. Optimized Algorithm : Complexity O(1)

�  const double sqrt5 =sqrt(5.0); �  long long f4 (int n){ �  return 0.5 + (pow((1+sqrt5)/2, n)) /

sqrt5; �  }

Page 15: Algorithm hierarchy

Fibonacci sequence (5)

�  5. Big data Algorithm

�  long long f5 (int n){ �  return f[n]; �  }

Page 16: Algorithm hierarchy

Fibonacci sequence (Output)

�  f1(90) = timeout �  f2(90) = 2880067194370816120 �  f3(90) = 2880067194370816120 �  f4(90) = 2880067194370824704 �  f5(90) = 2880067194370816120 � You may see f4(90) is slightly different

because of double-precision, but it works for n<=70

Page 17: Algorithm hierarchy

No Recursion

Remember: Don’t use Recursion for large scale problem, using Iteration instead at least, especially for graph problems like tree verify, sum and traversal etc.

Page 18: Algorithm hierarchy

Algorithm Hierarchy in Interview

�  For Algorithm Hierarchy in Coding Interview, in my humble opinion, most Phone interview is on level 1, and most on-site interview is on level 2-3, however, you may asking about level 4-5 algorithm.

Page 19: Algorithm hierarchy

Real Sample: Amazon’s most asked interview questions �  (source: geeksquiz.com) �  1) K largest elements from a big file or array. �  2) Find a triplet a, b, c such that a2 = b2 + c2. Variations of this problem like find

a triplet with sum equal to 0. Find a pair with given sum. All such questions are efficiently solved using hashing.

�  3) Binary tree traversal questions like left view, right view, top view, bottom view, maximum of a level, minimum of a level, children sum property, diameter etc.

�  4) Convert a BST into a DLL and DLL to BST in place. �  5) Vertical traversal of a Binary Tree. �  6) Lowest Common ancestor in a Binary Search Tree and Binary Tree. �  7) Implement a stack with push(), pop() and min() in O(1) time. �  8) Reverse a linked list in groups of size k. �  9) Given two numbers represented by two linked lists, write a function that

returns sum list. �  10) Rotate a matrix by 90 degree. �  11) Some stack based questions like stock span problem, next greater element. �  12) Some Dynamic Programming problems like maximum sum subarray,

maximum sum subarray such that no elements are consecutive, edit distance, assembly line scheduling.

�  You may easily find out there are 4 questions in each level 1-3, well balanced.

Page 20: Algorithm hierarchy

KISS in phone interview Think aloud face to face � Remember: Don’t think too complex in

phone interview, just clarify your idea and keep it stupid simple(KISS).

�  For on-site interview, you would better to well prepare and think aloud.

Page 21: Algorithm hierarchy

oj.leetcode.com for preparing  �  Last but not least, let me recommend

oj.leetcode.com for preparing Coding Interview.

�  After solving more than 140 problems in oj.leetcode.com in several days, I could summary out its Hierarchy: 49% level 1, 28% level 2, 23% level 3.

�  LeetCode is focusing on data structures and algorithms. It requires not only just workable, but also optimized code. So you need to program in strict time and space complexity.

Page 22: Algorithm hierarchy

Bottom line to pass interview

� Remember: In order to pass coding interview, you may need to solve 2-5 problems per hour in oj.leetcode.com.

Page 23: Algorithm hierarchy

Thanks �  Thanks for watching. It is just my

humble opinion, my 2 cents.

� Should you have any questions, please feel free to contact me.

� All rights reserved. Please contact changyu.yang for permission to copy, distribute or reprint.