Tencent interview questions

Source: Internet
Author: User
1. Design a cube program.
2. There are 10 million text messages, which are duplicated. They are saved as text files, one line at a time, and duplicated. Use five minutes to find the first 10 records that can be repeated at most.
3. I have added 10 thousand URLs to my favorites. Now I will give you a URL and how to find similar URLs. (The interviewer does not explain what is the cube of the first question in a similar category
It's actually quite easy!
First, there are four sides on each side. Therefore, we need to create four corresponding handles: top, bottom, left, and four non-faces, just as the two-way linked list node has both top and bottom sides, and then there is a square matrix in it, level 2 array. After adding an array, write a method function called rotation. The parameter is the row and column number/rotation. When the orientation is up or down, the row and column number is the row number, and when the orientation is left or right, the row and column number is the column number, this means to rotate a column of a row in a certain direction.
There are squares in the matrix and squares
Create six faces. Take the first face as the front and the bottom (take the second side) as the bottom, and the back and the bottom are the top, the top is the front, now back to the front, the front to the left is the left side, the left to the left is the back, the back to the left is the right side, the right to the left is the front ...... (You don't need to know what to do here)
After the six faces are created and connected to the upper, lower, and left sides, the program is complete.
Write the pseudocode below
Class square {
Color type color,
}

Class surface {
(Type,
(Type,
(Type) left,
(Type) Right side,

(Type) number row and column width = 3, // (3 or whatever you want)
(Type) Square Matrix [row and column width] [row and column width],
(Type) the box is rotated into the square bar [row and column width],

Method to rotate (type) numeric row and column numbers, (type) character, (type) squares into the column [row and column width]) // (1: Top, 2: bottom, 3: Left, 4: Right ))
Return
(Type) the box is rotated out of the square bar [row and column width]
{

If (rotate = ("Left" or "right ")){
Temporary square bar = square matrix [row and column numbers],

If (rotate = "Left "){
Square Matrix [row and column number] = returns a column number,

}
If (rotate = "right "){
Square Matrix [row and column number] = returns a column number,
}

}
Otherwise, if (rotate = ("up" or "down ")){
Temporary square bar = new square matrix [row and column width],
Loop (type) numeric row and column = 1 to row and column width ){
Temporary Square Bar [row and column] = square matrix [row and column] [row and column number],
Square Matrix [row and column] [row and column number] = spin into the column and column of the square [row and column],
}
}
Returns a temporary square,
}

}

Cube category {
(Type) area [6],

Create a square ()
Return null
{
Loop (type) number aspect = 1 to 6 ){
If (aspect No. <4 ){
Aspect [aspect No.]. Lower = aspect [aspect No. + 1],
} Otherwise, if (aspect No. <5 ){
Aspect [aspect number]. aspect left = aspect [5],
Aspect [aspect number]. Right = aspect [6],
If (aspect = 4 ){
Aspect [aspect No.]. Lower = aspect [1],
}
} Otherwise, if (aspect number <= 6 ){
Aspect [aspect number]. Bottom = aspect [2],
Aspect [aspect number]. aspect = [4],
}
}
}

Method: twist the cube (type) numeric area number, (type) character direction, (type) numeric row and column number)
Return null
{
(Type) character starts to rotate the surface name = aspect [surface number]
(Type) Temporary surface = aspect [surface number];
(Type) block temporary Square Bar [row and column width];
If (Direction = "TOP "){
Loop (type) number = 1 to 4 ){
Temporary square bar = temporary area. rotation (Direction, row and column number, temporary square bar ),
Temporary surface = top of temporary surface,
}
}
If (Direction = "bottom "){
Loop (type) number = 1 to 4 ){
Temporary square bar = temporary area. rotation (Direction, row and column number, temporary square bar ),
Temporary area = temporary area. Bottom,
}
}
If (Direction = "Left "){
Loop (type) number = 1 to 4 ){
Temporary square bar = temporary area. rotation (Direction, row and column number, temporary square bar ),
Temporary area = temporary area. Left,
}
}
If (Direction = "right "){
Loop (type) number = 1 to 4 ){
Temporary square bar = temporary area. rotation (Direction, row and column number, temporary square bar ),
Temporary surface = temporary surface. Right,
}
}
}
}
First, the length of 10 million text messages will not exceed 700 mb (350 Mb on average) based on the current SMS length. It is more appropriate to use memory ing files. it can be mapped at one time (of course, we can use field ing if there is a larger amount of data), because we do not need to frequently use file I/O and frequently allocate small memory, this greatly increases the data loading speed.
Second, the I (I from 0 to 70) characters of each text message are grouped by ASCII code, which is actually the creation tree. I is the depth of the tree and the I Letter of the text message.
// Tree node Definition
Struct tnode
{
Byte * ptext; // direct to the memory address mapped to the file. Using BYTE instead of char is a symbolic problem.
DWORD dwcount; // calculator that records the same number of messages at this node
Tnode * childnodes [256]; // The sub-node data. Because the ASCII value of a letter cannot exceed 256, the sub-node cannot exceed 256.

Tnode ()
{
// Initialize the Member
}
~ Tnode ()
{
// Release resources
}
};

// Byte * ptext directly points to the memory address mapped to the file. Using BYTE instead of char is a symbolic problem.
// Int nindex is a letter subscript
Void createchildnode (tnode * pnode, const byte * ptext, int nindex)
{
If (pnode-> childnodes [ptext [nindex] = NULL)
{// If this subnode does not exist, the. tnode constructor should have initialization code.
// For ease of processing, you can add this node to an array while creating it.
Pnode-> childnodes [ptext [nindex] = new tnode;
}

If (ptext [nindex + 1] = '/0 ')
{// The text message has been completed. Add 1 to the stenographer and save the text message content.
Pnode-> childnodes [ptext [nindex]-> dwcount ++;
Pnode-> childnodes [ptext [nindex]-> ptext = ptext;
}
Else // If (ptext [nindex]! = '/0 ')
{// If not, create the next level node
Createnode (pnode-> childnodes [ptext [nindex], ptext, nindex + 1 );
}
}

// Create the root node, ptexts is the SMS array, and dwcount is the number of SMS messages (10 million here)
Void createrootnode (const byte ** ptexts, DWORD dwcount)
{
Tnode rootnode;
For (DWORD dwindex = 0; dwindex <dwcount; dwindex ++)
{
Createnode (& rootnode, ptexts [dwindex], 0 );
}
// All nodes are sorted by the dwcount value.
// Code omitted...

// Obtain the first 10 nodes and display the result
// Code omitted...

}

This process looks complicated, in fact, to reduce the number of comparisons. I think you should be able to understand what I mean after reading this section of code.
Finally, sort these nodes by the dwcount value, and obtain the first 10 nodes.

In my opinion, this problem only involves two aspects: content loading and text message content comparison. the file memory ing technology can solve the performance problem of content loading (not only does not need to call the file I/O function, but also does not need to allocate a small block of memory for every text message read ), the use of the tree technology can effectively reduce the number of comparisons. of course, the basic idea is this. If you are in a mood, you can perform some optimization operations on this basis, so the effect will not be bad.

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.