preface

Also from a fan to share interview experience

This student had a summer internship in his junior year. He experienced three technical aspects and one HR aspect and landed in Tencent. The questions asked by the interviewers were also quite representative.

The interview after

  • After 3 skills +1hr, 4 rounds of interviews
  • Technical side —–6.16
  • Technical II —–6.18
  • Three sides of technology —–6.23
  • Hr side — — — — — 6.30
  • Oc — — — — — — — 7.1
  • Offer – 7.2

The interview questions

All the topics of this article is definitely not over, here to pick some more classic topics to chat with everyone.

The complete interview questions can see the “2021 Tencent JAVA post interview question” I organized,

Of course, if you are not interested in Tencent, I have also arranged the real questions of other top Internet companies. Pay attention to the public number: North Visit to learn Java, reply to the “interview” and you can receive all the interview materials I have arranged.

All right, no more nonsense, let’s get to the main text.

In accordance with the convention, I first posted the question, and then the answer, you can think about their own, can not scroll down to see the answer.

  • Deep cloning, shallow cloning, and implementation methods
  • Java object access
  • Hash conflict resolution method, Hash conflict data
  • Equals vs. hashCode
  • Why does InnoDB choose a B+ tree
  • Thinking problem, the balance weighs the small ball, in a pile of light to find a heavy, summarize the general formula
  • Topk problem scenario questions
  • Algorithm part: ① segmentation palindrome string ② handwritten LRU, and talk about the principle of the bottom of some, why use bidirectional linked list ③ RAND5 ()-> RAND7 ()

The answer key

1, deep cloning, shallow cloning, and the implementation method

Shallow cloning: The reference type variable of the object copies the reference value of the object

Deep cloning: Copies a copy of the object’s memory space to which a variable of the reference type refers

How to implement object cloning? In three steps:

  1. Object implements the Cloneable interface;
  2. Overrides the clone() method of class Object;
  3. Call super.clone() in the clone() method;
public class ShallowClone implements Cloneable{ public int id; public String name; public ShallowClone(int id, String name){ this.id = id; this.name = name; } public Object clone(){ Object sc = null; try { sc = super.clone(); } catch (Exception e) { System.out.println(e.toString()); } return sc; } public static void main(String[] args) { // TODO Auto-generated method stub ShallowClone sc1 = new ShallowClone(1, "sc1Name"); ShallowClone sc2 = (ShallowClone)sc1.clone(); System.out.println("sc1's id: " + sc1.id + "\tsc2's id: " + sc2.id); System.out.println("sc1's name: " + sc1.name + "\tsc2's name: " + sc2.name); System.out.println(sc1.name == sc2.name); System.out.println(sc1.name.equals(sc2.name)); }}

Output results:

sc1's id: 1    sc2's id: 1
sc1's name: sc1Name    sc2's name: sc1Name
true
true

2. Java object access

Handle access: The Java heap will be divided into a block of memory as a pool of handles. The reference stores the address of the object’s handle, and the handle contains the specific address information of the object’s instance and type data.

Pointer access: The reference variable directly stores the address of the object, while one part of the Java heap object stores the object instance data and the other part stores the object type data.

[Image upload failed…(image-68dcf6-1625474897121)]

These two ways of accessing objects have their own advantages. The biggest advantage of using handle access mode is that reference stores a stable handle address. When the object is moved, only the instance data pointer in the handle needs to be changed, while reference does not need to be changed.

The biggest advantage of using pointer access mode is speed, it saves a pointer location time overhead, in the case of virtual machine, it uses the second mode (direct pointer access)

3. How to resolve Hash conflicts

Although we do not want conflict, the possibility of conflict is still there. When the key value range is much larger than the length of the hash table, and the specific value of the key is not known in advance. Conflicts are inevitable.

In addition, when the actual value of a keyword is larger than the length of the hash table, and the table is already full of records, if a new record is inserted, not only will there be a conflict, but also an overflow. There are two common ways to handle conflicts and overflows

  • Open addressing
  • Zipper method

Specific will not expand to speak, not one or two words can be finished, if you are interested, you can find relevant information and blogs

4. Equals vs. hashCode

  • If equals is true for both objects, hashCode should be the same
  • If the two objects are equal to false, the hashCode should also be different. Otherwise, the hashCode should be overwritten

5. Why does InnoDB choose B+ tree

In this case, let me just mention the characteristics of B plus trees

B+ tree features:

  1. B+ trees can contain more nodes per node for two reasons. One is to lower the height of the tree. The other is to change the data range into multiple intervals, the more the interval, the faster the data retrieval. The footprint is very small, so each layer of nodes can index a wider range of data. In other words, more data can be searched per IO operation.
  2. Instead of storing just one key, each node can store multiple keys.
  3. Non-leaf nodes store keys, and leaf nodes store keys and data.
  4. The leaf nodes are linked to each other by two Pointers, so the sequential query performance is higher. The leaf nodes are pairwise, matching the read-ahead feature of the disk.

6, thinking question: the balance weighs the small ball, in a pile of light to find a heavy, summarize the general formula

This one is also easy, and you can get the answer very quickly by using the rule of thirds

