Data structure and algorithms classic 80

Source: Internet
Author: User
Tags arrays first string first row


Because of these problems, it is too hot. Therefore, should be the majority of netizens suggested the request, this has been collated before the publication of the first 80 questions,
Now, share it all at once. This is also considered the first 80 questions the collective appearance.
These questions, there have been tens of thousands of people, saw or see, if privately appropriated, must be known to the people, pay the price.
Therefore, the author declares:
I July all above any content and materials enjoy copyright, reproduced please indicate the author himself July source.
Pay tribute to your kindness. Thank you.



Turn the two-dollar lookup tree into a sorted doubly linked list
Topic:
Enter a two-dollar lookup tree to convert the two-dollar lookup tree into a sorted doubly linked list.
Requires that no new nodes be created, only the pointer is adjusted.
10
/ \
6 14
/ \ / \
4 8 12 16
Convert to doubly linked list
4=6=8=10=12=14=16.


     First we define the data structure of the two-tuple lookup tree node as follows:
     struct bstreenode
    {
     int m_nvalue;//value of node
     bstreenode *m_pleft;//left C Hild of node
     bstreenode *m_pright;//Right Child of Node
    };
Design a stack that contains the Min function.
Defines the data structure of the stack, requiring the addition of a min function to get the smallest element of the stack.
The time complexity required for the function min, push, and Pop is O (1).


To find the largest and most large array of sub-arrays
Topic:
Enter an array of shapes with positive and negative numbers in the array.
One or more consecutive integers in an array make up a sub-array, each of which has a and.
The maximum value for the and of all sub-arrays. Requires a time complexity of O (n).


For example, the input array is 1,-2, 3, 10,-4, 7, 2,-5, and the largest sub-array is 3, ten, -4, 7, 2,
so the output is the and 18 of that subarray.


4. Find and all paths to a value in the two-dollar Tree
Title: Enter an integer and a two-dollar tree.
From the root node of the tree, access all the nodes that pass through to the leaf nodes to form a path.
Prints out all paths equal to the input integers.
For example, enter an integer 22 and the following two-tuple tree
10
/ \
5 12
/ \
4 7
Two paths are printed: 10, 12 and 10, 5, 7.


    The data structure of a binary tree node is defined as:
    struct Binarytreenode//A node in the binary tree
    {
        int m_nvalue;//value of node
        Binaryt Reenode *m_pleft; Left child of node
        binarytreenode *m_pright;//Right Child of Node
    };


5. Find the smallest element of K
Title: Enter n integers to output the smallest of the K.
For example, enter 1,2,3,4,5,6,7 and 8 for these 8 numbers, then the smallest 4 digits are three-to-three and 4.



Question 6th
Tencent interview questions:
Give you 10 minutes, according to the upper row give 10 number, in its next row to fill out the corresponding 10 number
The number of rows that are required in the next row is the number of times that the previous 10 numbers appear in the row.
The 10 numbers on the top row are as follows:
"0,1,2,3,4,5,6,7,8,9"



To give an example,
Value: 0,1,2,3,4,5,6,7,8,9
Allocation: 6,2,1,0,0,0,1,0,0,0
0 appeared in the lower row 6 times, 1 in the lower row appeared 2 times,
2 appeared in the next row 1 times, 3 in the lower row appeared 0 times ....
etc...



Question 7th
The programming of the Microsoft Institute determines whether two linked lists Intersect
Give the head pointers of two unidirectional lists, such as H1,H2, to determine if the two lists intersect.
To simplify the problem, we assume that both of the lists are not with loops.



Problem extension:
1. If the linked list may have ring out?
2. What if you need to find the first node column where two linked lists intersect?



Question 8th
This paste selected some of the more strange questions, because of the problem itself and the algorithm is not a small relationship, only test thinking. hereby and make a question.
1. There are two rooms, one room has three lights, the other room has three switches that control three lights,



The two rooms are separated from each other and cannot be seen in one room.
The trainees are now required to enter the two rooms one at a time, and then determine which switch controls the three lights respectively.
Is there any way to do it.



