Preparing for Autumn Recruitment | Strong Training for Written Examination 19

Table of contents

1. Multiple choice questions

2. Programming questions

3. Solutions to multiple choice questions

4. Programming problem solving


1. Multiple choice questions

1. Time complexity of binary search ()

A. O(N*log(N))

B. O(N)

C. O(log(N))

D. O(N^2)

2. There is a one-way linked list with two adjacent elements A and B. There is a pointer p pointing to element A. Now, the S element pointed to by the pointer r is to be inserted between A and B. It is time to perform operation ()

A. p->next=p->next->next

B. r-next=p;p->next=r->next

C. r->next=p->next;p->next=r

D. r=p->next;->next=r->next

E. r->next=p;p->next=r

F. p=p->next->next

3. There are two pointer fields in the doubly linked list. llink and rlink point to the predecessor and successor respectively. Suppose p points to a node in the linked list. Now it is required to delete the node pointed by p. The correct deletion is () (in the chain) The number of nodes is greater than 2, p is not the first node)

A. p->llink->rlink:=p->llink; p->llink->rlink:=p->rlink; dispose(p);

B. dispose(p); p->llink->rlink:=p->llink; p->llink->rlink:=p->rlink;

C. p->link->rlink:=p->llink; dispose(p); p->llink->rlink:=p->rlink;

D. None of the above A, B, and C are correct

4. The push sequence of a stack is A, B, C, D, E, then the impossible output sequence of the stack is ()

A. EDCBA

B. DECBA

C. DCEAB

D. ABCDE

5. The circular queue is placed in the one-dimensional array A[0...M-1], end1 points to the head element of the queue, and end2 points to the position after the tail element of the queue. Assuming that both ends of the queue can perform enqueue and dequeue operations, the queue can accommodate up to M-1 elements. It is empty initially. Among the following conditions for judging whether the team is empty or full, the correct one is ()

A. The team is empty: end1==end2; the team is full: end1==(end2+1) mod M

B. The team is empty: end1==end2; the team is full: end2==(end1+1) mod (M-1)

C. The team is empty: end2==(end1+1) mod M; the team is full: end1==(end2+1) mod M

D. The team is empty: end1==(end2+1) mod M; the team is full: end2==(end1+1) mod (M-1)

6. It is known that the post-order traversal sequence of the binary tree is bfegcda, the in-order traversal sequence is badefcg, and its pre-order traversal sequence is ()

A. abcdefg

B. abdcefg

C. adbcfeg

D. abecdfg

7. A certain binary tree has a total of 399 nodes, of which 199 are nodes with degree 2. Then the number of leaf nodes in the binary tree is ()

A. There is no such binary tree

B. 200

C. 198

D. 199

8. Suppose a set of recorded keywords is {19,14,23,1,68,20,84,27,55,11,10,79}, use the chain address method to construct a hash table, and the hash function is H (key)=key MOD 13, there are () records in the chain with hash address 1

A. 1

B. 2

C. 3

D. 4

9. Which of the following sortings is unstable sorting ()

A. bubbling

B. Insertion sort

C. Merge sort

D. Quick sort

2. Programming questions

1. Soda bottle   question link

2. Find the longest common substring   question link between two strings a and b.

3. Solutions to multiple choice questions

1. Time complexity of binary search ()

A. O(N*log(N))

B. O(N)

C. O(log(N))

D. O(N^2)

Correct answer: C

answer:

         Basic concept questions;

2. There is a one-way linked list with two adjacent elements A and B. There is a pointer p pointing to element A. Now, the S element pointed to by the pointer r is to be inserted between A and B. It is time to perform operation ()

A. p->next=p->next->next

B. r-next=p;p->next=r->next

C. r->next=p->next;p->next=r

D. r=p->next;->next=r->next

E. r->next=p;p->next=r

F. p=p->next->next

Correct answer: C

answer:

         As shown in the figure below, we want to insert a new node;

3. There are two pointer fields in the doubly linked list. llink and rlink point to the predecessor and successor respectively. Suppose p points to a node in the linked list. Now it is required to delete the node pointed by p. The correct deletion is () (in the chain) The number of nodes is greater than 2, p is not the first node)

A. p->llink->rlink:=p->llink; p->llink->rlink:=p->rlink; dispose(p);

B. dispose(p); p->llink->rlink:=p->llink; p->llink->rlink:=p->rlink;

C. p->link->rlink:=p->llink; dispose(p); p->llink->rlink:=p->rlink;

D. None of the above A, B, and C are correct

Correct answer; D

answer:

         We can see the correct code in conjunction with the picture below;

