During the interview, the interviewer often asked us to write a few small programs to test our programming skills. Therefore, it is necessary to practice writing some programs on paper during the preparation of the interview.
Below are several questions that are frequently used in the interview. The frequency is very high !!!!
1. completely and correctly write the Binary Search Algorithm
Int binary_search (int arr [], int n, int key) {assert (arr! = NULL & n> 0); // note the role of assertion int left = 0; int right = n-1; int mid = 0; while (left <= right) {mid = left + (right-left)> 1); // not directly used (left + right) /2 is to prevent out-of-bounds and improve efficiency if (arr [mid] <key) left = mid + 1; else if (arr [mid]> key) right = mid-1; else return mid;} return-1 ;}
Notes in the program:
// Right = n-1 => while (left <= right) => right = middle-1;
// Right = n => while (left <right) => right = middle;
Middle = (left + right)> 1; in this case, the sum of left and right may overflow when the value of left and right is relatively large.
2. Write a sort algorithm (fast sorting and Merge Sorting) that you think is highly efficient and commonly used)
Void QSort (int arr [], int low, int high) {int lower; while (low
3. Reverse linked list
Struct ListNode {int m_nValue; ListNode * m_pNext;}; // The ListNode * ReverseList (ListNode * pHead) {assert (pHead! = NULL); // prevents incoming NULL pointers. parameter check is important !!! Do not forget to write ListNode * pReversedList = NULL; // point to the header node ListNode * pNode = pHead of the reverse linked list; // point to the first node of the chain table next to the closed linked list * pPrev = NULL; // point to the last node of the chain table before the closed list while (pNode! = NULL) {ListNode * pNext = pNode-> m_pNext; if (pNext = NULL) pReversedList = pNode; pNode-> m_pNext = pPrev; pPrev = pNode; pNode = pNext ;} return pReversedList ;}
4. Question description:
Required implementation of the library function strcpy,
Prototype Declaration: extern char * strcpy (char * dest, char * src );
Function: Copies the string ending with NULL indicated by src to the array indicated by dest.
Note: The memory areas specified by src and dest cannot overlap and dest must have sufficient space to accommodate src strings.
Returns the pointer to dest.
// Get 10 points. Basically, all the situations are taken into account. // if there is an overlap between the source and target regions, add 1 point! Char * strcpy (char * strDest, const char * strSrc) // Add the source string to const, indicating that it is the input parameter {if (strDest = strSrc) {return strDest ;} assert (strDest! = NULL) & (strSrc! = NULL); // Add non-0 asserted char * address = strDest to the source address and target address; while (* strDest ++ = * strSrc ++ )! = '\ 0'); return address; // to implement chained operations, return the target address}. We can also write the perfect strlen function version int strlen (const char * str) // enter the const {assert (strt! = NULL); // The asserted string address is not 0 int len; while (* str ++ )! = '\ 0') {len ++;} return len ;}
There are many library functions for string operations. We can refer to the above points to write the perfect version of other string operation library functions.
5 ,.
The implementation efficiency of the memmove () function. This function copies the source string to the temporary buf and then writes it to the target address from the temporary buf,.
Void * Memcpy (void * dst, const void * src, size_t size) // size_t It is a machine-related unsigned type, and its size is sufficient to ensure the size of objects in the storage memory. {// Note that the parameters and return values are void * char * psrc; char * pdst; if (NULL = dst | NULL = src) // check the passed parameters {return NULL;} if (src <dst) & (char *) src + size> (char *) dst) // when memory overlaps, pay attention to back-to-forward copy {psrc = (char *) src + size-1; pdst = (char *) dst + size-1; while (size --) {* pdst -- = * psrc --;} else {psrc = (char *) src; pdst = (char *) dst; while (size --) {* pdst ++ = * psrc ++;} return dst ;}
Class String
{
Public:
String (const char * str = NULL); // common Constructor
String (const String & other); // copy the constructor
~ String (void); // destructor
String & operate = (const String & other); // value assignment function
Private:
Char * m_data; // used to save strings
};
// Normal constructor String: String (const char * str) {if (str = NULL) {m_data = new char [1]; // score point: automatically apply for an empty string to store the NULL ending sign '\ 0' // additional points: Add NULL to m_data to judge * m_data =' \ 0 ';} else {int length = strlen (str); m_data = new char [length + 1]; // strcpy (m_data, str) is better if NULL can be added );}} // String destructor String ::~ String (void) {delete [] m_data; // or delete m_data;} // copy the constructor String: String (const String & other) // score point: the input parameter is const {int length = strlen (other. m_data); m_data = new char [length + 1]; // additional points: Judge strcpy (m_data, other. m_data) ;}// value assignment function String & String: operate = (const String & other) // score point: the input parameter is const {if (this = & other) // score point: Check the self-assigned value return * this; delete [] m_data; // score point: Release the original memory resource int length = strlen (other. m_data); m_data = new char [length + 1]; // additional points: Judge strcpy (m_data, other. m_data); return * this; // score point: return reference of this object}
This class includes the pointer class member variable m_data. When the class includes pointer class member variables, you must reload the copy constructor, value assignment function, and destructor, this is not only a basic requirement for C ++ programmers, but also a special clause in Objective C ++.
Take a closer look at this class and pay special attention to the significance of adding comments to the score points and points. In this way, more than 60% of the basic C ++ skills are available!