当前位置:网站首页>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
边栏推荐
猜你喜欢

Two forms of softmax cross entropy + numpy implementation

Common components of solder pad (2021.4.6)

Svg -- loading animation

C language force buckle question 61 of the rotating list. Double ended queue and construction of circular linked list
![[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)](/img/fe/8c707c30c734de7bb76ea68134842c.png)
[hands on deep learning] environment configuration (detailed records, starting from the installation of VMware virtual machine)

Won't you just stick to 62 days? Sum of words

Back propagation process of manual BP neural network

Implementation of jump connection of RESNET (pytorch)

不会就坚持61天吧 最短的单词编码

SVG--loading动画
随机推荐
Definition and implementation of stack and queue (detailed)
Fu Yingna: Yuan universe is the new generation of Internet!
10. Fallback message
全屋WiFi方案:Mesh路由器组网和AC+AP
Machine vision series 3:vs2019 opencv environment configuration
LCA board
Multi card training in pytorch
Record of problems encountered in ROS learning
不会就坚持60天吧 神奇的字典
Wechat applet parameter transfer
kotlin的List,Map,Set等集合类不指定类型
Integration of Nan causes in pytorch training model
Sequence list and linked list
Why do I delete the original record (OP d) and then add a new one in Kafka when I update MySQL data
Copy products with one click from Taobao, tmall, 1688, wechat, jd.com, Suning, taote and other platforms to pinduoduo platform (batch upload baby details Interface tutorial)
Pytoch distributed training
Is there any way for Youxuan database to check the log volume that the primary cluster transmits to the standby cluster every day?
Common components of solder pad (2021.4.6)
View partition table format
Pytorch GPU and CPU models load each other