9 big algorithm concepts for programming interviews
The following are the top 9 algorithm-related concepts that often appear in programming interviews. I will explain these concepts with simple examples to help you get a better understanding. Since mastering these topics requires more effort, this article serves as an introduction. The content is presented from a Java perspective and includes the following topics:
1. String
2. Linked List
3. Tree
4. Graph
5. Sorting
6. Recursion vs. Iteration
7. Dynamic Programming
8. Bit Manipulation
9. Probability Problems
**String**
When working with strings in Java, it's important to understand some basic operations. If your IDE does not support auto-completion, remember the following methods:
- `toCharArray()` – Converts a string into a character array.
- `Arrays.sort(char[] a)` – Sorts a character array.
- `Arrays.toString(char[] a)` – Converts a character array back into a string.
- `charAt(int index)` – Retrieves the character at a specific position.
- `length()` – Gets the length of the string.
- `length` – For arrays, it gives the size.
**2. Linked List**
In Java, a linked list can be implemented using nodes. Each node contains a value and a reference to the next node.
```java
class Node {
int val;
Node next;
Node(int x) {
val = x;
next = null;
}
}
```
Linked lists are commonly used to implement data structures like stacks and queues.
**Stack**
A stack follows the LIFO (Last In, First Out) principle. Here’s a simple implementation:
```java
class Stack {
Node top;
public Node peek() { return top; }
public Node pop() {
if (top == null) return null;
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**
A queue follows the FIFO (First In, First Out) principle.
```java
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;
Node temp = new Node(first.val);
first = first.next;
if (last == temp) last = first;
return temp;
}
}
```
**3. Tree**
A tree is typically a binary tree where each node has a left and right child.
```java
class TreeNode {
int value;
TreeNode left;
TreeNode right;
}
```
Key tree types include:
- **Balanced Binary Tree**: The depth of left and right subtrees differs by at most one.
- **Full Binary Tree**: Every node has either 0 or 2 children.
- **Perfect Binary Tree**: All leaf nodes are at the same level, and every parent has two children.
- **Complete Binary Tree**: All levels except the last are completely filled, and all nodes are as far left as possible.
**4. Graph**
Graph problems often involve traversal algorithms such as BFS (Breadth-First Search) and DFS (Depth-First Search).
Here’s a simple BFS implementation using a queue:
```java
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; }
}
```
Implementing BFS:
```java
public static void breadthFirstSearch(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);
}
}
}
}
```
**5. Sorting**
Different sorting algorithms have varying time complexities. Common ones include:
- Bubble Sort – O(n²)
- Quick Sort – O(n log n) average case
- Merge Sort – O(n log n)
- Insertion Sort – O(n²)
You can find detailed explanations on Wikipedia.
**6. Recursion vs. Iteration**
Recursion is a powerful tool for solving problems that can be broken down into smaller subproblems.
Example: Climbing stairs problem.
If you can take 1 or 2 steps at a time, the number of ways to reach step n is f(n) = f(n-1) + f(n-2).
Recursive approach:
```java
public static int f(int n) {
if (n <= 2) return n;
return f(n - 1) + f(n - 2);
}
```
Iterative approach:
```java
public static int f(int n) {
if (n <= 2) return n;
int first = 1, second = 2;
for (int i = 3; i <= n; i++) {
int third = first + second;
first = second;
second = third;
}
return second;
}
```
**7. Dynamic Programming**
Dynamic programming is used for problems that can be divided into overlapping subproblems. It stores results to avoid redundant computations.
Example: Fibonacci sequence with memoization:
```java
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);
return A[n];
}
```
**8. Bit Manipulation**
Bitwise operators are essential for low-level programming. Common operators include:
- `|` – OR
- `&` – AND
- `^` – XOR
- `<<` – Left shift
- `>>` – Right shift
- `~` – NOT
To check the ith bit of a number:
```java
public static boolean getBit(int num, int i) {
int result = num & (1 << i);
return result != 0;
}
```
**9. Probability Problems**
Probability questions require careful planning. For example, calculating the probability that at least two people share a birthday in a group of 50:
```java
public static double calculateProbability(int n) {
double x = 1.0;
for (int i = 0; i < n; i++) {
x *= (365.0 - i) / 365.0;
}
return Math.round((1 - x) * 100) / 100.0;
}
```
The result for 50 people is approximately 0.97, meaning there’s a 97% chance that at least two people share a birthday.
By running the grating ruler, the grating bars shield the light source, the light is transmitted between the grating bars and the bars, from which the number of pulses output by the receiver, a certain number of pulses are preset by the encoder, and when the number of output pulses reaches the preset value Just generate a control signal for the actuator to act. This method is generally used in CNC automatic control.
Encipheror Optical Grating,No Burr Encoder Raster,Stainless Optical Grating
SHAOXING HUALI ELECTRONICS CO., LTD. , https://www.cnsxhuali.com