2. You let some people work for you for seven days, you have to use a gold bar as a reward. The gold bars were divided into seven small pieces, given each day.
If you could only cut the gold bars two times, how would you assign them to the workers?



3.★ uses an algorithm to reverse the order of a linked table. Now do it again without recursion.
★ Use an algorithm to insert a node in a loop's link table, but not through the linked tables.
★ Use an algorithm to organize an array. Why do you choose this method?
★ Use an algorithm to match the generic string.
★ Invert a string. Optimize speed. Optimize space.
★ Reverse the order of the words in a sentence, such as converting "My name to Chris" to "Alice calls Me",



Achieve the fastest and least mobile.
★ Find a substring. Optimize speed. Optimize space.
★ Compare two strings with O (n) time and const space.
★ Suppose you have an array of 1001 integers that are arbitrarily arranged, but you know that all integers are between 1 and 1000 (including 1000). In addition, all other numbers appear only once, except that one number appears two times. Suppose you can only handle this array once, and use an algorithm to find the duplicate number. If you use secondary storage in your calculations, can you find an algorithm that doesn't work this way?
★ No multiplication or addition is added 8 times times. Now add 7 times times the same method.



Question 9th
Judging the sequence of integers is not a two-dollar lookup tree post-order traversal results
Title: Enter an array of integers to determine if the array is the result of a sequential traversal of a two-tuple lookup tree.
Returns False if True is returned.



For example, input 5, 7, 6, 9, 11, 10, 8, because this integer sequence is the following post-order traversal result of the tree:
8
/ \
6 10
/ \ / \
5 7 9 11
Therefore, returns True.
If you enter 7, 4, 6, 5, no tree has the result of a post-order traversal that is the sequence, and therefore returns false.



Question 10th
Flips the order of the words in the sentence.
Title: Enter an English sentence, flipping the order of the words in the sentence, but the order of the characters within the word is unchanged.



Words are separated by spaces in sentences. For simplicity, punctuation is treated like ordinary letters.
For example, enter "I am a student.", then output "student." A am I ".



Question 11th
Find the maximum distance of a node in a binary tree ...



If we look at the two-fork tree as a graph, the connection between the parent and child nodes is considered bidirectional,
Let's define the number of edges between two nodes for distance.
Write a program,
Find the distance between the two nodes farthest apart in a binary tree.



Question 12th
Title: Seeking 1+2+...+n,
Requirements cannot use multiplication, for, while, if, else, switch, case, and conditional judgment statements (A?). B:C).



Question 13th:
Title: Enter a one-way linked list to output the penultimate K-node in the linked list. The bottom No. 0 node of the list is the tail pointer of the linked list.
The linked list node is defined as follows:
struct ListNode
{
int M_nkey;
listnode* M_pnext;
};



Question 14th:
Title: Enter an array and a number that has been sorted in ascending order,
Find two numbers in the array so that they are exactly the number entered.
The required time complexity is O (n). If there are many pairs of numbers and equals the input number, the output can be any pair.
For example, input arrays 1, 2, 4, 7, 11, 15, and number 15. Because of the 4+11=15, the outputs are 4 and 11.



Question 15th:
Title: Enter a two-dollar lookup tree to convert the tree to its mirror,
That is, in the converted two-dollar lookup tree, the nodes of the Zuozi are larger than the nodes of the right subtree.
Recursive and cyclic methods are used to complete the image transformation of the tree.
For example, enter:
8
/ \
6 10
/\ /\
5 7 9 11



Output:
8
/ \
10 6
/\ /\
11 9 7 5



The nodes that define the two-tuple lookup tree are:
struct Bstreenode//A node in the binary search tree (BST)
{
int m_nvalue; Value of Node
Bstreenode *m_pleft; Left child of Node
Bstreenode *m_pright; Right Child of Node
};



Question 16th:
Topic (Microsoft):
Enter a two-dollar Tree to print each node of the tree from top to bottom, in the same layer, in the order from left to right.
For example, enter
8
/ \
6 10
/ \ / \
5 7 9 11



