Highlights of typical interview programming questions for famous enterprises [question 21-30]

Source: Internet
Author: User

21. Reverse linked list
Define a function, enter the node of a linked list, reverse the linked list, and output the header node of the inverted linked list.

listNode* ReverseList(listNode* head){if(head==NULL)return NULL;listNode* first = NULL;listNode* second = head;head = head->next;while (second!=NULL){second->next = first;first = second;second = head;if(head!=NULL)head = head->next;}return first;}

22. Search for the longest numeric string
Write a function. The original form is int continumax (char * outputstr, char * intputstr)
Function:
Find the longest consecutive numeric string in the string and return the length of the string,
And pay the longest numeric string to the memory specified by one of the function parameters outputstr.
For example, after the first address of "abcd12345ed125ss123456789" is passed to intputstr, the function returns 9,
Outputstr indicates 123456789

int continumax(char *outputstr,char *intputstr){if(outputstr==NULL || intputstr==NULL || *intputstr=='\0')return -1;char* start = NULL;int length = 0;char* startHelper = NULL;int lengthHelper = 0;for (char* ch = intputstr;*ch!='\0';++ch){if(*ch<='9' && *ch >='0'){if(startHelper==NULL)startHelper = ch;++lengthHelper;}else{if(lengthHelper>length){start = startHelper;length = lengthHelper;}startHelper = NULL;lengthHelper = 0;}}if(start!=NULL){int i = 0;for(;i!=length;++i)*(outputstr+i) = *(start+i);*(outputstr+i)='\0';return length;}elsereturn -1;}

23. Rotate the string on the left

Define the left rotation operation of the string: Move several characters before the string to the end of the string.

For example, the string abcdef is rotated two places to the left to obtain the string cdefab. Please implement the left rotation function of the string.
Requires that the complexity of the string operation with a length of N is O (n), and the auxiliary memory is O (1 ).

void Reverse(char* first,char* last){if(first==NULL || last==NULL || first-last>0)return;while (first<last){char temp = *first;*first++ = *last;*last-- = temp;}}void LeftRotate(char* str,int n){if(str == NULL || n<1)return;char* first = str;char* last =str;while(*last!='\0')++last;--last;Reverse(first,last);last-=n;Reverse(first,last);first=last+1;last+=n;Reverse(first,last);}

24. The binary value of an integer indicates the number of values in 1.
Enter an integer to calculate the number of 1 in the Binary Expression of this integer.
For example, input 10. Because the binary value is 1010 and there are two 1 s, Output 2.

Int sumofone (int n) {int K = 1; int ret = 0; For (INT I = 0; I! = 32; ++ I) {If (K & N) ++ ret; k = k <1;} return ret ;} // better solution int sumofone (int n) {int ret = 0; while (n) {+ ret; n = N & (n-1) ;}return ret ;}

25. Continuous positive number sequence for N
Input a positive number N, and output all sequences that are n continuous positive numbers.

For example, input 15, because 1 + 2 + 3 + 4 + 5 = 4 + 5 + 6 = 7 + 8 = 15, therefore, three consecutive sequences 1-5, 4-6, and 7-8 are output.
Analysis: This is Netease's interview question.

void FindContinuousSequence(int num){if(num<3)return;int start = 1;int end = 2;int sum = start + end;while (start!=end){if(sum<num){++end;sum+=end;}else if(sum>num){sum-=start;++start;}else{int temp = start;while(temp<=end)cout<<temp++<<" ";cout<<endl;++end;sum+=end;}}}

26. String Arrangement
Question: enter a string to print all the characters in the string.
For example, if the input string ABC is used, all strings that can be sorted by characters A, B, and C are output.
ABC, ACB, Bac, BCA, cab, and CBA.

Analysis: This is a good programming question for understanding recursion,
Therefore, they have frequently appeared in interviews and pen tests of major companies in the past year.

void Permutation(char* str,char* begin);void Permutation(char* str){if(str == NULL)return;Permutation(str,str);}void Permutation(char* str,char* begin){if(*begin == '\0')cout<<str<<"  ";for (char* ch = begin ; *ch!='\0';++ch){char temp  = *begin;*begin = *ch;*ch = temp;Permutation(str,begin+1);temp  = *begin;*begin = *ch;*ch = temp;}}

27. Adjust the array order so that the odd number is located before the even number.

Enter an integer array to adjust the order of numbers in the array so that all odd numbers are in the first half of the array,
All the even numbers are located in the second half of the array. The time complexity is O (n ).

void OddBeforeEven(int* data,int length){if(data == NULL || length <1)return;int* start = data;int* end = data+length-1;while (start<end){while(*start & 1)++start;while(!(*end & 1))--end;if(start<end){int temp = *start;*start = *end;*end = temp;}}}

28. Value assignment operator

Question: The cmystring-like statement is as follows:
Class cmystring
{
Public:
Cmystring (char * pdata = NULL );
Cmystring (const cmystring & Str );
~ Cmystring (void );
Cmystring & operator = (const cmystring & Str );

PRIVATE:
Char * m_pdata;
};
Implement the overload function of its value assignment operator, which requires exception security. That is, an exception occurs when an object is assigned a value, and the object state cannot be changed.

CMyString& CMyString::operator =(const CMyString& str){if (this != &str){CMyString tempStr(str);char* temp = tempStr.m_pData;tempStr.m_pData = m_pData;m_pData = temp;}return *this;}

29. Use two stacks to implement queues
Use two stacks to implement a queue. The queue declaration is as follows. Implement the two functions appendtail and deletehead,
Insert nodes at the end of the queue and delete nodes at the queue header.

template<typename T> class MyQueue{public:MyQueue(){}~MyQueue(){}void appendTail(const T&);T deleteHead();private:stack<T>stack1;stack<T>stack2;};template<typename T> MyQueue<T>::appendTail(const T& data){stack1.push(data);}template<typename T> T MyQueue<T>::deleteHead(){if(stack2.empty()){if(stack1.empty())throw exception("queue is empty!");else{while(!stack1.empty()){stack2.push(stack1.top());stack1.pop();}}}T ret = stack2.top();stack2.pop();return ret;}

30. output the linked list from the end to the end

Question: Enter the head node of a linked list and output the value of each node from the end to the end.

// If you can modify the linked list, the linked list is reversed first and then output one by one. If not, stack is used. Void printfromtailtohead (listnode * head) {If (Head = NULL) return; stack <listnode *> helper; while (Head! = NULL) {helper. Push (head); Head = head-> next;} while (! Helper. Empty () {cout 

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.