C + + face question "turn"

Source: Internet
Author: User
Tags rehash

Language section:
    • virtual function, polymorphic. This concept is almost necessarily asked.
    • The use of STL and the data structure behind it, vector string map set and Hash_map,hash_set
    • implement a stack class similar to the one in STL. This topic is very simple at first, I was a little disdain, how to come up with such a simple question. But the code written by C + + and people who have not written C + + can see the difference at a glance. For example, the three major functions have no write, the use of references are very critical. If there is no experience in this area, it is recommended to read the implementation of the simple data structure in the book http://book.douban.com/subject/1971825/, the details are critical
    • Part C: Declaration of a function pointer
    • Multithreading in C + +
Algorithm section:
  • Determine if a single-linked list has a loop (method of fast or slow pointer)
  • How to judge a pair of Mahjong card Hu (The method of return )
  • Binary search tree, the node has been labeled with numbers, how to find the minimum two nodes of the common node (find the first node in the middle of two nodes, such as two nodes are 3, 8, find the value between 3, 8 node, if not found, then 3, 8 one of which is another parent node)
  • Binary tree, how to find the minimum two nodes of the common node (the more stupid way is to find the path from root to two nodes, and then two paths from the root of the last same node is the desired node.) A reference LCA algorithm for subtle points)
  • The shortest digest is generated with an array and a collection that looks for the shortest substring that contains all the elements of the collection in the array. See the beauty of programming in detail 3.5
  • Find the largest number of K, programming beauty 2.5
  • How to determine the relevance of two pages (if the interview search related only to see the Wu of the mathematical beauty of great benefit, where the vector space model is very beautiful,TF and IDF These terms must also know)
  • The relationship between the average search length and the filling factor of the hash (a good aftertaste of the data structure of the book bar) hash table (equal probability case) find success and find the average length of unsuccessful search
      • Reload factor load factorλ: The ratio of the elements in the hash list to the size of the hash table.
      • Constant time execution Insert Delete lookup, hash function, resolve conflict: Detach link method, λ≈1, do not use separate connection, try to make λ<0.5 as efficient as possible
  • Consistent hash principle consistent hashing algorithm (consistent hashing)
  • Rehash may lead to a particularly long response time for inserted elements, is there any better way?
      • You can refer to the rehash implementation of Redis, when it is time to rehash, instead of migrating all the data to another larger hash table at a time, instead of migrating one bucket at a time, it can be split
  • How to compress all 01 files, there is no good answer. How high-compression files are implemented

System Design Section:
    • How to speed up the response when the system's processing request time is certain. (The use of the cache), it was silly, unexpectedly this, I give the answer is to increase the size of the system, which can only reduce the request waiting time. Sometimes the interviewer will short-circuit, Google's question, think for a long while to think out
    • Two virtual network cards to communicate, but need data encryption, how to achieve data encryption. It feels like SSL.
Programming section:
    • Binary search (High frequency)
    • Build the heap , by the way, the time complexity of the building is O (n), not log, proving that the introduction of the algorithm can be used to prove the time complexity of the binary heap construction
    • Write a function that reverses the string by reversing it as follows: "I am a student" is reversed to "student a AM I", without any library functions (the frequency is super high, first reverse the entire string, and then reverse each word )

Detailed analysis 2. Standard associative container set, multiset, map, Multimap
the internal use of a very efficient balanced retrieval binary tree: Red-black tree, also become RB tree (red-blacktree). The statistical performance of RB trees is better than that of normal balanced binary trees.
3.STL map and set are difficult to understand in the use of
    • Map:type Pair<constkey, t>
        many different const keys correspond to a collection of T-objects, all of the recordset as long as the const key is not the same, t irrelevant!
    • Set:type Const Key
        save only a single pair of const keys, no map of the T-like! Can be seen as a special case of map