Output 8 6 10 5 7 9 11.



Question 17th:
Title: Finds the first occurrence of a character in a string. If you enter Abaccdeff, then output B.
Analysis: This problem is a 2006 Google pen test.



Question 18th:
Title: N Numbers (0,1,..., n-1) Form a circle, starting with the number 0,
Each time the number of m digits is removed from this circle (the first is the current number itself, the second is the next number of the current number).
When a number is deleted, the next number from the deleted number continues to delete the first m digits.
Find the last number left in this circle.
July: I think a lot of people have seen this topic.



Question 19th:
Title: Define the Fibonacci sequence as follows:
/0 N=0
F (n) = 1 n=1
\ f (n-1) +f (n-2) n=2



Enter N to find the nth item of the sequence in the quickest way.
Analysis: When we talk about recursive functions in many C language textbooks, we use Fibonacci as an example.
So many programmers are very familiar with the recursive solution of this problem, but ... oh, you know.



Question 20th:
Title: Enter a String representing an integer to convert the string to an integer and output.
For example, the input string "345", the output integer 345.



Question 21st
2010 Chung-Hsing Face test
Programming Solution:
Enter two integers n and M, take a few numbers from the sequence 1,2,3.......N,
making it and equal to m requires that all possible combinations of them be listed.



Question 22nd:
There are 4 red cards and 4 blue cards, the host first take any two, and then respectively in a, B, C three people on the forehead affixed any two cards,
A, B, c three people can see the other two people on the forehead of the cards, after reading let them guess what color on their forehead card,
A said do not know, B said do not know, C said do not know, then a said to know.
Ask how to reason, A is how to know.
If you use the program, how to achieve it.



Question 23rd:
The simplest and quickest way to calculate whether the following circle intersects a square. ”
3D coordinate system origin (0.0,0.0,0.0)
Circular:
Radius r = 3.0
Center o = (., 0.0,.)



Square:
4 angular coordinates;
1: (., 0.0,.)
2: (., 0.0,.)
3: (., 0.0,.)
4: (., 0.0,.)



Question 24th:
Linked list operations,
(1). Single-linked list in-place inversion,
(2) Merge linked list



Question 25th:
Write a function whose prototype is int Continumax (char *outputstr,char *intputstr)
Function:
Find the longest consecutive number string in the string, and return the length of this string,
and send this longest number string to one of the function parameters outputstr the memory.
For example: When the first address of "abcd12345ed125ss123456789" is passed to Intputstr, the function returns 9,
Outputstr refers to a value of 123456789



26. Left rotation string



Topic:
Defines the left rotation of a string: moves several characters in front of the string to the end of the string.



If the string abcdef left rotation 2 bit to get the string cdefab. Please implement the function of string left rotation.
The complexity of a string operation that requires time to be N is O (n), and auxiliary memory is O (1).



27. Stair Jumping problem
Title: A step has a total of n, if you can jump 1 levels at a time, you can jump 2 levels.
How many total hops are in total, and the time complexity of the algorithm is analyzed.



This problem is often seen recently, including MicroStrategy and other companies that pay more attention to algorithms
Have successively selected this problem as a face test or pen test.



28. The number of integers in the binary representation of 1
Title: Enter an integer to find out how many 1 are in the binary representation of the integer.
For example, enter 10, because the second binary is represented as 1010, there are two 1, so the output is 2.



Analysis:
This is a very basic test of the bit arithmetic.
Many companies, including Microsoft, have used the problem.



29. Stack push, pop sequence
Title: Enter a sequence of two integers. One of the sequences represents the push order of the stack,
Judging whether another sequence is possible is the corresponding pop order.
For the sake of simplicity, we assume that any two integers of the push sequence are not equal.



For example, the input push sequence is 1, 2, 3, 4, 5, then 4, 5, 3, 2, 1 may be a pop series.
Because you can have the following push and pop sequences:
Push 1,push 2,push 3,push 4,pop,push 5,pop,pop,pop,pop,
The resulting pop sequence is 4, 5, 3, 2, 1.
But sequences 4, 3, 5, 1, 2 are not likely to be the sequence of pop sequences of the push sequence 1, 2, 3, 4, 5.



