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 efforts, this list only serves as an introduction. They are viewed from a Java perspective. The following concepts will be covered:

  1. String
  2. Linked List
  3. Tree
  4. Graph
  5. Sorting
  6. Recursion vs. Iteration
  7. Dynamic Programming
  8. Bit Manipulation
  9. Probability
  10. Combinations and Permutations

1. String

Without code auto-completion of any IDE, the following methods should be remembered.

toCharyArray() //get char array of a String
Arrays.sort() //sort an array
Arrays.toString(char[] a) //convert to string
charAt(int x) //get a char at the specific index
length() //string length
length //array size

Also in Java a String is not a char array. A String contains a char array and other fields and methods.

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.

class Node {
int val;
Node next;
 
Node(int x) {
val = x;
next = null;
}
}

Two popular applications of linked list are stack and queue.

Stack

class Stack{
Node top;
 
public Node peek(){
if(top != null){
return top;
}
 
return null;
}
 
public Node pop(){
if(top == null){
return null;
}else{
Node temp = new Node(top.val);
top = top.next;
return temp;
}
}
 
public void push(Node n){
if(n != null){
n.next = top;
top = n;
}
}
}

Queue

class Queue{
Node first, last;
 
public void enqueue(Node n){
if(first == null){
first = n;
last = first;
}else{
last.next = n;
last = n;
}
}
 
public Node dequeue(){
if(first == null){
return null;
}else{
Node temp = new Node(first.val);
first = first.next;
return temp;
}
}
}

3. Tree

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

class TreeNode{
int value;
TreeNode left;
TreeNode right;
}

Here are some concepts related with trees:

  1. Binary Search Tree: for all nodes, left children <= current node <= right children
  2. Balanced vs. Unbalanced: In a balanced tree, the depth of the left and right subtrees of every node differ by 1 or less.
  3. Full Binary Tree: every node other than the leaves has two children.
  4. 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.
  5. 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

4. Graph

Graph related questions mainly focus on depth first search and breath first search.

Below is a simple implementation of a graph and breath first search.

1) Define a GraphNode

class GraphNode{
int val;
GraphNode next;
GraphNode[] neighbors;
boolean visited;
 
GraphNode(int x) {
val = x;
}
 
GraphNode(int x, GraphNode[] n){
val = x;
neighbors = n;
}
 
public String toString(){
return "value: "+ this.val;
}
}

2) Define a Queue

class Queue{
GraphNode first, last;
 
public void enqueue(GraphNode n){
if(first == null){
first = n;
last = first;
}else{
last.next = n;
last = n;
}
}
 
public GraphNode dequeue(){
if(first == null){
return null;
}else{
GraphNode temp = new GraphNode(first.val, first.neighbors);
first = first.next;
return temp;
}
}
}

3) Breath First Search uses a Queue

public class GraphTest {
 
public static void main(String[] args) {
GraphNode n1 = new GraphNode(1);
GraphNode n2 = new GraphNode(2);
GraphNode n3 = new GraphNode(3);
GraphNode n4 = new GraphNode(4);
GraphNode n5 = new GraphNode(5);
 
n1.neighbors = new GraphNode[]{n2,n3,n5};
n2.neighbors = new GraphNode[]{n1,n4};
n3.neighbors = new GraphNode[]{n1,n4,n5};
n4.neighbors = new GraphNode[]{n2,n3,n5};
n5.neighbors = new GraphNode[]{n1,n3,n4};
 
breathFirstSearch(n1, 5);
}
 
public static void breathFirstSearch(GraphNode root, int x){
if(root.val == x)
System.out.println("find in root");
 
Queue queue = new Queue();
root.visited = true;
queue.enqueue(root);
 
while(queue.first != null){
GraphNode c = (GraphNode) queue.dequeue();
for(GraphNode n: c.neighbors){
 
if(!n.visited){
System.out.print(n + " ");
n.visited = true;
if(n.val == x)
System.out.println("Find "+n);
queue.enqueue(n);
}
}
}
}
}

Output:

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

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
Counting Sort n+k n+k n+k
Insertion sort n^2 n^2  
Quick sort n log(n) n^2  
Merge sort n log(n) n log(n) depends

In addition, here are some implementations/demos: Counting sortMergesortQuicksortInsertionSort.

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;

public static int f(int n){
if(n <= 2) return n;
int x = f(n-1) + f(n-2);
return x;
}

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(1) + f(0) + f(1) + f(0) + f(1)
f(1) + f(0) + f(1) + f(1) + f(0) + f(1) + f(0) + f(1)

It should be straightforward to convert the recursion to iteration.

public static int f(int n) {
 
if (n <= 2){
return n;
}
 
int first = 1, second = 2;
int third = 0;
 
for (int i = 3; i <= n; i++) {
third = first + second;
first = second;
second = third;
}
 
return third;
}

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:

  1. An instance is solved using the solutions for smaller instances.
  2. The solution for a smaller instance might be needed multiple times.
  3. The solutions to smaller instances are stored in a table, so that each smaller instance is solved only once.
  4. 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.

