C/c ++ interview questions (2)

Source: Internet
Author: User

Int Strcmp (char * str1, char * str2)
{
Int I = 0;
Int B = 0;
While (str1 [I] | str2 [I])
{
If (str1 [I]> str2 [I])
{
B = 1; break;
}
Else if (str1 [I] <str2 [I])
{
B =-1; break;
}
I ++;
}
Return B;
}

**************************************** **************************************** *******************************
1. Explain the running results of the following program and briefly describe its reasons:

Char buf1 [10] = "hello ";
Char buf2 [10] = "hello ";
If (buf1 = buf2)
Printf ("equal! ");
Else printf ("not equal! ");
Because buf1 and buf2 are allocated with different memory blocks, compared with the array name, they are actually two pointers pointing to the address of the starting element of the array respectively.

2. Point out the following issues in the program:

Int loop, a [5];
Int * p =;
For (loop = 0; loop <5; loop ++)
{P ++;
* P = loop;
}

Array a [5] is not initialized when it is created. It does not fully initialize the array in the for loop, and it assigns a value to an unknown memory. In the last round
At the end, p points to the next address of the last element of array a [5.

String Series

Char * strcpy (char * strDest, const char * strSrc)
{
Assert (strDest! = NULL) & (strSrc! = NULL ));
Char * address = strDest;
While (* strDest ++ = * strSrc ++ )! = '/0 ');
Return address;
}

Char * strncpy (char * strdest, const char * strsrc, int n)
{
Assert (strdest! = NULL) & (strsrc! = NULL ));
Char * address = strdest;
While (n --> 0)
* Strdest ++ = * strsrc ++;
Return address;
}

Int strcmp (const char * str1, const char * str2)
{
Assert (str1! = NULL) & (str2! = NULL );
Int ret = 0;
While (! (Ret = (unsigned char *) * str1-(unsigned char *) * str2) & (* str2 ))
{
Str1 ++;
Str2 ++;
}
If (ret> 0)
Ret = 1;
Else if (ret <0)
Ret =-1;
Return ret;
}

Int strlen (const char * str)
{
Assert (str! = NULL );
Int len = 0;
While ('/0 '! = * Str ++)
Len ++;
Return len;
}
Constructor of the string type
String: string (const char * str)
{
If (str = NULL)
{
M_data = new char [1];
* M_data = '/0 ';
}
Else
{
Int length = strlen (str );
M_data = new char [STR + 1];
Strcpy (m_data, STR );
}
}

String destructor
String ::~ String ()
{
Delete [] m_data;
}

String copy constructor
String: string (const string & other)
{
Int Len = strlen (other. m_data );
M_data = new char [Len + 1];
Strcpy (m_data, other. m_data );
}

String Value assignment function
String & string: Operator = (const string & other)
{
If (this = & other)
Return * this;
Delete [] m_data;
Int len = strlen (other. m_data );
M_data = new char [len + 1];
Strcpy (m_data, other. m_data );
Return * this;
}

Int strlen (char * a) is implemented without any local or global variables)

Int strlen (char * ){
If ('/0' = *)
Return 0;
Else
Return 1 + strlen (a + 1 );
}

1) questions about sizeof
2) const Problems
3) A large number of exercises and variants of Lin Rui's books
These three will appear almost every time
The following are program-related questions, many of which have been discussed before. Please help me sort out answers that are suitable for interviews. Thank you.
The discussion link gives me a chance to learn from my later generation.
1) Algorithm for Finding similarity.
2) write the binary search code.
Int binary_search (int * arr, int key, int n)
{
Int low = 0;
Int high = n-1;
Int mid;
While (low <= high)
{
Mid = (high + low)/2;
If (arr [mid]> k)
High = mid-1;
Else if (arr [mid] <k)
Low = mid + 1;
Else
Return mid;
}
Return-1;
}