30. The number of occurrences of 1 in positive numbers from 1 to n
Title: Enter an integer n to find the number of decimal representations of the n integers from 1 to n in 1 occurrences.



For example, enter 12, from 1 to 12 these integers contain 1 of the numbers have 1,10,11 and 12, 11 have appeared 5 times.
Analysis: This is a widely circulated Google face question.



31.  Interview Questions:
A diagram similar to the structure of a honeycomb, searching the shortest path (requires 5 minutes)



32.
There are two sequences, a, B, the size is n, the values of the sequence elements are arbitrary integers, unordered;
Requirement: To minimize the difference between the and of [sequence A and] and [sequence B elements] by exchanging the elements in A/b.
For example:
var a=[100,99,98,1,2, 3];
var b=[1, 2, 3, 4,5,40];



33.
Implement a very advanced character matching algorithm:
Give a string of very long strings, asking to find matching strings, such as the destination string: 123
1******3***2, 12*****3, these are all going to be found.
is actually similar to some harmonious system .....



34.
Implement a queue.
The scenario for a queue is:
A producer thread into row the number of int types, and a consumer thread dequeue the number of type int



55h
Find the largest two-dimensional matrix (element and maximum) in a matrix. such as:
1 2 0) 3 4
2 3 4) 5 1
1 1 5) 3 0
The largest of these is:
4 5
5 3
Requirements: (1) write algorithm, (2) Analyze Time complexity, (3) write key code in C



Question 36th-40 questions (some of the topics collected in Csdn, have been marked):
36. References from netizens: Longzuo
Google written test:
N team competition, respectively numbered 0,1,2 .... N-1, known for their strength-contrast relationship,
Stored in a two-dimensional array w[n][n], the value of W[i][j] represents a stronger branch of the team numbered i,j.



So W[i][j]=i or J, now give them the order of appearances, and store them in the array order[n],
For example Order[n] = {4,3,5,8,1 ...}, then the first round is 4 to 3, 5 to 8. .......
Winners, losers eliminated, the same round of the elimination of all ranks no longer subdivided, that can be casually platoon,
The next round by the winner of the previous round in order, followed by 22, for example, may be 4 to 5, until the first place appears



A programming implementation that gives a two-dimensional array W, a one-dimensional array order, and an array of result[n for the output of the tournament rank],
Find the result.



37.
There are n strings of length m+1,
If the last m character of a string matches the first m characters of a string, then two strings can be joined,
Ask these n strings to be a maximum number of strings and return an error if a loop occurs.



38.
Baidu interview:
1. Use the balance (can only be compared, not weigh) from a pile of small balls to find out the only one of the lighter, using X-times the balance,
You can find the lighter one from the Y ball and the relationship between Y and X.



2. There is a large and large input stream, so large that no memory can store it,
and enter only once, how to get M records randomly from this input stream.



3. A large number of URL strings, how to remove duplicates, optimize the complexity of time space



39.
NetEase Youdao Written test:
(1).
To find the maximum distance between any two nodes in a binary tree,
The two-node distance is defined as the number of edges between the two nodes,
For example, the distance between a child's node and the parent node is 1, and the distance between the adjacent sibling nodes is 2, which optimizes the complexity of the time space.



(2).
To find the cut point of a forward connected graph, the definition of a cut point is that if the node and its associated edges are dropped,
The undirected graph is no longer connected and describes the algorithm.



40. Baidu Research and development pen test
Referenced from: zp155334877
1) design a stack structure to satisfy the condition: the time complexity of the min,push,pop operation is O (1).



2) A string of beads (m) connected to the end, having N colors (n<=10),
Design an algorithm that takes out one segment, requires all n colors, and minimizes the length.
and analyze the complexity of time and space.



3) Design a system to deal with the problem of word collocation, for example, China and the people can match,
The Chinese people of China are effective. Requirements:



