Interview related questions: give you a text file, which stores 100 million QQ numbers, please use the program to order from small to large, Khan!

Source: Internet
Author: User
Interview question: give you a text file, which stores 100 million QQ numbers, please use the program to order from small to large, Khan Ah!..
Interview question: give you a text file, which stores 100 million QQ numbers, please use the program to order from small to large, Khan Ah!..
Ask the master to explain the idea!
There are other questions:
For example, the operating mechanism of memcache, its working principle it has what advantages and disadvantages!
There is an inventory of 100 products to be killed in seconds, in the second kill the number of seconds killed far more than inventory, how you will deal with, should pay attention to what problems!
Talk about your insight into MySQL optimization, or how you can design and optimize a database if you design it!
Have a master, today interview all oneself think answer is not too ideal, ask advice, and the following interview!
Younger brother's just been laid off, originally on the winter, really cold ah!


------Solution--------------------
There is no good way to sort the problem, is there any more specific constraints, such as run time and memory?
If you do not limit the direct sort function on the line, which is used in the fast sorting method (Quicksort), the theoretical efficiency should be the highest, and others are native code, how is also more than the PHP code to simulate a sorting algorithm faster.

The operating mechanism of memcache is to use the duty process to open up a memory space to hold key/value data, and all requests and applications share this data. The advantage is the fast access speed, suitable for caching frequently read and write data. The disadvantage is that it takes up memory and can only be retrieved by key, and cannot be queried by relational (SQL, etc.).

To ensure atomic operation, a certain locking mechanism is used to prevent multiple requests from manipulating one data at a time, resulting in a mismatch between the results and expectations.

MySQL database optimization is primarily an index and a table, for performance to index all fields that need to be sorted and retrieved, and to increase efficiency through horizontal or vertical table-splitting.
------Solution--------------------
The purpose is not to let you speculate, import the database, build the cable guide, but can mention


Iterate over, put the number by size, write the appropriate file ... For example, a 100,001 number segment is agreed

such as 10,000, written in the No. 0 file, 100,000,001, belongs to the 1th K file inside

Sort each file data, stitching files

When sorting, if the file is large, here according to the file size, probably can estimate the number of orders of magnitude. If the number is small, you can choose a fast row, otherwise,

Create an array of 100,000, traverse again, arr[qqnum-i*100000]+1;

Iterate through the array, increment the number by the array value,

Complexity is O (n), O (Nlogn)
------Solution--------------------
This question a bit familiar ah, algorithm plate seemingly discussed, so I answer with bitmap, space change time.
and the actual need to do may require segmentation, such as the 5-7-bit QQ direct bit hash,7-10 bit hash value + 1000000
PHP code
!--? phpset_time_limit (0);//5-7 bit qq$s = ' 0 '; $s { 9999999} = 1; $s {22334} = 1; $s {375345} = 1; $i = 10000;while (Isset ($s {$i})) {if ($s {$i} = = 1) echo "QQ: ". $i."                                                                                                                        
"; $i + +;}?
------Solution--------------------
discussion

This question is a bit familiar, the algorithm plate seems to have been discussed, so I replied with bi TMap, space change time.
and the actual need to do may require fragmentation processing, such as the 5-7-bit QQ direct bit hash,7-10 bit hash value + 1000000
PHP code
!--? php
set_time_limit (0);
//5-7-bit qq
$s = ' 0 ';
$s {9999999} = 1;
$s {22334} = 1;
...

------Solution--------------------
discussion

Sort phone numbers inside the programming Zhu Ji Nanxiong The truth should be the same. 100 million QQ number, need 100 million bit, probably 11MB
  • Related Article

    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.