The following are the top 10 algorithm-Related Concepts in Programming Interviews. I will explain these concepts through some simple examples. Since it takes more efforts to fully master these concepts, this list is just an introduction. This article will look at the problem from the Java perspective, including the following concepts: 1. if the IDE does not have the code auto-complementing function, you should remember the following methods. ToCharArray ()//
The following are the top 10 algorithm-Related Concepts in Programming Interviews. I will explain these concepts through some simple examples. Since it takes more efforts to fully master these concepts, this list is just an introduction. This article will look at the problem from the Java perspective, including the following concepts:
1. string
If the IDE does not have the code auto-completion function, you should remember the following methods.
ToCharArray () // Obtain the char array Arrays corresponding to the string. sort () // array sorting Arrays. toString (char [] a) // Convert the array to the string charAt (int x) // Obtain the character length () at an index // string length // array size
2. linked list
In Java, the implementation of the linked list is very simple. each Node has a value val and a link to the next Node.
class Node { int val; Node next; Node(int x) { val = x; next = null; }}
Two famous 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
The tree here usually refers to a binary tree. each node contains a left child node and a right child node, as shown below:
class TreeNode{ int value; TreeNode left; TreeNode right;}
The following are some tree-related concepts:
- Balance vs. imbalance: in a balanced binary tree, the depth of left and right subtrees of each node is mostly 1 (1 or 0 ).
- Full binary tree (Full Binary Tree): Each node except the leaf node has two children.
- Perfect binary tree (Perfect Binary Tree): Is a full binary tree of the following nature: all leaf nodes have the same depth or layer, and each parent node must have two children.
- Complete binary tree (Complete Binary Tree): In a binary tree, each layer may be completely filled except the last one, and all nodes must be left-aligned as much as possible.
Note: perfect binary trees are also known as full binary trees. An example of a perfect binary tree is an ancestor map given by a person in a given depth, because each person must have two parent trees. A complete binary tree can be seen as a perfect binary tree with several extra left-leaning leaf nodes. Q: What is the difference between a perfect binary tree and a full binary tree? (Reference: http://xlinux.nist.gov/dads/HTML/perfectBinaryTree.html)
4. figure
Graph-related problems mainly involve depth first search and breath first search ).
The following is a simple implementation of image breadth-first search.
1) define 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) use Queue for breadth-first search
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:
12 |
value: 2 value: 3 value: 5 Find value: 5 value: 4 |
5. sort
The following describes the time complexity of different sorting algorithms. you can go to wiki to view the basic ideas of these algorithms.
Algorithm |
Average Time |
Worst Time |
Space |
Bubble sorting |
N ^ 2 |
N ^ 2 |
1 |
Select 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, there are some implementations/Demos ::? Counting sort, Mergesort ,? Quicksort ,? InsertionSort.
- 7 Common sorting algorithms for visual perception
- Video: 6-minute demonstration of 15 sorting algorithms
6. recursion vs. iteration
For programmers, recursion should be an inherent idea (a built-in thought), which can be illustrated by a simple example.
Question: There are n steps. there are only one or two steps at a time. how many steps are there.
Step 1: find the relationship between the first n steps and n-1-1 steps.
In order to walk n steps, there are only two ways: 1 step from N-1 steps to climb or 2 steps from the n-2 step. If f (n) is the number of methods that climb to step n, f (n) = f (n-1) + f (n-2 ).
Step 2: Make sure that 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 n, because there are many redundant computations, as shown below:
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)
The direct idea is to convert recursion into 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;}
In this example, Iteration takes less time. you may also want to see Recursion vs Iteration.
7. Dynamic planning
Dynamic planning is a technology that solves the following problems:
- A problem can be solved through a solution to the problem. (Note: the optimal solution includes the optimal solution of the subproblem, that is, the optimal sub-structure ).
- Some subproblems may need to be resolved multiple times (note: This is the overlapping nature of subproblems ).
- The subproblem is stored in a table, so that each subproblem is calculated only once.
- Additional space is required to save time.
The climbing problem is completely in line with the above four features. Therefore, it can be solved 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 operation
Bitwise operator:
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 |
Obtain the I-th digit of the given number n: (I starts counting from 0 and starts from the right)
Public static boolean getBit (int num, int I) {int result = num & (1 <
For example, to obtain the 2nd digits of 10:
I = 1, n = 10
1 <1 = 10
1010 & 10 = 10
10 is not 0, so return true;
9. Probability problems
Solving probability-related problems usually requires a good planning and understanding (formatting the problem). here is a simple example of such problems:
If there are 50 people in a room, what is the probability of two people having the same birthday? (Ignore the leap year fact, that is, 365 days a year)
The probability of computing certain things can often be converted to calculating the relative surface first. In this example, we can calculate the probability that all people have different birthdays, that is, 365/365*364/365*363/365 *... * (365-49)/365, so that at least two people have the same probability of birthdate as 1.
Public static double caculateProbability (int n) {double x = 1; for (int I = 0; I
CalculateProbability (50) = 0.97
10. arrange and combine
The difference between combination and arrangement lies in whether the order is critical.
If you have any questions, please comment below.
Reference/recommendation materials:
1 .? Binary tree
2 .? Introduction to Dynamic Programming
3 .? UTSA Dynamic Programming slides
4 .? Birthday paradox
5. Cracking the Coding Interview: 150 Programming Interview Questions and Solutions, Gayle Laakmann McDowell