(1) Why is the insertion and deletion efficiency of map and set higher than with other sequence containers? , tree Answer: Because there is no need for memory copy and memory movement for the associated container. That's right, that's true. All elements within the map and set containers are stored as nodes, with a node structure that is similar to the linked list, pointing to the parent and child nodes (2) Why did the previously saved iterator not expire after each insert? A: Iterator here is a pointer to the node, the memory is not changed, the pointer to the memory is how to invalidate it (of course, the deleted element itself has been invalidated). Each time the pointer is deleted and inserted, it is possible for the cursor to fail relative to the vector, and the call Push_back at the end of the insertion. Because in order to ensure the continuous storage of internal data, the iterator pointed to the block within the deletion and insertion process may have been overwritten by other memory or memory has been freed. Even when the push_back, the container internal space may not be enough, need a new larger memory, only the previous memory freed, request new larger memory, copy the existing data elements to the new memory, and finally put the elements need to be inserted into the last, then the previous memory pointer is naturally unusable. In particular, when working with algorithms such as find, keep this principle in mind: do not use outdated iterator. (3) Why map and set cannot have a reserve function like a vector to pre-allocate data? A: As I have said before, the rationale for this is that it is not the element itself that is stored inside the map and set, but rather the node that contains the element. That is to say, map internal use of the Alloc is not Map<key, Data, Compare, alloc> declaration of the time from the parameters of the incoming Alloc.
4.set, Multiset
set and multiset automatically sort elements according to specific sorting criteria, the elements in set are not allowed to be duplicated, and multiset can be repeated. because it is ordered, the elements in the set cannot be modified, only deleted and then added. the element types added to the set must be overloaded with the < operator for sorting. Sorting meets the following guidelines:
      1. Asymmetric, if the a<b is true, then the B<a is false.
      2. Can be passed, if a<b,b<c, then a<c.
      3. A<a is always false.
set determines whether the elements are equal: if (!) ( A<b | | B<a)), when both A<b and B<a are false, they are equal.
5.map,multimap
map and Multimap the pair of key and value as an element, automatically sorts the elements according to the ordering criteria of key, the key of the elements in the map does not allow repetition, multimap can be repeated.
      • map<key,value>  
