Some interesting interview questions (Continuous updates) and interesting interview questions

Source: Internet
Author: User

Some interesting interview questions (Continuous updates) and interesting interview questions

1. Write an efficient C language program to calculate the number of 1 in an unsigned integer.

For (count = 0; x; count ++) x & = X-1; similarly, calculate the number of digits of 0: for (count = 32; x; count --) x & = X-1;


2. Given the strings S1 and S2, write a program to determine whether S2 can be rotated by S1. It is required that only one strstr system function can be called.

void main(){    char *str1 = "wang";    char *str2 = "angw";    char *tmp = (char*)malloc(2*strlen(str1) + 1);    sprintf(tmp, "%s%s", str1, str1);    if (strstr(tmp, str2))        cout<<"yes"<<endl;    getchar();}


3. What is entered in the IF condition so that the following program can print HelloWorld?

if(<condition>)              printf ("Hello");else    printf("World");

if (!printf("Hello"))    printf("Hello");else    printf("World");

4. modify or add only one character so that the following program prints 20 * characters. (There are at least three solutions)
int main(){    int i, n = 20;    for (i = 0; i < n; i--)        printf("*");    return 0;}

Solution 1: intmain () {inti, n = 20; for (I = 0; I <n; n --) printf ("*"); return0;} solution 2: intmain () {inti, n = 20; for (I = 0; I <n; I ++) printf ("*"); return0 ;}


5. Write an algorithm to reverse the word order in the string.

Example: Hi Welcome to cricode is reversed to cricode to Welcome Hi

Void main () {string str ("Hi Welcome to cricode"); stack <char> cstack; stack <char> tmp; int index = 0; for (index = 0; index <str. size (); index ++) {cstack. push (str [index]);} index = 0; while (! Cstack. empty () {if (''= cstack. top () // experiment Code, without judging the punctuation marks {while (! Tmp. empty () {str [index ++] = tmp. top (); tmp. pop () ;}str [index ++] = ''; cstack. pop ();} else {tmp. push (cstack. top (); cstack. pop () ;}} while (! Tmp. empty () {str [index ++] = tmp. top (); tmp. pop () ;}cout <str <endl; getchar ();}

6. There are two balls on the 100 floor. The ball has the following features: If you drop the ball from a floor less than X in the building, the ball will not be broken, if you drop from a floor greater than or equal to X, the ball will be broken. If you can reuse a ball that has not been broken, please give an algorithm to find the boundary floor X with the minimum number of throws.

If there are two glass balls, m is the minimum number of times to determine which layer of the building height is N. It is equivalent to finding the smallest m, making 1 + 2 +... + m> = N. Suppose N is equal to 1 + 2 +... + m, then I think the best strategy is to put the first glass ball on the m layer. If it is broken, it is clear that the remaining m-1 layers need to be tried one by one from the bottom, the worst case is m. If m is not broken, the problem is equivalent to building height N '= 1 + 2 +... + m-1) where the same problem requires the number of times m' + 1 (1 is the first attempt at the m layer), according to our recursion, it is easy to obtain that the number of times required for 'n' is exactly the same as the number of times expressed as m. We tend to focus on failure or success. In both cases, the total number of detection times is equal. Therefore, this should be the optimal algorithm. Of course, when N cannot be expressed as 1 + 2 +... + m, we can only find the minimum m as the number of tests required. As for the 100 floor, obviously m = 14, we should put the first ball at 14th, if not broken, continue at 14 + 13 = 27, if it is not broken, it will be thrown at Layer 27th + 12 = 39, and so on.

7. Cities A and B are 1000Km apart. We have 3000 bananas to ship from City A to City B. It is known that an elephant can transport up to 1000 bananas at A time, in addition, an elephant needs to eat a banana at 1Km every time. If the banana is completely eaten, the elephant can no longer go forward. How many bananas can you transport to City B at most?

Setting a turning point in 1000 is not feasible. Set two turning points A and B in 1000, that is, divide 1000 into three segments. Set the length of each segment from the starting point to the Point X1, point A to point B X2, point B to the end point X3, x1 + X2 + X3 = 1000 at, you need to go back and forth 5 times to transport 3000 of bananas. Then point A has 3000-5 * X1 bananas, via inference, 3000-5 * X1 less than or equal to 2000, so to point B 3000-5 * X1-3 * X2, this value is less than or equal to 1000, the end of the remaining 3000-5 * X1-3 * X2-X3, according to the above judgment, if all equal to the words: X1 = 200; X2 = 334 eventually remaining 532. Note: In fact, X2 takes 333. At this time, the remaining 533 are needed. In this case, you need to discard a banana.


8. Six equi-length chopsticks must be made up of four equi-edge triangles. They cannot be broken or bent.

You can use the regular triangle.


9. Exchange the values of Two integer variables without the third variable.

Solution 1: place the two integer data on the number axis. (You cannot draw a picture on the blog. this is inconvenient. You can consider adding the simple drawing function.): <span style = "color: #000099; "> ----- <span style =" color: # FF0000; ">|</span> ----------- <span style =" color: # FF0000; ">|</span> ------> </span> a <span style =" color: # FF0000; "> <-- B-a --> </span> B is equal to a = B-a. // a is the distance between a and B. B = B-a/B minus. distance between and between B, equal to the value of a = B + a // The distance between B and a is equal to the value of B. solution 2: according to the difference or operator nature, a = a ^ B: a = a ^ B // a' = a ^ B, B '= bb = B ^ a // B' =, a' = a ^ ba = B ^ a // B '= a, A' = B solution 3: pointer operation, same principle 1


10. If the comparison operator is not used to calculate the Greater two numbers

Principle of digital axis

# Define max (a, B) (a) + (B) + abs (a)-(B)/2)


11. Shuffling Algorithm

Solution 1

A. Define a 54-integer array poker [54] with a value of 1 ~ 54

B. generate two random numbers: m = rand () % 54 + 1, n = rand () % 54 + 1

C. Exchange the values of pocker [m] And pocker [n]

D. Repeat B and c operations N times. The larger the N value, the larger the average value, but the efficiency needs to be considered.


Solution 2

Use a set, such as vector or list

Vector pocker; // a poker without shuffling, in the order of 1 ~ 54 vector orderd; // The shuffled poker set. The initial value is empty while (pocker. size () {p = rand () % pocker. size (); orderd. push_back (pocker [p]); pocker. erase (pocker. begin () + p );}



Which of the following questions will be answered?

It's too long. Typing is very tiring!
 
Interesting interview questions

There are three answers to this question:
RMB 1 and RMB 2 respectively earned RMB 2 for two times.
2, 3 yuan, this person initially spent 8 yuan, and finally took back 11 yuan, the difference between the two, for 3 yuan.
3.-2 RMB. The minimum purchase cost and maximum selling price of a person are 8 yuan and 11 yuan respectively. In the first transaction, this person sold at 9 yuan and lost 2 yuan, the second purchase was two yuan higher than the lowest cost, so there was a loss of two yuan, a total loss of 4 yuan. In the two transactions, he made a profit of 2 yuan, so he eventually lost 2 yuan.

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.