  1. Divide the ball into three parts at a time (divide evenly if you can).
  2. Put the same number of two pieces on the scale, if the two pieces are the same weight, then the lighter ball must be in the third piece, then do the same 1 operation for the third piece;
  3. Otherwise, do the same 1 operation for the lighter portion.

So, y < = 3 ^ x

7, Topk problem scenario questions

This should also be a more common scene in the interview question, there are a lot of online answer blog, here to mention, do not repeat

8. Algorithm:

① Split palindrome string

This problem is also a more classic force buckle, here to everyone posted a picture, should be very good to understand

(2) the handwritten LRU

package algorithm.Interview; import java.util.HashMap; public class LRUCache { private Node firstNode; private Node lastNode; private int initialCapacity; private HashMap<String, Node> hashMap; public LRUCache(int initialCapacity) { if (initialCapacity <= 0) { throw new IllegalArgumentException("initialCapacity must > 0"); } this.initialCapacity = initialCapacity; hashMap = new HashMap<>(); } public String get(String key) { Node node = hashMap.get(key); if (node == null) { return null; } // When the element is queried, move the element to removeToToRail (node) at the end of the list; return node.value; } public void put(String key, String value) { Node node = hashMap.get(key); If (node == null) {// larger than the memory capacity, If (hashMap.Size () >= InitialCapacity) {// Remove the least used String oldKey = removeNode(firstNode); hashMap.remove(oldKey); } Node newNode = new Node(key, value); addNode(newNode); hashMap.put(key, newNode); } else { node.value = value; // Move to removeToToRail (node) at the end of the list. Private void removeNodeTail (node node) {if (node == lastNode) {return; } // Remove removeNode(node) first; // Add to the tail addNode(node); } /** * @Param Node */ private void addNode(node node) {if (lastNode! = null) { lastNode.next = node; node.prev = lastNode; } // The lastNode points to the newly inserted node lastNode = node; If (firstNode == null) {firstNode = node; } node.next = null; } /** * Removes the specified element, * @param node * @return */ private String removeNode(node node) {if (node == lastNode) {lastNode = lastNode.prev; LastNode. next =null; lastNode.next =null; } else if (node == firstNode) { firstNode = firstNode.next; FirstNode. prev = null; firstNode.prev = null; } else { node.prev.next = node.next; // The successor node of the current node's predecessor node points to the successor node of the current node, node.next. Prev = node.prev; } return node.key; return node.key; return node.key; } private class Node { Node prev; // Node next; // Subsequent node String key; String value; Node(String key, String value) { this.key = key; this.value = value; } } @Override public String toString() { StringBuilder ret = new StringBuilder(); Node p = firstNode; while (p! =null){ ret.append("key:").append(p.key).append(",value:").append(p.value).append("; "); p = p.next; } return ret.toString(); }}

(3) rand5 () – > rand7 ()

Algorithm code:

public static int random5() { return (int) (1+Math.random()*5); } public static int random7() { int a=random5(); int b=random5(); int rand=10*a+b; if(rand<14) { return 1; //11 12 13 }else if(rand<22) { return 2; //14 15 21 }else if(rand<25) { return 3; //22 23 24 }else if(rand<33) { return 4; //25 31 32 }else if(rand<36) { return 5; //33 34 35 }else if(rand<44) { return 6; //41 42 43 }else if(rand<52) { return 7; //44 45 51 } else return random7(); //52 53 54 55}

Test code:

public static void main(String[] args) { HashMap<Integer,Integer> map5=new HashMap<Integer,Integer>(); for(int i=0; i<10000000; i++) { int key = random5(); if(map5.get(key)==null) { map5.put(key, 1); }else map5.put(key, map5.get(key)+1); } System.out.println("random5:"); Map5. EntrySet (). The forEach (e - > {System. Out. Println (e + "\ t" + 1.0 * um participant etValue () / 10000000); }); System.out.println(); HashMap<Integer,Integer> map7=new HashMap<Integer,Integer>(); for(int i=0; i<10000000; i++) { int key = random7(); if(map7.get(key)==null) { map7.put(key, 1); }else map7.put(key, map7.get(key)+1); } System.out.println("random7:"); Map7. EntrySet (). The forEach (e - > {System. Out. Println (e + "\ t" + 1.0 * um participant etValue () / 10000000); }); }

Well, this article will write this, in addition to the above said these questions, there are some about TCP, thread pool and other questions, ask a very wide, after all, the interview build rocket, space limited here will not be posted out.

But don’t be disappointed, all the questions I have sorted into a “2021 Tencent JAVA post interview real questions” PDF, the back will continue to include the latest interview questions, directly click can be received

In addition to Tencent, I am also collecting and sorting out the real questions of other Dachang, and I can share them with you for free.

Students who need to pay attention to the public number: North Tour to learn Java, reply to the “interview” can receive all the interview materials I have arranged, there are massive Java system learning materials Oh!