当前位置:网站首页>Coding questions encountered in the interview
Coding questions encountered in the interview
2022-07-29 04:21:00 【Deluxe big bowl tea】
Design class
- Use Redis Realize a one minute lottery for a user 3 Time
- Write a singleton pattern that you think is the best , Do you think there is any problem with this writing ?
- Several ways to write the singleton pattern
- Hungry Chinese style
- Slacker type 、 Double check lock
- Static inner class
- Several ways to write the singleton pattern
public class Singleton {
private Singleton(){
}
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
Enumeration class
public enum SingletonEnum {
INSTANCE(1, "aa");
SingletonEnum(int value, String v) {
}
}
- How to destroy the singleton mode
- The strategy pattern demo
my github Upper demo - Factory mode demo
Design patterns demo - Write an observer pattern demo
demo - Template method demo
Algorithm problem
- Sort
- Bubble sort
- Merge sort
- Selection sort
- Insertion sort
- Quick sort
public int[] sortArray(int[] nums) {
if (nums == null) {
return nums;
}
quickSort(nums, 0, nums.length - 1);
return nums;
}
public void quickSort(int[] numbers, int left, int right) {
if (right <= left) {
return;
}
int pivot = numbers[left];
int start = left;
int end = right;
while (start < end) {
while (start < end && pivot <= numbers[end]) {
end--;
}
swapArr(numbers, start, end);
while (start < end && numbers[start] <= pivot) {
start++;
}
swapArr(numbers, start, end);
}
quickSort(numbers, left, start - 1);
quickSort(numbers, start + 1, right);
}
private void swapArr(int[] numbers, int left, int right) {
int temp = numbers[left];
numbers[left] = numbers[right];
numbers[right] = temp;
}
- The first, middle and last order traversal of the tree
/* Depth first */
/** * The first sequence traversal * @param root * @param linkedList */
public static void preOrder(TreeNode root, List<TreeNode> linkedList) {
if (root != null) {
linkedList.add(root);
preOrder(root.left, linkedList);
preOrder(root.right, linkedList);
}
}
/** * In the sequence traversal * @param root * @param linkedList */
public static void inOrder(TreeNode root, List<TreeNode> linkedList) {
if (root != null) {
inOrder(root.left, linkedList);
linkedList.add(root);
inOrder(root.right, linkedList);
}
}
/** * After the sequence traversal * @param root * @param linkedList */
public static void postOrder(TreeNode root, List<TreeNode> linkedList) {
if (root != null) {
postOrder(root.left, linkedList);
postOrder(root.right, linkedList);
linkedList.add(root);
}
}
- Restore the binary tree to the result of preorder and inorder traversal
- Fiboracci series
- Two points search
public int search(int[] nums, int target) {
if (nums == null) {
return -1;
}
int low = 0;
int high = nums.length - 1;
while (low <= high) {
int middle = (low + high)/2;
if (nums[middle] == target) {
return middle;
} else if (nums[middle] < target) {
low = middle + 1;
} else {
high = middle - 1;
}
}
return -1;
}
- Single list flip
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = pre;
pre = curr;
curr = next;
}
return pre;
}
- The number of occurrences of numbers in an array
- Sum of two numbers 2sum
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int temp = target - nums[i];
if (map.containsKey(temp)) {
return new int[]{
map.get(temp), i};
}
map.put(nums[i], i);
}
return null;
}
- Determine if the list has links
// Basic Edition Advanced version can use speed pointer
public ListNode detectCycle(ListNode head) {
Set<ListNode> nodeSet = new HashSet<ListNode>();
ListNode item = head;
int index = 0;
while (item != null) {
if (nodeSet.contains(item)) {
return item;
}
nodeSet.add(item);
item = item.next;
}
return null;
}
- Merge two ordered arrays
// Double pointer
public void merge(int[] nums1, int m, int[] nums2, int n) {
int[] temp = new int[nums1.length];
int mp = 0, np = 0;
for (int i = 0; i < temp.length; i++) {
if (mp < m && np < n) {
if (nums1[mp] < nums2[np]) {
temp[i] = nums1[mp++];
} else if (nums2[np] < nums1[mp]) {
temp[i] = nums2[np++];
} else {
temp[i++] = nums1[mp++];
temp[i] = nums2[np++];
}
continue;
}
if (np < n) {
temp[i] = nums2[np++];
continue;
}
if (mp < m) {
temp[i] = nums1[mp++];
}
}
if (m >= 0) System.arraycopy(temp, 0, nums1, 0 ,temp.length);
}
- Merge two arrays
You can sort it first , After the merger - Palindrome number
Turn it over first , To determine - Flip integers
public int reverse(int x) {
long result = 0;
while (x != 0) {
result = result*10 + x%10;
x = x/10;
}
// Pay attention to the judgment of crossing the boundary
return result > 0x7fffffff || result < 0x80000000 ? 0 : (int)result;
}
- Delete the last from the list n Elements
public ListNode removeNthFromEnd(ListNode head, int n) {
int index = - n;
ListNode item = head;
while (item != null) {
item = item.next;
index++;
}
ListNode pre = null;
ListNode curr = head;
while (curr != null) {
if (index-- == 0) {
if (pre == null) {
return curr.next;
}
pre.next = curr.next;
break;
}
pre = curr;
curr = curr.next;
}
return head;
}
- Square , There are altogether three roads from the upper left corner to the lower right corner
- Two ordered arrays , The first n Large number
- One length n Array of , front k(k<n) Ascending order , Descending order after
- Find the maximum depth of a binary tree
边栏推荐
- Why is it necessary to scale the attention before softmax (why divide by the square root of d_k)
- 全屋WiFi方案:Mesh路由器组网和AC+AP
- The data source is SQL server. I want to configure the incremental data of the last two days of the date field updatedate to add
- Opengauss pre check installation
- RMAN do not mark expired backups
- Whole house WiFi solution: mesh router networking and ac+ap
- C语言力扣第61题之旋转链表。双端队列与构造循环链表
- pyscript无法引入包
- String, array, generalized table (detailed)
- Don't stick to it for 68 days. Baboons eat bananas
猜你喜欢
不会就坚持62天吧 单词之和
Deep learning training strategy -- warming up the learning rate
[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)
Visio draw grid
Beginner: array & String
No, just stick to it for 64 days. Find the insertion location
Don't insist on 66 days. Weight generates random numbers
SVG--loading动画
Applet: Area scrolling, pull-down refresh, pull-up load more
Not for 58 days. Implement prefix tree
随机推荐
不会就坚持68天吧 狒狒吃香蕉
异常解决:cococaption包出现找不到edu.stanford.nlp.semgraph.semgrex.SemgrexPattern错误
Don't stick to it for 68 days. Baboons eat bananas
Dabao and Erbao
[kvm] common commands
[material delivery UAV] record (ROS + Px4 + yolov5 + esp8266 + steering gear)
15.federation
不会就坚持59天吧 替换单词
Visio draw grid
Exception handling: pyemd or pyemd not found
The third ACM program design competition of Wuhan University of Engineering
String, array, generalized table (detailed)
The function "postgis_version" cannot be found when installing PostGIS
[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)
pyscript无法引入包
The data source is SQL server. I want to configure the incremental data of the last two days of the date field updatedate to add
Not for 63 days. The biggest XOR
对一个元素使用多种变形的方法
[Openstack] keystone,nova
全屋WiFi方案:Mesh路由器组网和AC+AP