* The number of queries per second of the system may be thousands of thousand;
* The order of words is 10W;
* Each word can match up to 1W words



When the user enters the Chinese people, asks to return the information related to this collocation phrase.



41. Finding the Crystal Element Finder for solid crystal Machine
The wafer plate consists of an unknown number of crystal elements, which are not necessarily fully covered with the wafer plate,



The camera each time this can match a crystal, if matched, then pick up the crystal,
If matched, however, the camera is moved to the next position according to the measured wafer spacing.
The algorithm of traversing wafer disk is used to find the idea.



42. Please modify the Append function to use this function:



Two sets of non-descending lists, 1->2->3 and 2->3->5, and 1->2->3->5
You can only output results, and you cannot modify the data of two linked lists.



43. Recursive and non-recursive two methods to achieve the two-fork tree of the pre-sequence traversal.



44. Tencent interview Questions:
1. Design a Rubik's Cube (six sides) of the program.
2. There are 10 million SMS, there are duplicates, in the form of a text file saved, one line, there are duplicates.
Please use 5 minutes to find the top 10 duplicates.



3. I've got 10,000 URLs, and now I'm going to give you a URL, how to find a similar URL. (Interviewer does not explain what is similar)



45. Yahoo:
1. For an integer matrix, there is an operation that adds a moment to any element in the matrix, requiring it to be adjacent (up or down)



An element also adds one, now gives a positive matrix, to determine whether it can be obtained by a full 0 matrix through the above operation.
2. An array of integers with a length of n, dividing it into m parts, making each part equal to the maximum value of M
For example {3,2,4,3,6} can be divided into {3,2,4,3,6} m=1;
{3,6} {2,4,3} m=2
{3,3} {2,4} {6} m=3 so the maximum value for M is 3



46. Sohu:
Four pairs of parentheses can have a number of matching arrangements. For example, two pairs of parentheses can have two types: () () and (())



47. Innovation Workshop:
Find the longest descending subsequence of an array, such as {9,4,3,2,5,4,3,2}, the longest descending subsequence of {9,5,4,3,2}



48. Microsoft:
An array is formed by moving several bits to the left of a descending sequence, such as {4,3,2,1,6,5}
is formed from {6,5,4,3,2,1} left two bits, in which a number is found.



49. A seemingly scary algorithm interview question:
How to sort n numbers, requiring time complexity O (n), Spatial complexity O (1)



50. NetEase Youdao Written test:
1. To find the maximum distance between any two nodes in a binary tree, the distance of two nodes is defined as the number of edges between these two nodes,
For example, the distance between a child's node and the parent node is 1, and the distance between the adjacent sibling nodes is 2, which optimizes the complexity of the time space.



2. To find the cut point of a forward connected graph, the definition of the cut point is that if the node and its associated edges are removed, the graph is no longer connected and describes the algorithm.



51. And is a continuous positive sequence of N.
Title: Enter a positive n, output all and n consecutive positive sequence.



For example, input 15, because of 1+2+3+4+5=4+5+6=7+8=15, so output 3 sequential sequence 1-5, 4-6 and 7-8.
Analysis: This is a question of netease.



52. The depth of the two-dollar Tree.



Title: Enter the root node of a two-dollar tree to find the depth of the tree.



A path from the root node to the leaf node followed by a node (with root and leaf nodes) to form a tree, the length of the longest path is the depth of the tree.



For example: Enter a two-tuple tree:
10
/ \
6 14
/ / \
4 12 16



Outputs the depth of the tree 3.



The nodes of a binary tree are defined as follows:



struct Sbinarytreenode//A node of the binary tree
{
int m_nvalue; Value of Node
Sbinarytreenode *m_pleft; Left child of Node
Sbinarytreenode *m_pright; Right Child of Node
};
Analysis: The problem is still to examine the traversal of the two-yuan tree in nature.



