转:Top 10 Algorithms for Coding Interview
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:
- String
- Linked List
- Tree
- Graph
- Sorting
- Recursion vs. Iteration
- Dynamic Programming
- Bit Manipulation
- Probability
- 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 |
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 {
|
Two popular applications of linked list are stack and queue.
Stack
class Stack{
|
Queue
class Queue{
|
3. Tree
Tree here is normally binary tree. Each node contains a left node and right node like the following:
class TreeNode{
|
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
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{
|
2) Define a Queue
class Queue{
|
3) Breath First Search uses a Queue
public class GraphTest {
|
Output:
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 sort, Mergesort, Quicksort, InsertionSort.
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){
|
The time complexity of the recursive method is exponential to n. There are a lot of redundant computations.
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) {
|
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.
public static int[] A = new int[100]; |
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){
|
For example, get second bit of number 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){
|
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
Category: Algorithms,Interview
转:Top 10 Algorithms for Coding Interview的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- crack the coding interview
crack the coding interview answer c++ 1.1 #ifndef __Question_1_1_h__ #define __Question_1_1_h__ #i ...
- TOP 10 ONLINE COMPILER
Top 10 Online Compilers +1338 Tweet Share106 Share Pin 444 Shares Online compilers are one type of t ...
- 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 ...
- Top 10 Programming Fonts
Top 10 Programming Fonts Sunday, 17 May 2009 • Permalink Update: This post was written back in 2009, ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
随机推荐
- javaweb笔记5之请求编码问题
post提交: 设置实体内容的编码:request.setCharacterEncoding("utf-8"); 注意:一定要在获取所有参数之前设置,否则设置无效! get方式提交 ...
- validate方法配置项
validate()方法配置项 submitHandler 通过验证后运行的函数,可以加上表单提交的方法 invalidHandler 无效表单提交后运行的函数 ignore 对某些元素不进行验证 r ...
- pycharm-4.5.3 汉化教程(附汉化包)
汉化包地址 http://pan.baidu.com/s/1coLZau 密码: muu5 1.先找到Pycharm文件夹中的lib文件夹,将resources_en.jar复制到桌面并改名为res ...
- [Angular 2] ng-model and ng-for with Select and Option elements
You can use Select and Option elements in combination with ng-for and ng-model to create mini-forms ...
- SVN Checkout 不包括源文件夹根目录(转)
SVN Checkout 不包括源文件夹根目录,比如我要checkout trunk/ 下面的所有文件,但是不包括trunk 文件夹 我们可以在svn文件夹后面打个空格,在加个“.”就行了 eg: ...
- Android(java)学习笔记235:多媒体之计算机图形表示方式
1.多媒体 很多媒体:文字(TextView,简单不讲),图片,声音,视频等等. 2.图片 计算机如何表示图片的? (1)bmp 高质量保存 256色位图:图片中的每个像素点可以有256种颜 ...
- Java中如何分析一个案列---猫狗案例为例
猫狗案例: 具体事务: 猫.狗 共性: 姓名.年龄.吃饭 分析:从具体到抽象 猫: 姓名.年龄--->成员变量 吃饭 ---> 成员方法 构造方法:无参.有参 狗: 姓名.年龄 ...
- 模板-->中国剩余定理[互质版本]
如果有相应的OJ题目,欢迎同学们提供相应的链接 相关链接 所有模板的快速链接 扩展欧几里得extend_gcd模板 poj_1006_Biorhythms,my_ac_code 简单的测试 None ...
- NYOJ-520 最大素因子
这个题基本上就两个知识点, 一个素数筛选法求素数,另一个是求最大公因子, 不过确定最大素数在素数表中的位置时,要用到二分的思想,不然会超时,下面是具体代码的实现; #include <stdio ...
- 使用MWC四轴起飞侧翻解决方法
原因如下:1.电机顺序错了,如上图所示,上面蓝色的箭头是机头,绿色的箭头是电机转向,3.10.11.9对应MWC飞控版上的D3,D9,D11,D9,蓝色箭头对应MWC飞控板的箭头 或者传感器的Y轴 以 ...