Highlights of typical interview programming questions for famous enterprises [-40]

Source: Internet
Author: User

31. Delete the linked list node in O (1) Time

Question: Given the head pointer and a node pointer of the linked list, delete the node at O (1) time. The linked list node is defined as follows:

Analysis: This is a widely used Google interview question. It can effectively evaluate our basic programming skills and evaluate our response speed,

More importantly, we can also examine our understanding of time complexity.

void DeleteNode(listNode*& head , listNode* node){if(head==NULL || node==NULL)return;if(node->next!=NULL){listNode* nextNode = node->next;node->next = nextNode->next;node->value = nextNode->value;delete nextNode ;}else if(head==node){delete head;head = NULL;}else{listNode* helper = head;while(helper->next!=node)helper = helper->next;helper->next = NULL;delete node;}}

32. Find two numbers that appear only once in the array
Question: except two numbers in an integer array, the other numbers appear twice.
Write a program to find the numbers that appear only once. The time complexity is O (n) and the space complexity is O (1 ).

Analysis: This is a novel interview question about bit operations.

void FindNumbers(int* data, int length,int& num1,int& num2){if(data==NULL || length<2)return;int result = 0;for(int i=0;i!=length;++i)result^=*(data+i);int helper = 1;while(!(helper & result))helper = helper<<1;num1 = 0;num2 = 0;for (int i=0;i!=length;++i){if(helper & *(data+i))num1^= *(data+i);elsenum2^= *(data+i);}}

33. Find the first public node of the linked list
Question: two one-way linked lists to find their first public node.

Analysis: This is a Microsoft interview question. Microsoft is very fond of linked list related topics,
Therefore, in Microsoft's interview questions, the probability of a linked list appearing is quite high.

listNode* FindFirstSameNode(listNode* head1,listNode* head2){if(head1 == NULL || head2 == NULL)return NULL;int num1=0;int num2=0;listNode* helper1 = head1;listNode* helper2 = head2;while (helper1!=NULL){++num1;helper1 = helper1->next;}while (helper2!=NULL){++num2;helper2 = helper2->next;}int diffenece = num1 - num2; if(diffenece>=0){for(int i = 0; i!=diffenece;++i)head1 = head1->next;}else{for(int i = 0; i!=-diffenece;++i)head2 = head2->next;}while (head1->next != head2->next){head1 = head1->next;head2 = head2->next;}return head1->next;}

34. delete a specific character from the string
Question: enter two strings to remove all the characters from the first string.

For example, input "they are students." and "aeiou ",

Then, the first string after deletion becomes "Thy R stdnts .".

Analysis: This is a Microsoft interview question. In Microsoft's Frequently Asked Questions, string-related questions account for a large part,
Because writing program operation strings can reflect our basic programming skills.

void DeleteChars(char* str, char* chars){if(str == NULL || chars == NULL)return;char hashTable[256]={0}; for (char* ch = chars;*ch!='\0';++ch)++hashTable[*ch];for (char* ch = str;*ch!='\0';++ch){if(!hashTable[*ch])*str++ = *ch;}*str = '\0';}

35. Looking for ugly data
Question: The numbers that only contain factors 2, 3, and 5 are called the ugly number ). For example, the numbers 6 and 8 are ugly,
But 14 is not because it contains factor 7. In habits, we regard 1 as the first ugly number.
Calculate the number of ugliness in the ascending order.

Analysis: This is a widely used interview question on the Internet. It is said that Google has used this question.

int FindUgly(int n){if(n<1)throw exception("invalid parameters!");int* uglyNumbers = new int[n];uglyNumbers[0] = 1;int nextUglyIndex = 1;int* Multiply2 = uglyNumbers;int* Multiply3 = uglyNumbers;int* Multiply5 = uglyNumbers;while (nextUglyIndex!=n){int minNum = min(*Multiply2*2,min(*Multiply3*3,*Multiply5*5));uglyNumbers[nextUglyIndex]=minNum;while(*Multiply2*2<=minNum)++Multiply2;while(*Multiply3*3<=minNum)++Multiply3;while(*Multiply5*5<=minNum)++Multiply5;++nextUglyIndex;}int ret = uglyNumbers[n-1];delete[] uglyNumbers;return ret;}