53. The arrangement of strings.
Title: Enter a string to print out all the permutations of the characters in the string.
For example, the input string abc, the output of the characters A, B, C can be arranged out of all the strings
ABC, ACB, BAC, BCA, Cab, and CBA.



Analysis: This is a good test of recursive understanding of programming problems,
Therefore, in the past year, frequently appeared in the major companies in the interview, written questions.



54. Adjust the array order so that the odd digits are preceded by even numbers.



Title: Enter an array of integers to adjust the order of the numbers in the array so that all the odd digits are in the first half of the array,



All even digits are in the second half of the array. Requires a time complexity of O (n).



55.



Title: the Declaration of class cmystring is as follows:
Class CMyString
{
Public
CMyString (char* pData = NULL);
cmystring (const cmystring& str);
~cmystring (void);
cmystring& operator = (const cmystring& str);



Private
char* m_pdata;
};
Implement the overloaded function of its assignment operator, which requires exception security, that is, if an exception occurs when an object is assigned, the state of the object cannot be changed.



56. The longest common string.



Title: If all the characters of string one appear in the order of the strings in the other string two,



The string is called a substring of string two.



Note that a character that does not require a substring (string one) must appear consecutively in string two.
Write a function, enter two strings, ask for their longest common substring, and print out the longest common substring.



For example: Enter two strings Bdcaba and Abcbdab, string BCBA and Bdab are their longest common substrings,
Then output their length 4 and print any substring.



Analysis: Finding the longest common substring (longest Common subsequence, LCS) is a very classic dynamic programming problem,



So some companies that pay attention to algorithms like MicroStrategy are using it as a face test.



57. Implement the queue with two stacks.



Title: The declaration of a queue is as follows:



Template class Cqueue
{
Public
Cqueue () {}
~cqueue () {}


  void Appendtail (const t& node); Append a element to tail
  void Deletehead ();               Remove a element from head


Private
T> M_stack1;
T> M_stack2;
};



Analysis: From the declaration of the class above, we found that there are two stacks in the queue.
So this problem essentially requires that we use two stacks to implement a queue.
I believe you know the basic nature of stacks and queues: The stack is a back-to-first-out data container,
So the insertion and deletion of the queue is done on top of the stack, and the queue is a first-in, first-out data container.
We always insert new elements into the tail of the queue and remove elements from the head of the queue.



58. Output the linked list from the tail to the head.



Title: Enter the head node of a linked list, which in turn outputs the value of each node from the end of the tail point. The linked list node is defined as follows:
struct ListNode
{


  int       M_nkey;
  listnode* M_pnext;


};
Analysis: This is a very interesting face test.
The problem and its variants are often seen in the interviews and written questions of major companies.



59. Classes that cannot be inherited.
Title: Design a class in C + + that cannot be inherited.



Analysis: This is the latest question for Adobe's 2007 campus recruitment.
This problem in addition to examining the basic skills of C + + candidates, but also to examine the response ability, is a very good topic.



60. Delete the linked list node within O (1) time.



Title: The head pointer of the given list and a node pointer, at O (1) Time to delete the node. The linked list node is defined as follows:



struct ListNode



{


  int        M_nkey;

  listnode* M_pnext;


};



The declaration of the function is as follows:
void Deletenode (listnode* plisthead, listnode* ptobedeleted);



Analysis: This is a widely circulated Google interview problem, can effectively examine our programming skills, but also to examine our response speed, More importantly, we can also examine our understanding of the complexity of time.



61. Find two occurrences of a number in an array
Title: In an integer array, except for two digits, the other numbers appear two times.
Please write the program to find the two only occurrences of the number. The required time complexity is O (n), and the spatial complexity is O (1).



Analysis: This is a very novel on the bit operation of the face question.



62. Find out the first common node of the list.
Title: Two one-way lists to find their first common node.



The nodes of a linked list are defined as:
struct ListNode



{


  int         M_nkey;

  listnode*   M_pnext;


};



Analysis: This is a Microsoft face test. Microsoft is very fond of topics related to linked lists,
So in Microsoft's interview problem, the probability of the list appearing is quite high.



