Interview question: Java interview experience!=!= not seen

Source: Internet
Author: User

2018.03.09 Shenzhen le Wei Technology

I looked at the feeling that the company looks pretty good, interview my husband is also very good, the atmosphere should be very good, but I lack of strength, alas, successive efforts, the following interview in the deep impression of the three questions to write about.

Interview Question 1: The database deletes duplicate data, and only one of multiple duplicate data is retained
Table name T

a b C
1 222 333
2 222 333
3 222 333

If the data in the A field is different for each row, SQL is as follows:(keep a field with the smallest data, if you want to keep the big one, say min (a) to Max (a))

DELETE FROM tt WHERE NOT EXISTS( SELECT * FROM ( SELECT *, MIN(a) AS id FROM tt GROUP BY b HAVING COUNT(*) >= 1 ) e WHERE e.id = tt.a)
    • 1
    • 2
    • 3
    • 4
    • 5

PS: Above is the use of MySQL  
There is another way to use the database, the following SQL put in MySQL run will error, it seems that only MySQL will error, error message is: [ERR] 1093-you Can ' t specify target table ' TT ' for update in FROM clause error means that you cannot select some of the values in the same table first, and then update the table (in the same statement). I was going to tell you. Select out of the outside to set a layer of select, but the logic of this statement does not seem to set a layer of select, after the set will have a syntax error;

DELETE Span class= "Hljs-keyword" >from tt where b IN (select b from tt group by b having COUNT (*) >1) and a not  In (select min (a) FROM tt group by b having count (*) >1)         
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

Above is a value of a different case, if the value of each row A is the same? Seemingly can use line number to mark, concrete also need in research research, not to be continued ~

Interview Question # 2: How to allocate the money each person gets (to steal the Red envelope algorithm)

@param total Red envelope balance@param rest Red Envelopes remaining number-1 (Subtract the number of this time first)PrivateLongNextmoney (Long Total,Long rest) {if (Rest <0 | | Total < 0) {// If rest or total is less than 0, throw an exception, you can write an exception handling class to inherit runtimeexception throw ...} //If rest is 0, indicating that this is the last red envelope, return the balance directly if (rest = 0) { Span class= "Hljs-keyword" >return total; } //take the balance average (to add the number of this time) long per = total/(rest + 1); //the random number of 1 to the average of the balance (mathematical expectation?) ) long result = Randomutils.nextlong (1, per * 2); //if the result is large enough to meet the remaining number of each minimum of 1 of the requirements, leaving the minimum balance, the remaining return if (Total- Result < rest) {result = Total-rest;} return result;}            
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st

Interview Question 3: Why use beans, what benefits or advantages?
What I'm answering is decoupling ah, using more flexible Ah,,, Σ (° °| | |) ︴, see the interviewer in helpless shook his head,,,, ╥﹏╥ ...
Then I looked for it on the internet, saying that I didn't find the answer to the question, Khan, I will continue to look for.

Interview question: Java interview experience!=!= not seen

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.