3 Frequent Data Structure Interview Questions and Answers

1. What is the difference between a stack and a queue?

Answer: The stack (stack) and the queue (queue) are both linear data structures. The stack is a last-in-first-out (LIFO) data structure, and the queue is a first-in-first-out (FIFO) data structure.

In a stack, the last element that came in is the first to be popped; in a queue, the first element that came in is the first to be popped.

2. What are the traversal methods of the binary tree?

Answer: There are three ways to traverse a binary tree: pre-order traversal, in-order traversal, and post-order traversal. Among them, pre-order traversal refers to visiting the root node first, and then recursively visits the left subtree and right subtree in turn; in-order traversal refers to recursively visiting the left subtree first, then visiting the root node, and finally recursively visiting the right subtree; post-order traversal is It refers to recursively visit the left subtree and right subtree first, and finally visit the root node.

3. What is a hash table? How to resolve hash collisions?

Answer: A hash table is a data structure that maps keys to values ​​through a hash function.

A hash table uses an array to store elements, and the position in the array is calculated by a hash function. A hash collision occurs when two different keys are mapped to the same location.

There are several methods to resolve hash collisions, two of which are more common are open addressing and chaining. The open addressing method means that when a hash conflict occurs, the next position in the array is probed backwards until a free position is found; while the chain method maintains a linked list at each position in the hash table, and the Elements with the same hash value are stored in this linked list.

Guess you like

Origin blog.csdn.net/xiangxin1030/article/details/130693085