Google pen exam.

Source: Internet
Author: User

I. single answer
1. In 80x86, the decimal number-3 is expressed as a 16-bit binary number?

Q: 0xfffd. The data is represented by a complement code in the computer. The positive complement code is its original code, which remains unchanged. The negative value is its complement code for Inverse addition. The highest bit is the symbol bit.

Therefore, we can calculate 3 minus 1, equals 2, and then obtain the-3 complement code.

 

2. Assume that the symbols-, *, and $ represent subtraction, multiplication, and exponential Operations respectively, and
The priority order of the three operators is-highest, * Second, and $ lowest;
The operator is left-aligned during operation. Calculate the value of 3-2*4 $1*2 $3:
(A) 4096, (B)-61, (c) 64, (d)-80, (e) 512

Answer:-The expression has the highest priority. The expression is calculated from left to right. Calculate 3-2, get 1, multiply by 4, and get 4. Because $ D has the lowest priority

Then calculate 1 × 2, which is 2. The final expression is 4 $2 $3. From left to right, It is 4 to the power of 2, get 16, then Power 3, get 4096.

 

3. In the following pseudocode, the parameter is passed by reference and the result is?
Calc (Double P, double Q, Double R)
{

Q = q-1.0;

R = R + P

}

Main (

{
Double A = 2.5, B = 9.0;
Calc (B-a, a, );
Print ();
}
(A) 1.5 (B) 2.5 (c) 10.5 (d) 8 (e) 6.5

A: The result of B-A is 6.5, which is passed as a temporary variable. Therefore, the actual calculated expression is

A = A-1.0;-> A = 1.5

A = a + 6.5;-> A = 1.5 + 6.5

4. output result:
Int Foo (int x, int y)

{

If (x <= 0 | Y <= 0) return 1;
Return 3 * Foo (X-1, Y/2 );
}

Printf ("% d/N", Foo (3, 5 ));
(A) 81 (B) 27 (c) 9 (d) 3 (e) 16

Answer: This question examines the call of recursive functions. The call stack sequence is: Foo (3, 5)-> 3 * Foo (2, 2)-> 3*3 * Foo (1, 1) -> 3*3*3 * F (0, 0)

Foo (0, 0) returns one, so the result is 27.

5. Which of the following data structures is most widely used in Priority Queues?
(A) heap (B) array (c) bidirectional linked list (d) graph (e) vector

A: As we all know, the implementation of priority queue is based on heap. Because the minimum value (maximum value) of the heap is at the top of the heap, this feature makes it possible to implement a non-heap priority queue.

 

 

6. The following algorithm describes how to find the K element in a two-way linked list of n elements.
Method (k> = 1 and K <= N ):
If K <= n-k, the K-1 elements start from the linked list.
Otherwise, start from the end and go back to n-k elements.
What is the time cost of this algorithm?
(A) θ (nlogn) (B) θ (max {k, n-k}) (c) θ (K + (n-k ))
(D) θ (max {k, K-n}) (e) θ (min {k, n-k })

A: The purpose of this algorithm is to find a time-saving algorithm. Therefore, E. Is min (K. n-k ).

 


7. How many edges does a graph contain ten vertices and each vertex has six degrees?
(A) 60 (B) 30 (c) 20 (d) 80 (e) 90

A: I forgot about graph theory. How to calculate?

 

8. Regular Expression L = x * (X | Yx + ). Which of the following strings is not signed by L?
(A) x (B) xyxyx (c) xyx (d) yxx (e) Yx

Pick up the regular expression again, and haven't written it for a long time: from left to right, x * indicates that it can match 0 times or multiple times X.

In the brackets again, X | Yx + can be X or Yx ++. Note that Yx is not a whole.

Therefore, xyxyx does not match.

 

9. The total time required to prepare a disk drive for reading a piece of data includes:
(A) waiting time (B) seeking time (c) transmission time (d) Waiting Time plus seeking time
Wait time + seek time + Transfer Time

Answer: I personally think the answer is D. The answer is unreliable. This question remains to be studied.

Ii. Algorithms

1. Print the content of a binary tree.

Answer: print the left subtree recursively, output the root node, and then print the right subtree. Of course, it is not that easy to print in the tree.

A little more complicated.

 

2. Find the first character that appears only once in a string. For example, abaccdeff, output B.

You can use a simple hash method to use a hash array. the hash result is the number of occurrences of each character.

Then, traverse the content of this array from left to right to see who has the value of 1.

Algorithm:
Char findearliestfirstoccurence (const char * Str)
{
Const char * P = STR;
Char table [256] = {0 };
For (; * P; P ++)
{
Table [* p] ++;
}
Const char * q = STR;
For (; * q; q ++)
If (Table [* q] = 1)
Return * q;
Return 0; // if the return value is zero, no value is found.
}

The time complexity is about 2 * strlen (STR); it is still linear.

3. Given an integer array whose length is n (the element has positive and negative values), calculate the sum of all elements.
The largest subarray. Analyzes the algorithm's time-space complexity. You do not need to write code.

 

I personally think this question is a variant that calculates the maximum value of the sum of all elements. (For an integer array whose length is N, positive and negative exist, the maximum value of the sum of sub-arrays is obtained ).

 

Method 1: exhaustive. Calculate the maximum value.

Method 2: Divide and conquer

Method 3: A clever method. The logic is as follows: from left to right, calculate the sum of elements. If the sum of elements is positive, continue to the right. If the sum of elements is negative

Segment element value and set to zero. Recalculate the sum of elements. Algorithm Description:

Int maxsubsum (const vector <int> &)

{

Int maxsum = 0;

Int currentsum = 0;

 

For (INT I = 0; I <n; ++ I)

{

Currentsum + = A [I];

If (currentsum> maxsum)

Maxsum = currentsum;

Else if (currentsum <0) // if the current segment is worth and less than 0, this segment can be discarded because it is negative, and the final result must include this segment.

Currentsum = 0;

}

)

 

With this foundation, it is easy to find the maximum number of words. Just use two variables to identify the head and end of the maximum number of words.

Vector <int> maxsubsum (const vector <int> &)
{
Int maxsum = 0;
Int currentsum = 0;
Int start = 0;
Int end = 0;

Vector <int> resultset;
For (size_t I = 0; I <A. Size (); ++ I)
{
Currentsum + = A [I];
If (currentsum> maxsum)
{

Maxsum = currentsum;
End = I; // The end value is constantly updated. Used to indicate the end of the word group. End is updated with the update of maxsum.
}
Else if (currentsum <0) // if the current segment is worth and less than 0, this segment can be discarded because it is negative, and the final result must include this segment.
{
Currentsum = 0;
Start = I + 1;
}
}

Copy (A. Begin () + start, A. Begin () + end, back_inserter (resultset); // copy the longest group array to the result array .. It is equivalent to copying data from start to end to the result array.
Return resultset;

}

 

Test procedure:

# Include <vector>
# Include <iostream>
# Include <iterator>
# Include <algorithm>

Using namespace STD;

Int _ tmain (INT argc, _ tchar * argv [])
{
Vector <int>;
Copy (istream_iterator <int> (CIN), istream_iterator <int> (), back_inserter ());

Vector <int> B;
B = maxsubsum ();
Copy (B. Begin (), B. End (), ostream_iterator <int> (cout ,""));
Return 0;
}
The time complexity of this algorithm is O (n ).

 

 

The code is basically written in C/C ++. It is easy to use and the question is not correct. Please point it out.

 

 

 

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.