3) write the code to find the occurrence times of the substring in the parent string.
* 4) write the code of a quick sort or a sort algorithm.
Appears frequently
5) write an algorithm to search for all child sets output from a set.
* 6) Implement the strcpy Function
Char * strcpy (char * DEST, const char * SRC)
{
Assert (DEST! = NULL) & (SRC! = NULL ));
Char * address = DEST;
While ('/0 '! = (* DEST ++ = * SRC ++ ));
Return address;
}
Appears frequently
* 7) Implement strcmp Functions
Int mystrcmp (const char * str1, const char * str2)
{
Assert (str1! = NULL) & (str2! = NULL ));
Int ret = 0;
While (! (Ret = * (unsigned char *) str1-* (unsigned char *) str2) & * str2)
{
Str1 ++;
Str2 ++;
}
If (Ret> 0)
Ret = 1;
Else if (Ret <0)
Ret =-1;
Return ret;
}

Appears frequently
8) reverse order of a single-chain table
Struct Test
{
Int number;
Double score;
Test * next;
}
Void reverse (test * & head)
{
Test * Pe = head;
Test * PS = head-> next;
While (PS! = NULL)
{
Pe-> next = ps-> next;
PS-> next = head;
Head = Ps;
Ps = pe-> next;
}
}



9) switch and delete nodes in the cyclic linked list.
* 10) convert a numeric string to a number. "1234" --> 1234
# I nclude <iostream>
Using namespace std;

Int f (char * s)
{
Int k = 0;
While (* s)
{
K = 10 * k + (* s ++)-'0 ';

}
Return k;
}

Int main ()
{
Int digit = f ("4567 ");
Cout <digit <endl;
Cin. get ();
}
Appears frequently
11) Add or multiply Integers of any length.
* 12) write the function to copy the memory.
Implementation Body of a memory copy Function

Void * memcpy (void * pvTo, const void * pvFrom, size_t size)

{

Assert (pvTo! = NULL) & (pvFrom! = NULL ));

Byte * pbTo = (byte *) pvTo; // prevents address change

Byte * pbFrom = (byte *) pvFrom;

While (size --> 0)

* PbTo ++ = * pbForm ++;

Return pvTo;

}

Appears frequently

 

. Written examination:
1) Write a memory copy function without any database functions. This is the problem discussed in the previous version.
Void * memcpy (void * pvTo, const void * pvFrom, size_t size)
{
Assert (pvTo! = NULL) & (pvFrom! = NULL ));
Byte * pbTo = pvTo;
Byte * pbFrom = pbFrom;
While (size --> 0)
{
* PbTo ++ = * pbFrom ++;
}
Return pvTo;
}
2) sort a single-chain table in reverse order. (This problem is a conventional data structure problem. However, when you are not careful, efficiency will be lost)
3) room reservation problems. Based on the number of people reported by the customer and Room level, select all the qualified

Room number. When the customer does not require a level, only the number of people can be considered. Consider the fact that some rooms have been booked.

(To write code, consider the efficiency of each other)
4) perform binary search for an unordered Sequence
Sort and search again

(5) convert a numeric string to a numeric string. "1234" --> 1234
Int convert (char * str)
{
Int k = 0;
While (* str! = '/0 ')
{
K = k * 10 + * s ++-'0 ';
}
Return k;
}
6) read the information (including employee ID and employee output) in the file (call the database function to create, without considering the database method)

According to the entered information (including employee number, employee output) .. check whether there are the same employee number records. If so, add

Output. If not, a new record is created. The sorting of the final record is based on the worker output (in descending order). If the output is the same

By employee ID (ascending)

Sub)
.
2. Interview
1) Identify the similarity between two Chinese sentences (for example, "Nanjing, Jiangsu, China", "Nanjing, Jiangsu, China ").

In the same place, the interviewer requires an algorithm to calculate similarity in one minute.) (fortunately, I heard the teacher talk about Chinese word segmentation, or

Then the system crashes on the spot)
2) write the binary search code.
3) generalize the above Code. (in the c Specification, it is the problem I mentioned earlier)
4) write the code to find the number of occurrences of the substring in the parent string (regardless of the efficiency, let's talk about it. At that time, I did not care about KMP.

When I came out, I was asked to describe the whole process. I had to learn it from the beginning. But I didn't have a cold field. I learned it and said. hoho)
5) How to define many static variables in a function.
6) Write quick_sort
7) write an algorithm to search for all child sets output from a set.
8) There are various types of pointers. sizeof calculation results for various data types (in C)

 

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.