public static int[] A = new int[100];
 
public static int f3(int n) {
if (n <= 2)
A[n]= n;
 
if(A[n] > 0)
return A[n];
else
A[n] = f3(n-1) + f3(n-2);//store results so only calculate once!
return A[n];
}

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)

public static boolean getBit(int num, int i){
int result = num & (1<<i);
 
if(result == 0){
return false;
}else{
return true;
}
}

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;

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.

public static double caculateProbability(int n){
double x = 1;
 
for(int i=0; i<n; i++){
x *= (365.0-i)/365.0;
}
 
double pro = Math.round((1-x) * 100);
return pro/100;
}

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.

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

Related posts:

Category: Algorithms,Interview

转:Top 10 Algorithms for Coding Interview的更多相关文章

  1. Top 10 Algorithms for Coding Interview--reference

    By X Wang Update History:Web Version latest update: 4/6/2014PDF Version latest update: 1/16/2014 The ...

  2. Top 10 Algorithms of 20th and 21st Century

    Top 10 Algorithms of 20th and 21st Century MATH 595 (Section TTA) Fall 2014 TR 2:00 pm - 3:20 pm, Ro ...

  3. 18 Candidates for the Top 10 Algorithms in Data Mining

    Classification============== #1. C4.5 Quinlan, J. R. 1993. C4.5: Programs for Machine Learning.Morga ...

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  5. crack the coding interview

    crack the coding interview answer c++ 1.1 #ifndef __Question_1_1_h__  #define __Question_1_1_h__  #i ...

  6. TOP 10 ONLINE COMPILER

    Top 10 Online Compilers +1338 Tweet Share106 Share Pin 444 Shares Online compilers are one type of t ...

  7. Favorites of top 10 rules for success

    Dec. 31, 2015 Stayed up to last minute of 2015, 12:00am, watching a few of videos about top 10 rules ...

  8. Top 10 Programming Fonts

    Top 10 Programming Fonts Sunday, 17 May 2009 • Permalink Update: This post was written back in 2009, ...

  9. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

随机推荐

  1. [置顶] 【Git入门之十四】Git GUI

    原创作品,转载请标明:http://blog.csdn.net/jackystudio/article/details/12374495 文章都快写完了,Git GUI才浮出水面,很多人要骂我,这么方 ...

  2. sqlserver使用OpenQuery或OPENROWSET遇到的问题

    OpenQuery,OPENROWSET允许用户在链接服务器上查询.通过这种方法来得到查询的结果集.     1.在创建存储过程中,必须设置           SET   ANSI_NULLS   ...

  3. PHP实现大文件的上传设置

    打开php.ini,首先找到 ;;;;;;;;;;;;;;;; ; File Uploads ; ;;;;;;;;;;;;;;;; 区域,有影响文件上传的以下几个参数: file_uploads = ...

  4. Linux 安全

     Linux 安全 1.安装 使系统处于单独(或隔离)的网络中.以防止未受保护的系统连接到其它网络或互联网中受到可能的攻击 安装完成后将下面软件卸载 pump                      ...

  5. 谈"http get和post的区别"

    --以下内容如有各种问题,烦请指出,谢谢各位^_^-- 最基本的Java程序员面试题都有这个题 --http get和post的区别? 不少人大学还没毕业就知道,就算不知道也会去搜,我记得我快毕业那会 ...

  6. EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询

    场景:在实际的项目中使用EntityFramework都会遇到使用Ef处理连接查询的问题,这里做一些小例子如何通过Linq语法处理内连接(inner join).外连接(left/right oute ...

  7. 【.NET调用Python脚本】C#调用python requests类库报错 'module' object has no attribute '_getframe' - IronPython 2.7

    最近在开发微信公众号,有一个自定义消息回复的需求 比如用户:麻烦帮我查询一下北京的天气? 系统回复:北京天气,晴,-℃... 这时候需要根据关键字[北京][天气],分词匹配需要执行的操作,然后去调用天 ...

  8. raknet unity3d

    Raknet是一高性能的跨平台的网络库. 他主要基于UDP实现,性能非常好,能够做server. 鉴于unity3d较差的网络支持. 本人成功实现了raknet c# for unity3d的使用,s ...

  9. TCO 2015 Round 1B DIV1 500 概率题

    [题意]现在有一些线索,每个线索被发现的概率p[i],如果线索i被知道,那么其他线索也可能会被知道,用vector<string> c给出,c[i][j]='Y'表示知道i这个线索,j这个 ...

  10. eclipse[日文版] 的SVN 上传步骤

    可能有些朋友在日企上班,肯定要用到SVN,可是一般就下载和更新,没有用到上传 这里来介绍下上传 1.项目右键 2.点击Share Project 3.点击SVN下一步 4.选择你的上传的服务器地址 5 ...