4. The push sequence of a stack is A, B, C, D, E, then the impossible output sequence of the stack is ()

A. EDCBA

B. DECBA

C. DCEAB

D. ABCDE

Correct answer: C

answer:

         Option C, when we first pop out D, it means that ABC has been pushed onto the stack. At this time, we pop out a C, and there is AB in the stack. We then pop in an E, and then pop out another E. Then the element on the top of the stack should be B instead of A, so C is wrong;

5. The circular queue is placed in the one-dimensional array A[0...M-1], end1 points to the head element of the queue, and end2 points to the position after the tail element of the queue. Assuming that both ends of the queue can perform enqueue and dequeue operations, the queue can accommodate up to M-1 elements. It is empty initially. Among the following conditions for judging whether the team is empty or full, the correct one is ()

A. The team is empty: end1==end2; the team is full: end1==(end2+1) mod M

B. The team is empty: end1==end2; the team is full: end2==(end1+1) mod (M-1)

C. The team is empty: end2==(end1+1) mod M; the team is full: end1==(end2+1) mod M

D. The team is empty: end1==(end2+1) mod M; the team is full: end2==(end1+1) mod (M-1)

Correct answer: A

answer:

         When the two subscripts are at the same position, they are empty. The size of the space above the tail + 1 modulus is equal to the head of the queue, which is full; therefore, choose A;

6. It is known that the post-order traversal sequence of the binary tree is bfegcda, the in-order traversal sequence is badefcg, and its pre-order traversal sequence is ()

A. abcdefg

B. abdcefg

C. adbcfeg

D. abecdfg

Correct answer: B

answer:

         We can determine a root each time through the post-order, and split the left and right subtrees in the in-order; the specific analysis is as follows;

7. A certain binary tree has a total of 399 nodes, of which 199 are nodes with degree 2. Then the number of leaf nodes in the binary tree is ()

A. There is no such binary tree

B. 200

C. 198

D. 199

Correct answer: A

answer:

         Formula, n0 = n2 + 1; calculated to 200;

8. Suppose a set of recorded keywords is {19,14,23,1,68,20,84,27,55,11,10,79}, use the chain address method to construct a hash table, and the hash function is H (key)=key MOD 13, there are () records in the chain with hash address 1

A. 1

B. 2

C. 3

D. 4Correct
answer: D

answer:       

         The question asks us to find which of the above data modulo 13 is equal to 1; they are 1, 14, 27, and 79 respectively; so choose D;

9. Which of the following sortings is unstable sorting ()

A. bubbling

B. Insertion sort

C. Merge sort

D. Quick sort

Correct answer: D

answer:

         Quick sort cannot guarantee the stability of sorting;

4. Programming problem solving

1. Soda bottle

Idea: We simulate the process of drinking soda. We create a variable to record the current number of bottles, and a variable to record the total number of bottles drunk; then simulate the process of drinking soda to solve;

#include <iostream>
using namespace std;

int main() 
{
    int n;
    while(cin >> n)
    {
        if(n == 0)
            break;
        // 记录当前瓶子个数
        int botton = n;
        int water = 0;
        while(botton >= 3)
        {
            // 可以兑换汽水数
            water += botton / 3;
            // 求得喝完以后的瓶子数
            botton = botton % 3 + botton / 3;
        }
        // 处理特殊情况,借瓶子
        if(botton == 2)
            water += 1;
        cout << water << endl;
    }
    return 0;
}

2. Find the longest common substring in two strings a and b.

Idea: It is recommended to use dynamic programming to solve this problem to find the maximum number of consecutive characters of str1 and str2 ending with i-1; 

#include <iostream>
#include <string>
#include <vector>
using namespace std;

string LComSub(string& str1, string& str2)
{
    int len1 = str1.size();
    int len2 = str2.size();
    // 多开一个位置
    vector<vector<int>> dp(len1 + 1, vector<int>(len2 + 1, 0));

    int start = 0;
    int max = 0;
    for(int i = 1; i <= len1; i++)
    {
        for(int j = 1; j <= len2; j++)
        {
            if(str1[i - 1] == str2[j - 1])
                dp[i][j] = dp[i - 1][j - 1] + 1;
            if(dp[i][j] > max)
            {
                max = dp[i][j];
                start = i - max;
            }
        }
    }
    return str1.substr(start, max);
}

int main() 
{
    string str1, str2;
    cin >> str1 >> str2;
    // 最短字符串放在str1中
    if(str1.size() > str2.size())
        str1.swap(str2);
    cout << LComSub(str1, str2) << endl;
}

Guess you like

Origin blog.csdn.net/Nice_W/article/details/132023923