American Group Review 2017 school recruit written test real problem-algorithm engineer a
1. Which of the following STL container implementations is not the same as the other three
A. Set
B. deque
C. Multimap
D. Map
Correct answer: B
The STL container can be divided into the following major categories:
1, sequential (sequence) container, with Vector,list,deque, String,stack (adapter Class), queue (adapter Class), Priority queues (adapter Class).
2, associated containers, have set, Multiset,map,multimap, Bitset,hash_set, Hash_map, Hash_multiset, Hash_multimap.
2. A balanced binary tree with a height of 4, the minimum number of nodes is
A. 5
B. 6
C. 7
D. 8
Correct answer: C
Assuming that a balanced binary tree with a depth of N has at least f (n) nodes, then f (n) satisfies f (n) =f (n-1) +f (n-2) +1
A balanced two-fork tree is a "balanced" treatment of a two-fork tree. The balance factor of the node is defined as its right subtree height minus the Zuozi height. If the balance factor of any node is 1, or 0, or + 1, then this binary sort tree is a balanced two-fork sort tree. According to this principle, the minimum number of nodes is 7.
3. How many structures may be present in a two-fork tree of three nodes
A. 3
B. 4
C. 5
D. 6
Correct answer: C
Number of nodes = number of Cattleya = (2n! )/(n! *n! * (n+1))
4. Which of the following is not a non-stable sorting algorithm
A Merge Sort
B. Quick Sort
C Heap Sort
D Hill Sort
Correct answer: A
(1) Bubble sort
The bubble sort is to move the small element forward or the large element back. The comparison is an adjacent two element comparison, and the interchange also occurs between these two elements. So, if the two elements are equal, I think you will not be bored to exchange them again, if the two equal elements are not adjacent, then even through the preceding 22 exchange two adjacent together, this time will not be exchanged, so the same elements of the order has not changed, so bubble sort is a stable sorting algorithm.
(2) Select sort
The choice of sorting is to choose the smallest current element for each location, such as selecting the smallest one for the first position, selecting the second small for the second element in the remaining element, and so on, until the n-1 element, the nth element is not selected, because it is left with one of its largest elements. Then, in a trip, if the current element is smaller than an element, and the small element appears behind an element that is equal to the current element, then the post-swap stability is destroyed. Compare the awkward, for example, sequence 5 8 5 2 9, we know that the first time to select the 1th element 5 and 2 Exchange, then the original sequence of 2 5 of the relative sequence is destroyed, so the selection of sorting is not a stable sorting algorithm.
(3) Insert sort
An insert sort is an element that is inserted one at a time, based on an already ordered small sequence. Of course, there are only 1 elements at the beginning of this ordered sequence, which is the first element. The comparison starts at the end of the ordered sequence, that is, the element that you want to insert and the one that is already in order, is inserted directly behind it if it is larger than it is, or until it is found where it is inserted. If you encounter an element equal to the insert, the insertion element places the element you want to insert behind the equal element. So, the order of the equal elements is not changed, the order from the original unordered sequence is the order of the sequence, so the insertion sort is stable.
(4) Quick Sort
The quick sort has two directions, the left I subscript goes right, when A[i] <= A[center_index], where Center_index is the array subscript of the central element, generally takes the No. 0 element of an array. and the right J subscript goes left, when a[j] > A[center_index]. If I and J are not moving, I <= J, Exchange A[i] and A[j], repeat the process above until I > J. Exchange A[j] and A[center_index], to complete a quick sort of a trip. When the central element and A[j] are switched, it is very possible to disrupt the stability of the previous element, such as the sequence is 5 3 3 4 3 8 9 10 11, now the central element 5 and 3 (5th element, subscript from 1) The exchange will be the stability of element 3 is disturbed, so fast sorting is an unstable sorting algorithm, Instability occurs at the moment of the exchange of central elements and a[j].
(5) Merge sort
The merge sort is to divide the sequence recursively into the short sequence, the recursive exit is the short sequence only 1 elements (think directly ordered) or 2 sequences (1 comparison and exchange), then merges each ordered segment sequence into an orderly long sequence, merges continuously until the original sequence is all ordered. It can be found that when 1 or 2 elements, 1 elements are not exchanged, and 2 elements are equal in size and no one is intentionally exchanged, which does not destabilize the stability. So, in the process of merging short ordered sequences, is stability compromised? No, we can guarantee that if the two current elements are equal, we keep the elements in the preceding sequence in front of the result sequence, which guarantees stability. Therefore, the merge sort is also a stable sorting algorithm.
(6) Base order
The cardinality sort is sorted by low, then collected, sorted by high order, then collected, and so on, until the highest bit. Sometimes some properties are prioritized, sorted by low priority, then sorted by high priority, and the final order is high priority high, high priority is the same low-priority high. Base sorting is based on sorting separately and is collected separately, so it is a stable sorting algorithm.
(7) Hill sort (shell)
Hill sort is the insertion of elements according to different steps, when the first element is very disordered, the step is the largest, so the number of elements inserted in the order is very small, fast; When the elements are basically ordered, the step size is very low, and the insertion sort is very efficient for ordered sequences. So, the time complexity of hill sorting would be better than O (n^2). Because of the number of insertions, we know that one insert sort is stable and does not change the relative order of the same elements, but in different insertion sorts, the same elements may move in their own insert sort, and finally their stability will be disturbed, so the shell sort is unstable.
(8) Heap Sorting
We know that the heap structure is the child of node I is 2 * I and 2 * i + 1 nodes, the large top heap requires that the parent node is greater than or equal to its 2 child nodes, and the small top heap requires the parent node to be less than or equal to its 2 child nodes. In a sequence of n, the process of heap sequencing is to select the largest (large top heap) or the smallest (small top heap) from the beginning of N/2 and its child nodes by a total of 3 values, and the choice between the 3 elements will of course not destabilize. But when for N/2-1, N/2-2, ... 1 When these parent nodes select an element, the stability is broken. It is possible that the N/2 parent exchange has exchanged the latter element, while the N/2-1 parent node does not exchange the subsequent same element, then the stability between the 2 identical elements is destroyed. Therefore, heap sorting is not a stable sorting algorithm.
In conclusion, it is concluded that the sorting algorithm is not stable, such as selecting sort, quick sorting, hill sort, heap sorting, and bubble sort, insert sort, merge sort and cardinal sort are stable sorting algorithms.
5. There is a text, of which only a,b,c,d,e contains five letters, the number of occurrences of which is a appears 1 times, B appears 2 times, C appears 10 times, D appears 6 times, E appears 4 times, then after Havermann encoding, each letter corresponding encoding may be the following group?
A a=1101,b=1100,c=0,d=10,e=110
B a=1100,b=1101,c=0,d=10,e=111
C. a=1111,b=1101,c=10,d=0,e=110
D. a=1101,b=1100,c=10,d=0,e=111
Correct answer: B
6. If there are 36 nodes in a tree, the minimum height of the three-fork tree is
A. 3
B. 4
C. 5
D. 6
Correct answer: B
The minimum height is the height of a three-prong tree of 3 children per node except for leaves:
Set root node level to 1
First layer: a node.
The second tier has a maximum of: 3 nodes.
The third tier has a maximum of: 9 nodes.
The fourth floor has: 27 nodes.
1+3+9=13<27, 36<1+3+9+27 so the height is 4.
American Group Review 2017 school recruit written test real problem-algorithm engineer a