because it is sort, the key of the element in the map cannot be modified, but can only be deleted and then added. The value corresponding to key can be modified. The key type of the element added to the map must be overloaded with the < operator for sorting. The sort is consistent with the set rule.
6. Function method of List
there are actually two kinds of lists: One is the basic ArrayList, the advantage is the random access element, the other is the more powerful linkedlist, it is not for the fast random access design, but has a more general method. List: Order is the most important feature of the list: it ensures that the elements are maintained in a specific order. The list adds a number of methods to collection, allowing you to insert and remove elements to the middle of the list (this is recommended for linkedlist use only. A list can generate Listiterator, which can be used to traverse a list in two directions or to insert and remove elements from the middle of a list.
ArrayList: A list implemented by an array. Allows fast random access to elements, but inserts and removes elements in the middle of the list is slow. Listiterator should only be used to traverse the ArrayList backward, not to insert and remove elements.   Because that's much more expensive than linkedlist. LinkedList: Sequential access is optimized, and the cost of inserting and deleting to the list is small. Random access is relatively slow. (use ArrayList instead.) ) also has the following methods: AddFirst (), AddLast (), GetFirst (), GetLast (), Removefirst (), and Removelast (), which are not defined in any interface or base class Enables LinkedList to be used as stacks, queues, and bidirectional queues
What is the difference between 7.1 hash_map and map?
constructor function. Hash_map requires hash function, equals function; map only needs to compare functions (less than functions). Storage structure. Hash_map use hash table storage, map is generally used red-black tree (RB tree) to achieve. So its memory data structure is not the same.
7.2 When do I need to use hash_map and when do I need a map?
Overall, the Hash_map lookup speed is faster than map, and the lookup speed is basic and data data volume size, the lookup speed of a constant level map is log (n) level . Not necessarily the constant is smaller than log (n), hash and hash function time-consuming, understand, if you consider efficiency, especially when the element reaches a certain order of magnitude, consider hash_map. But if you are particularly strict about memory usage, and you want the program to consume as little memory as possible, be careful, hash_map may embarrass you, especially if your hash_map object is so special, you're more out of control, and Hash_map's construction is slow.
        do you know how to choose now? Weigh three factors: find speed, amount of data, memory usage. 8. Some recommendations for use:
Level
1-use as map only: static array level 2-preserves fixed-length data, and all traversal when used: Dynamic array (static array is also OK if the length is fixed at the beginning) level 3-the ability to save a variable length array, which requires a dynamic increase, focusing on the speed of finding data : Using Vectorlevel 3-Save variable length array, need to increase the ability of dynamic, focus on increasing the speed of deleting data: listlevel 4-complex operation of data, that is, the ability to add and delete data, but also good data access speed: Using Dequelevel 5- The data in the middle of the deletion operation is more: Using list, it is suggested that on the basis of sorting, the bulk of the operation efficiency can provide the maximum guarantee level 6-the above is not found suitable: The combination of STL container or set up a special data structure to achieve
9. Other
(1). Vector-an automatically growing array of
Vector<int>vec (10)//a container with 10 int elements
vector<float> VEC (10 , 0.5)//a container with 10 float elements, and their values are 0.5
vector<int>::iterator iter;
for
(iter = Vec.begin (); ITER! = Vec.end (); iter++)
{
//...
}
because the array grows only forward, the vector also provides only back-end insertions and back-end deletions, namely Push_back and Pop_back. Of course, in the front-end and in the middle to manipulate the data is also possible, with insert and erase, but the front end and the middle of the data operation will inevitably cause the movement of data blocks, which has a great impact on performance.
        
The biggest advantage is the ability to randomly access. Vector<t1>::iterator related methods are:
begin (): Used to obtain a pointer to the first element of the vector
end (): Used to get a pointer to that position after the last element of the vector, note that it does not point to the last element.
Erase (vector<t1>::iterator): Used to delete the element pointed to by the iterator that was passed in as a parameter.
(2). List-good at inserting and deleting linked lists
list<string>milkshakes; list<int> Scores;
push_back, Pop_backpush_front pop_front.
 
list is an implementation of a doubly linked list.
To
provide two-way traversal capability, the list is two more pointers to the front and back than the average data unit
.
 
An example of using iterator to delete an element
list<string> stringlist;
List<string>::iterator iter;
Advance (ITER, 5);//iterator points to the fifth element of Stringlist
stringlist.erase (iterator);//Delete
So after the delete operation, where does the iterator of the incoming erase () method point to? It points to the next element of the element pointed to before the delete operation.
(3). Deque-double-ended queue with advantages of both vector and list
(4). Summary comparison and general usage guidelines for these three templates
These three templates are part of the sequence class template, and you can see that they have a common approach
        • Size (): Get container size
        • Begin (): Gets a pointer to the first element within the container (the type of the pointer varies depending on the container)
        • End (): Gets a pointer to a position after the last element within the container
        • Erase (): Delete the element that the incoming pointer points to
        • Clear (): Clears all elements
        • = = Operator: Determines whether two containers are equal
        • = operator: Used to assign a value to a container
The
above three templates have their own characteristics
        • The data of the vector template is continuously arranged in memory, so the speed of the random access element (that is, accessed through the [] operator) is the fastest, and the array is consistent. Also because it is continuously arranged, it is slow to delete or add elements at a location other than the tail, so avoid this when using vectors.
        • The data of the list template is chained, so the element cannot be accessed randomly. Its advantage is that the speed at which the elements are deleted is added at any location.
        • Deque templates are implemented by linking several pieces of continuous data, thus balancing the characteristics of the above two containers


C + + face question "turn"

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.