63. Remove a specific character from the string.
Title: Enter two strings and remove all characters from the second string from the first string. For example, enter "they is students." and "Aeiou",



The first string after deletion becomes "Thy R stdnts."



Analysis: This is a Microsoft face test. In Microsoft's common interview questions, string-related topics account for a large part of the
Because the writing program operation string can reflect our programming basic skill very well. Look for the ugly number.
Title: We refer to numbers that contain only factors 2, 3, and 5 as ugly numbers (Ugly number). For example, 6, 8 are ugly numbers,
But 14 is not, because it contains factor 7. We used to think of 1 as the first ugly number.
Find the 1500th ugly number in order from small to large.



Analysis: This is a network of widely circulated on the surface of the question, it is said that Google has used this problem.



65. Output 1 to the maximum number of n digits
Title: Enter the number n, output sequentially from 1 to the largest n bits of the 10 binary. For example, enter 3,



The output is 1, 2, and 31 until the maximum 3 digits is 999.
Analysis: This is a very interesting topic. It looks very simple, but in fact there are a lot of mystery.



66. Reverse the stack.
Title: Reverse a stack with recursion. For example, input stack {1, 2, 3, 4, 5},1 at the top of the stack.
The stack after upside down is {5, 4, 3, 2, 1},5 at the top of the stack.



67. Two busy play entertainment.



1. Playing cards of the straight son
Randomly draw 5 cards from poker, judging whether it is a straight, that is, the 5 cards are not continuous.
2-10 is the number itself, A is 1,j to 11,q for 12,k to 13, and the size King can be regarded as any number.



The number of points in 2.N dice.
Throw n dice on the ground, and the sum of the points on the top side of the dice is S. Enter N,
Prints out the probability that all possible values of s appear.



68. Arrange the array to the smallest number.
Title: Enter an array of positive integers, concatenate them into a number, and output the smallest of all the numbers that can be drained.
For example, the input array {32, 321}, the output of these two can be ranked as the smallest number 32132.
Please give the algorithm to solve the problem and prove the algorithm.



Analysis: This is a June 09 Baidu interview problem,
From this question we can see Baidu on the candidate in the algorithm has a very high demand.



69. Rotate the smallest element in the array.
Title: Move a number of elements at the beginning of an array to the end of the array, which we call the rotation of the array. Enter a rotation of an orderly array,



Outputs the smallest element of the rotated array. For example, the array {3, 4, 5, 1, 2} is a rotation of {1, 2, 3, 4, 5}, and the minimum value of the array is 1.


Analysis: The most intuitive solution to this problem is not difficult. By iterating through the array once, you can find the smallest element,


The complexity of time is obviously O (N). But this idea does not take advantage of the characteristics of the input array, we should be able to find a better solution.



70. Give a function to output all permutations of a string.
ANSWER simple backtracking can be achieved. Of course there are many kinds of algorithms for the generation of permutations, to look at combinatorial mathematics,



There are also reverse-order generation permutations and some methods that do not require recursive generation of permutations.
The impression of the first volume of Knuth in-depth about the formation of the arrangement. The understanding of these algorithms requires a certain mathematical foundation,
Also need some inspiration, interested in the best look.



71. The integer value of the number of square.



Title: Implement the function double Power (double base, int exponent), to find the exponent of the base.
There is no need to consider overflow.



Analysis: This is a seemingly simple question. There may be a lot of people who see the topic 30 seconds after writing the following code:
Double Power (double base, int exponent)
{


  Double result = 1.0;
  for (int i = 1; I <= exponent; ++i)
        result *= base;
  return result;


}



72.
Title: Designing a class, we can only generate an instance of the class.
Analysis: A class that can generate only one instance is a type that implements the singleton pattern.



73. Maximum length of the action string.



Title: Enter a string that outputs the maximum length of the symmetric substring in the string.
For example, the input string "Google", because the longest symmetric substring in the string is "goog", so output 4.



Analysis: Many people may have written a function that determines whether a string is symmetric, and this topic can be seen as a reinforced version of the function.