36. Output 1 to the maximum n digits
Question: Enter the number N and output the n-digit 10-digit number from 1 in sequence. For example, input 3,

The output values are 1, 2, 3, and the maximum 3-digit value is 999.
Analysis: This is a very interesting question. It looks very simple, but there are actually a lot of xuanjicang in it.

// The large void printnums (int n) {If (n <1) return; int * Nums = new int [n + 1]; for (INT I = 0; i! = N + 1; ++ I) * (Nums + I) = 0; * Nums = 1; while (* (Nums + n) = 0) {// output large number bool startflag = false; For (INT I = n-1; I >=0; -- I) {If (startflag) cout <* (Nums + I ); else if (* (Nums + I )! = 0) {cout <* (Nums + I); startflag = true ;}} cout <Endl; // If (* nums! = 9) ++ * Nums; else {int * helper = Nums; while (* helper = 9) {* helper = 0; ++ helper ;} + + * helper ;}}}

37. Rotate the smallest element in the array
Question: Moving the first several elements of an array to the end of an array is called the rotation of an array. Enter a rotation of an sorted array,

The minimum element of the output rotating array. For example, if an array {3, 4, 5, 1, 2} is a rotation of {1, 2, 3, 4, 5}, the minimum value of this array is 1.

Analysis: The most intuitive solution to this question is not difficult. You can traverse the array once from start to end to find the smallest element,
The time complexity is obviously O (n ). However, this idea does not take advantage of the input array feature. We should be able to find a better solution.

int findMin(int* theArray,int length){if (theArray==NULL||length<=0)throw std::exception("invalid parameters!");findMin(theArray,theArray+length-1);}int findMin(int* start,int* end){int endNum = *end;int midNum = *(start+(end-start)/2);if(start!=end){if(midNum<=endNum)findMin(start,start+(end-start)/2);elsefindMin(start+(end-start)/2+1,end);}elsereturn *start;}

38. Integer power of a value

Question: implement the function Double Power (double base, int exponent) to calculate the power of base's exponent.
Overflow is not required.

Analysis: this seems simple. Some people may write the following code 30 seconds after they see the question:
Double Power (double base, int exponent)
{

Double result = 1.0;
For (INT I = 1; I <= exponent; ++ I)
Result * = base;
Return result;
}

// You need to consider more double power (double base, int exponent) {If (exponent = 0) return 1; double result = 1; int abexponent = exponent> 0? Exponent:-exponent; For (INT I = 0; I! = Abexponent; ++ I) Result * = base; If (exponent> 0) return result; else if (base = 0) Throw exception ("unvalid parameters! "); Elsereturn 1/result ;}

39. Maximum length of a symmetric string

Question: enter a string and output the maximum length of the symmetric substring in the string.
For example, if the input string is "google", output 4 is because the longest symmetric substring in the string is "Goog.

Analysis: many people may have written a function to determine whether a string is symmetric. This question can be viewed as an enhanced version of the function.

int GetLongestSymmetricalStr(char* str){if(str == NULL)return 0;int length = 1;char* pChar = str;while(*pChar != '\0'){// Substrings with odd lengthchar* pFirst = pChar - 1;char* pLast = pChar + 1;while(pFirst >= str && *pLast != '\0' && *pFirst == *pLast){pFirst--;pLast++;}int newLength = pLast - pFirst - 1;if(newLength > length)length = newLength;// Substrings with even lengthpFirst = pChar;pLast = pChar + 1;while(pFirst >= str && *pLast != '\0' && *pFirst == *pLast){pFirst--;pLast++;}newLength = pLast - pFirst - 1;if(newLength > length)length = newLength;pChar++;}return length;}

40. More than half of the numbers appear in the array

Question: If a number in the array appears more than half the length of the array, find the number.

Analysis: This is a widely used interview question. Many companies, including Baidu, Microsoft, and Google, have
I have used this question. It takes tens of minutes to answer this question,
In addition to good programming ability, quick response and strong logical thinking are also required.

int FindNum(int* data,int length){if(data==NULL || length<1)return -1;int num = *data;int times = 1;for(int i=1;i!=length;++i){if(num==*(data+i))++times;else{--times;if(times==0){num = *(data+i);++times;}}}return num;}

Welcome to my independent technical blog | sameideal.com

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.