http://www.programcreek.com/2012/11/top-10-algorithms-for-coding-interview/

The problem of climbing steps perfectly fit those 4 properties. Therefore, it can be solve by using dynamic programming.

12/06/2013 – Add “Add Two Numbers”, “Binary Tree Traversal(pre/in/post-order)”, “Find Single Number”.

12/07/2013 – Add “Word Break”

12/08/2013 – Add “Reorder List”

12/10/2013 – Add “Edit Distance”, ” Reverse Integer”

12/14/2013 – Add “Copy List with Random Pointer”, “Evaluate Reverse Polish Notation”, “Word Ladder”.

The following are top 10 algorithms related concepts in coding interview. I will try to illustrate those concepts though some simple examples. As understanding those concepts requires much more effort, this list only serves as an introduction. They are viewed from a Java perspective and the following concepts will be covered:

- String
- Linked List
- Tree
- Graph
- Sorting
- Recursion vs. Iteration
- Dynamic Programming
- Bit Manipulation
- Probability
- Combinations and Permutations
- Other problems that need us to find patterns

I will keep updating this list to add more classic problems and problems from Leetcode.

**1. String**

Unlike in C++, a String is not a char array in Java. It is a class that contains a char array and other fields and methods. Without code auto-completion of any IDE, the following methods should be remembered.

Classic problems: Evaluate Reverse Polish Notation, Longest Palindromic Substring, Word Break, Word Ladder.

* Questions related with strings/arrays often require advanced algorithm to solve.

**2. Linked List**

The implementation of a linked list is pretty simple in Java. Each node has a value and a link to next node.

Two popular applications of linked list are stack and queue.

Stack

Queue

It is worth to mention that Java standard library already contains a class called “Stack“, and LinkedListcan be used as a Queue. (LinkedList implements the Queue interface) If you directly need a stack or queue to solve problems in your interview, you can directly use them.

**3. Tree**

Tree here is normally binary tree. Each node contains a left node and right node like the following:

Here are some concepts related with trees:

*Binary Search Tree*: for all nodes, left children <= current node <= right children*Balanced vs. Unbalanced*: In a balanced tree, the depth of the left and right subtrees of every node differ by 1 or less.*Full Binary Tree*: every node other than the leaves has two children.*Perfect Binary Tree*: a full binary tree in which all leaves are at the same depth or same level, and in which every parent has two children.*Complete Binary Tree*: a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible

Classic problems: Binary Tree Preorder Traversal , Binary Tree Inorder Traversal, Binary Tree Postorder Traversal, Word Ladder.

**4. Graph**

Graph related questions mainly focus on depth first search and breath first search. Depth first search is straightforward, you can just loop through neighbors starting from the root node.

Below is a simple implementation of a graph and breath first search. The key is using a queue to store nodes.

1) Define a GraphNode

2) Define a Queue

3) Breath First Search uses a Queue

Output:

value: 2 value: 3 value: 5 Find value: 5

value: 4

value: 4

Classic Problems: Clone Graph

**5. Sorting**

Time complexity of different sorting algorithms. You can go to wiki to see basic idea of them.

Algorithm | Average Time | Worst Time | Space |

Bubble sort | n^2 | n^2 | 1 |

Selection sort | n^2 | n^2 | 1 |

Insertion sort | n^2 | n^2 | |

Quick sort | n log(n) | n^2 | |

Merge sort | n log(n) | n log(n) | depends |

* BinSort, Radix Sort and CountSort use different set of assumptions than the rest, and so they are not “general” sorting methods. (Thanks to Fidel for pointing this out)

**6. Recursion vs. Iteration**

Recursion should be a built-in thought for programmers. It can be demonstrated by a simple example.

Question: there are n stairs, each time one can climb 1 or 2. How many different ways to climb the stairs.

*Step 1: Finding the relationship before n and n-1.*

To get n, there are only two ways, one 1-stair from n-1 or 2-stairs from n-2. If f(n) is the number of ways to climb to n, then f(n) = f(n-1) + f(n-2)

*Step 2: Make sure the start condition is correct.*

f(0) = 0;

f(1) = 1;

f(1) = 1;

The time complexity of the recursive method is exponential to n. There are a lot of redundant computations.

f(5)

f(4) + f(3)

f(3) + f(2) + f(2) + f(1)

f(2) + f(1) + f(2) + f(2) + f(1)

f(4) + f(3)

f(3) + f(2) + f(2) + f(1)

f(2) + f(1) + f(2) + f(2) + f(1)

It should be straightforward to convert the recursion to iteration.

For this example, iteration takes less time. You may also want to check out Recursion vs Iteration.

**7. Dynamic Programming**

Dynamic programming is a technique for solving problems with the following properties:

- An instance is solved using the solutions for smaller instances.
- The solution for a smaller instance might be needed multiple times.
- The solutions to smaller instances are stored in a table, so that each smaller instance is solved only once.
- Additional space is used to save time.

The problem of climbing steps perfectly fit those 4 properties. Therefore, it can be solve by using dynamic programming.

**8. Bit Manipulation**

Bit operators:

OR (|) | AND (&) | XOR (^) | Left Shift (<<) | Right Shift (>>) | Not (~) |

1|0=1 | 1&0=0 | 1^0=1 | 0010<<2=1000 | 1100>>2=0011 | ~1=0 |

Get bit i for a give number n. (i count from 0 and starts from right)

For example, get second bit of number 10.

i=1, n=10

1<<1= 10

1010&10=10

10 is not 0, so return true;

1<<1= 10

1010&10=10

10 is not 0, so return true;

Classic Problems: Find Single Number.

**9. Probability**

Solving probability related questions normally requires formatting the problem well. Here is just a simple example of such kind of problems.

There are 50 people in a room, what’s the probability that two people have the same birthday? (Ignoring the fact of leap year, i.e., 365 day every year)

Very often calculating probability of something can be converted to calculate the opposite. In this example, we can calculate the probability that all people have unique birthdays. That is: 365/365 * 364/365 * 363/365 * … * 365-n/365 * … * 365-49/365. And the probability that at least two people have the same birthday would be 1 – this value.

calculateProbability(50) = 0.97

**10. Combinations and Permutations**

The difference between combination and permutation is whether order matters.

Please leave your comment if you think any other problem should be here.

**11. Others**

Other problems need us to use observations to form rules to solve them.

Classic problems: Reverse Integer

**Revision History**

12/06/2013 – Add “Add Two Numbers”, “Binary Tree Traversal(pre/in/post-order)”, “Find Single Number”.

12/07/2013 – Add “Word Break”

12/08/2013 – Add “Reorder List”

12/10/2013 – Add “Edit Distance”, ” Reverse Integer”

12/14/2013 – Add “Copy List with Random Pointer”, “Evaluate Reverse Polish Notation”, “Word Ladder”.

References/Recommmended Materials:

1. Binary tree

2. Introduction to Dynamic Programming

3. UTSA Dynamic Programming slides

4. Birthday paradox

5. Cracking the Coding Interview: 150 Programming InterviewQuestions and Solutions, Gayle Laakmann McDowell

5. Counting sort

1. Binary tree

2. Introduction to Dynamic Programming

3. UTSA Dynamic Programming slides

4. Birthday paradox

5. Cracking the Coding Interview: 150 Programming InterviewQuestions and Solutions, Gayle Laakmann McDowell

5. Counting sort

## Comments

## Post a Comment