Following part 1 , I would like to continue the interview story in Silicon Valley. After that failure, the unit continued to take the second candidate to interview. Unexpectedly, when Director of Software engineer used the same question set last time. That is a situation that is not taken into account, because everyone thinks that he will ask another question. The result is that everyone knows, again
Don’t skip Part 1:
After a series of expert commentaries, the most accurate answer can be found
Question 1: Write code example of Singleton class
Actually there are several types of Singleton class code in Java, of which there are 2 common types
Early Instantiation :
Create an instance at the time of class loade time
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Singleton { // singleton instance, this instance is created in JVM during start of the application // which is early loading private static final Singleton singletonInst = new Singleton (); // making constructor private so that không có thể được khác số của mặc định private Singleton () { } // the method which gives access to chỉ chỉ singtel public static Singleton getInstance () { return singletonInst; } } |
Lazy Loading
Instance is created when Singleton class is used for the first time, as the code presented in part 1
It is necessary to present a multi-threaded case as part 1. The skirt of the skirt must analyze each case, assess the advantages and disadvantages
And 1 Singleton can be called as man standard temporarily as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Singleton { private static volatile Singleton self; private Singleton () { if (self! = null) { throw new UnsupportedOperationException ("Use getInstance ()"); } } public static synchronized Singleton getInstance () { if (self == null) { self = new Singleton (); } return self; } } |
The most important is this question, which determines the fail or pass
Is there a case where Singleton has created multiple instances?
The correct answer here is YES
Java has a concept of Reflection , an API that allows checking and modify behavior of methods, class at runtime
This reflection can cause the destroy of the singleton property of the singleton class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
// Java code to interpret effect of Reflection // on Singleton property import java.lang.reflect.Constructor; // Singleton class class Singleton { // public instance initialized when loading the class public static Singleton instance = new Singleton (); private Singleton () { // private constructor } } public class GFG { public static void main (String [] args) { Singleton instance1 = Singleton.instance; Singleton instance2 = null; try { Constructor [] constructors = Singleton.class.getDeclaredConstructors (); cho (constructor Constructor: constructors) { // Below code will destroy the singleton pattern constructor.setAccessible (true); instance2 = (Singleton) constructor.newInstance (); break; } } catch (Exception e) { e.printStackTrace (); } System.out.println ("instance1.hashCode (): -" + instance1.hashCode ()); System.out.println ("instance2.hashCode (): -" + instance2.hashCode ()); } } |
Leading to the destroyed singleton and creating two different objects belonging to the sample class
1 2 3 |
Output: - instance1.hashCode (): - 366712642 instance2.hashCode (): - 1829164700 |
There is also a case where serialize / desterilize object also occurs
https://gurunh.com/2018/05/singleton-co-thuc-su-de/
Quote the original sentence in Head First Design Patterns
Hãy giữ lại IF bạn đang dùng nhiều bộ nạp ảnh ; này không thể gỡ bỏ tiến trình Singleton và kết quả trong nhiều instances.
Question 2: Coding
Write reverse character string
In addition to using element-inverting loops as part 1 , recursive algorithms can be used
1 2 3 4 5 6 7 8 9 10 11 |
public static int [] reverseArray (int [] a, int left, int right) { // Tail Recursion. if (left <right) { // swap elements a [i], a [j] int temp = a [left]; a [left] = a [right]; a [right] = temp; reverseArray (a, left + 1, right-1); } return a; } |
The simpler code does not have to be repeated, but I do not think recursion is the most optimal because in the recursive function will be called multiple times and the stack memory will be superimposed with easily cause data about memory
Post code number 2
For positive integer n, find the number of ways to divide n into 5 smaller positive numbers so that it is still n
For example, n = 5 then output 1 because there is only 1 case (1, 1, 1, 1, 1)
N = 12 then there are 7 ways
Thanks to the help of Guc Go, it was discovered that behind this problem is a sky of mathematical knowledge
https://www.mathpages.com/home/kmath556/kmath556.htm
After reading it, I feel stupid
Great type of formula to find this way. N is a positive number to divide, K is a positive number <N
Briefly replace the type of farmers like this.
General statement: give candy to k babies, ask how many ways? (n, k positive)
Summary of solutions:
- Evenly spread n candies to the table, notice there are n – 1 space between the candies
- We have k babies -> need to insert k-1 partition to produce k spaces -> the answer is convolution (k-1) of (n-1), or (n-1) C (k-1 )
N = 7, k = 5 according to the convolutional combination, there are 15 specific ways:
{2, 2, 1, 1, 1}
{2, 1, 2, 1, 1}
{2, 1, 1, 2, 1}
{2, 1, 1, 1, 2}
{1, 2, 2, 1, 1}
{1, 2, 1, 2, 1}
{1, 2, 1, 1, 2}
{1, 1, 2, 2, 1}
{1, 1, 2, 1, 2}
{1, 1, 1, 2, 2}
(Actually there is only 1 way 2 numbers 2 and 3 number 1)
{3, 1, 1, 1, 1}
{1, 3, 1, 1, 1}
{1, 1, 3, 1, 1}
{1, 1, 1, 3, 1}
{1, 1, 1, 1, 3
(Actually there is only one way is 1 number 3 and 4 number 1)
However, because the problem does not care to order, it is necessary to add steps to eliminate the same results
Question 3: Question about system read data 99%, 1% write, what kind of data should you choose
The answer is ArrayList as part 1 , but in the case of multithreading, you can still use ArrayList but not share this list between threads. In short, handle with care, goods are easy to use
Or another answer as follows (Please advise of a Java player)
If it is a single-thread environment, using ArrayList is better than LinkedList in this case (especially when the list is relatively large) because ArrayList is random access while the LinkedList is not.
In return, adding, removing elements on the ArrayList will be worse in performance than LinkedList (due to having to handle the backed array). Of course, the performance of the system cannot be based entirely on qualitative calculations, but should be based on monitor tools with specific numbers.
If the multi-thread environment should use CopyOnWriteArrayList, the reason is that it ensures thread-safe during the implementation of the read () function in the CopyOnWrite model it does not require the use of lock.
However, this question should be asked back because it lacks the necessary information to get the correct answer
Original proof of a senior
Read what is read, write is write ntn, retort always has plus points. The problem for that cannot be enough to judge. For example, read random read more array, but sequential read the same. Write too, if write at the end, the array is the fastest. Advanced slashing, if the number of extremely large elements and sequential read, the array will have a clear advantage because the memory is allocate contiguous and not fragment like the linked list. So it fits into the cache line, because this bottle neck is mainly in memory, and if fragmenting the cache, it will fly a few hundred cycles right away.
Question 4: Do you have a service expose API for the client, if you use GET api call service via HTTPS, do you feel secure?
In case of sensitive data must include in GET request, it will violate security best practices. Full URL query parameters (including query parameters) of GET requess will be logged in server logs and there is a risk of unauthorized access. In the case of data on the query parameters should not appear in logs, you should use POST instead of GET
OK, so after collecting all the necessary information, the team builds into a set of interview questions including questions and answers. It’s the same type of university entrance exam.
The next job was to give the title to a third young man for an interview, to pray for the old man to ask the right question
The third candidate has quite good English, the topic, the answers have been handed over and read first. Unlike the previous one, this time there is a full board of leaders present. Because determined this time, it is considered as losing the project, you hang nieu.
He did his best, and waited for candidate’s wind-cutting skills. I heard that the manager also went to Ba Vang temple to worship more than two dozen tubers for relief
The clock moves gradually until the interview time is 9:30, everyone is waiting …
Director of Software engineering appeared, a gentle smile.
The first question
Question 1) Write example code for Singleton class
Question 2) Write code to reverse the chain …
Question 3) ….
It is true that previous lifetimes are good, heavenly Buddha blesses maintenance, the questions are the same. The result is clear …
Emotion burst, it was old silver with lipstick. This time, I was able to read how to interview and not to trick me
Happy Ending
But now I still can’t think about it, why is the tool sharing a set of questions for an interview?
Conclude
The old people said that “ The loser is the one who gives up”, the winner is the one who does not give up . Sometimes being successful requires a bit of luck. But luck is only available when you try your best. The success of this person may have to be exchanged with the contribution and sacrifice of the collective, but all for the common purpose.