74. More than half the number of occurrences in the array



Title: There is a number in the array that appears more than half the length of the array to find this number.



Analysis: This is a widely circulated face test, including Baidu, Microsoft and Google, many companies are
Once used this topic. It takes a couple of 10 minutes to answer the question nicely.
In addition to good programming ability, but also need a faster response and strong logical thinking ability.



75. Minimum common parent node for two fork tree two nodes
Title: Two the node of the fork tree is defined as follows:
struct TreeNode
{


int m_nvalue;
treenode* M_pleft;
treenode* M_pright;


};



Enter the two nodes in the binary tree, and output the two nodes at the lowest common parent node in the number.
Analysis: Finding the lowest common node of two nodes in a number is a frequent problem in the interview. There are at least two variants of the problem.



76. Replication of complex linked lists



Title: There is a complex list of nodes, with the exception of a m_pnext pointer pointing to the next node.
There is also a m_psibling point to any node in the list or null. The C + + definition of its node is as follows:
struct Complexnode
{
int m_nvalue;
complexnode* M_pnext;
complexnode* m_psibling;
};



The following figure is a complex linked list of this type that contains 5 nodes.
The solid arrow in the figure represents the M_pnext pointer, and the dashed arrow indicates the m_psibling pointer. For simplicity's sake,
A pointer to null is not drawn.
Please complete the function complexnode* clone (complexnode* phead) to copy a complex linked list.



Analysis: A slight change in the common data structure, this is a very new face test.
To solve this type of problem in less than one hours, we need a quicker response capability,
A thorough understanding of the data structure and solid programming skills.



77. The interview topics on linked list issues are as follows:



1. Given a single-linked list, detect if there is a ring.



Using two pointers p1,p2 from the linked list head, p1 each step forward, P2 each advance two steps. If P2 reaches the tail of the list,



Description without ring, otherwise P1, P2 will inevitably meet at some point (P1==P2), thus detecting the link list in the ring.



2. Given two single-linked lists (Head1, head2), detects if there is an intersection of two linked lists, and returns the first intersection if there is one.


    If head1==head2, then apparently intersect, return directly to Head1.


Otherwise, from head1,head2 start traversing two linked lists to get their lengths len1 and Len2, assuming Len1>=len2,
Then the pointer p1 from head1 to move backward len1-len2 step, the pointer p2=head2,
Below P1, p2 each backward and compares p1p2 for equality, and returns the node if equal.
Otherwise, the two linked lists do not have intersections.



3. Given a single-linked list (head), if there is a loop, return to the first node of the ring from the top.
Using problem one, we can check whether there are loops in the list.
If there is a ring, then the P1P2 coincident point P must be in the ring. Disconnect the ring from P point,
The methods are: P1=p, P2=p->next, P->next=null. At this point, the original single-linked list can be regarded as two single-linked list,
One begins with the head and the other begins with the P2, so using the method of two, we find the first intersection of them to be asked.



4. Delete this node only by giving a pointer to a node p (not the last node, or p->next!=null) in the order list.
The method is very simple, the first is to put the data in P, and then copy the P->next data into P, then delete the P->next.



5. Insert a node in front of P only to a node in the order list, p (non-null node).
The method is similar to the former, first assigning a node q, the q is inserted after p, then the data in P is copied into Q,
The data that will be inserted is then recorded in P.



78. What is the difference between a linked list and an array?



Analysis: Mainly in the basic concept of understanding.
But the best thing to think about is that the competition for hiring now may be in the details,
Who is more careful, who wins the opportunity is big.



79.
1. Write an algorithm for sorting the list. Explain why you would choose to use this method.
2. Write an algorithm that implements array sorting. Explain why you would choose to use this method.
3. Write code that directly implements the function of the STRSTR () function.



80. Alibaba a pen test



Problem Description:
12 tall and different people, lined up in two rows, each row must be from short to high, and the second parallelism corresponds to the first row of people high, ask how many kinds of arrangement?
This pen question is very yd, because it hides a recursive relationship very deeply.


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.