A personal guide to software engineering technical interviews

Link address: https://github.com/kdn251/interviews/blob/master/README-zh-cn.md

data structure

Linked List

  • A linked list is a linear collection of nodes, and each node can use pointers to point to other nodes. It is a data structure containing multiple nodes that can be used to represent sequences.
  • Singly Linked List : Nodes in the linked list only point to the next node, and the last node points to null.
  • Doubly linked list : where each node has two pointers p, n, such that p points to the previous node and n points to the next node; the n pointer of the last node points to null.
  • Circular linked list : A linked list where each node points to the next node and the last node points to the first node.
  • time complexity:
    • index: O(n)
    • search: O(n)
    • insert: O(1)
    • remove: O(1)

Stack

  • A stack is a collection of elements, which consists of two basic operations: the push operation can be used to push elements onto the stack, and the pop operation can remove the top element from the stack.
  • Follow the Last In First Out (LIFO) principle.
  • time complexity:
  • index: O(n)
  • search: O(n)
  • insert: O(1)
  • remove: O(1)

Queue

  • A queue is a collection of elements that consists of two basic operations: the enqueue operation can be used to insert elements into the queue, and the dequeeu operation can be used to remove elements from the queue.
  • Follow the first-in, first-out principle (FIFO).
  • time complexity:
  • index: O(n)
  • search: O(n)
  • insert: O(1)
  • remove: O(1)

Tree

  • A tree is an undirected, connected, acyclic graph.

Binary Tree

  • A binary tree is a tree data structure in which each node contains at most two nodes, a left child node and a right child node.
  • Full binary tree : Each node in the tree contains only 0 or 2 nodes.
  • Perfect Binary Tree : Each leaf node in a binary tree has two child nodes with the same height.
  • Complete binary tree : The number of nodes on each layer except the last layer reaches the maximum value; only a few nodes on the right are missing on the last layer.

Binary Search Tree

  • A Binary Search Tree (BST) is a special kind of binary tree whose value in any node is greater than or equal to the value stored in its left subtree and less than or equal to the value stored in its right subtree.
  • time complexity:
    • index: O(log(n))
    • search: O(log(n))
    • insert: O(log(n))
    • delete: O(log(n))

Binary Search Tree

Trie

  • Dictionary trees, also known as radix trees or prefix trees, can be used to store dynamic collections whose keys are strings or search trees of associative arrays. The node in the tree does not directly store the associated key value, but the mount position of the node in the tree determines its associated key value. All child nodes of a node have the same prefix, and the root node of the entire tree is the empty string.

Alt text

Fenwick Tree

  • A tree-like array, also known as Binary Indexed Tree, is expressed in the form of a tree, but is essentially implemented as an array. The subscripts in the array represent the vertices in the tree, and the subscripts of the parent or child nodes of each vertex can be obtained by bitwise operations. Each element in the array contains the sum of the precomputed interval values, which are also updated during the entire tree update process.
  • time complexity:
    • Interval evaluation: O(log(n))
    • renew: O(log(n))

Alt text

Segment Tree

  • Line segment tree is a tree data structure used to store intervals or line segments, which allows to quickly find the number of times a node appears in several line segments.
  • time complexity:
    • Interval query: O(log(n))
    • renew: O(log(n))

Alt text

Heap

  • A heap is a special tree-based data structure that satisfies certain characteristics. The key values ​​of all parent and child nodes in the entire heap will satisfy the same sorting conditions. The heap can be more accurately divided into the maximum heap and the minimum heap. In the maximum heap, the key value of the parent node is always greater than or equal to the value of the child node, and the maximum value in the entire heap is stored in the root node; while in the minimum heap, the parent The key value of a node is always less than or equal to the key value of its child nodes, and the minimum value in the entire heap is stored in the root node.
  • time complexity:
    • access: O(log(n))
    • search: O(log(n))
    • insert: O(log(n))
    • remove: O(log(n))
    • Remove max/min: O(1)

Max Heap

Hashing

  • A hash can map data of arbitrary length to data of fixed length. The hash function returns the hash value. If two different keys get the same hash value, this phenomenon is called collision.
  • Hash Map : Hash Map is a data structure that can establish the relationship between keys and values. Hash Map can use hash functions to convert keys into subscripts in buckets or slots, thereby optimizing the search speed for target values.
  • collision resolution
    • Separate Chaining : In the chain address method, each bucket is independent of each other and contains a list of a series of indexes. The time complexity of the search operation is the sum of the time to search the bucket (fixed time) and the time to traverse the list.
    • Open Addressing : In the Open Addressing method, when a new value is inserted, it will be judged whether the hash bucket corresponding to the value exists. If it exists, the next possible location will be selected in turn according to a certain algorithm until a new value is found. Addresses that are not yet occupied. The so-called open address method also means that the location of an element is not always determined by its hash value.

Alt text

Graph

  • A graph is an abstract data type composed of a many-to-many relationship between data elements and a set of basic operations.
    • Undirected Graph : An undirected graph has a symmetric adjacency matrix, so if there is an edge from node u to node v, there is also an edge from v to u.
    • Directed Graph : The adjacency matrix of a directed graph is asymmetric, that is, if there is an edge from u to v, it does not mean that there must be an edge from v to u.

Graph

algorithm

sort

quick sort

  • Stable: No
  • time complexity:
    • Best time: O(nlog(n))
    • Worst time: O(n^2)
    • Average time: O(nlog(n))

Alt text

merge sort

  • Merge sort is a typical divide and conquer algorithm, which continuously divides an array into two parts, sorts the left subarray and the right subarray respectively, and then merges the two arrays into a new sorted array.
  • Stable: yes
  • time complexity:
    • Best time: O(nlog(n))
    • Worst time: O(nlog(n))
    • Average time: O(nlog(n))

Alt text

bucket sort

  • Bucket sort divides an array into a finite number of buckets. Each bucket is then sorted individually (possibly using another sorting algorithm or recursively continuing to use bucket sort to sort).
  • time complexity:
    • Best time: Ω(n + k)
    • Worst time: O(n^2)
    • Average time:Θ(n + k)

Alt text

radix sort

  • Radix sort is similar to bucket sort, which divides the array into a limited number of buckets; however, it does not sort each bucket individually after division, but directly merges.
  • time complexity:
    • Best time: Ω(nk)
    • Worst time: O(nk)
    • Average time: Θ(nk)

Graph Algorithms

depth-first search

  • A depth-first algorithm is an algorithm that traverses child nodes first rather than backtracking.
  • time complexity: O(|V| + |E|)

Alt text

Breadth-first search

  • Breadth-first search is a graph traversal algorithm that preferentially traverses neighbor nodes rather than children.
  • time complexity: O(|V| + |E|)

Alt text

topological sort

  • Topological sorting is a linear sorting of directed graph nodes. If there is an edge from u to v, the subscript of u is considered to be before v.
  • time complexity: O(|V| + |E|)

Dijkstra's algorithm

  • Dijkstra's algorithm is  used to compute the single-source shortest path problem in directed graphs.
  • time complexity: O(|V|^2)

Alt text

Bellman-Ford algorithm

  • The Bellman-Ford algorithm is an algorithm for calculating the shortest path from a single source point to other nodes in a weighted graph.
  • Although the algorithm is more complex than Dijkstra's algorithm, it is suitable for graphs that contain negative edges.
  • time complexity:
    • Best time: O(|E|)
    • Worst time: O(|V||E|)

Alt text

Floyd-Warshall algorithm

  • The Floyd-Warshall algorithm  can be used to find the shortest path to any node in an acyclic weighted graph.
  • time complexity:
    • Best time: O(|V|^3)
    • Worst time: O(|V|^3)
    • Average time: O(|V|^3)

Prim algorithm

  • Prim's algorithm is a greedy algorithm for computing a minimum spanning tree in a weighted undirected graph. In other words, Prim's algorithm can extract the minimum-cost subset of edges connecting all nodes in the graph.
  • time complexity: O(|V|^2)

Alt text

Kruskal's algorithm

  • Kruskal's algorithm is also an algorithm for calculating the minimum spanning tree of a graph. The difference from Prim is that the graph does not need to be connected.
  • time complexity: O(|E|log|V|)

Alt text

bit operation

  • Bit operation is the technology of operating at the bit level, and proper bit operation can help us get faster operation speed and smaller memory usage.
  • Test the kth bit: s & (1 << k)
  • Set the kth bit: s |= (1 << k)
  • The kth position is zero: s &= ~(1 << k)
  • Toggle the kth bit value: s ^= ~(1 << k)
  • Multiply by 2: s << n
  • Divide by 2: s >> n
  • Intersection: s & t
  • Union: s | t
  • Subtraction: s & ~t
  • exchange x = x ^ y ^ (y = x)
  • Extract lowest set bit: s & (-s)
  • Extract lowest unset bit: ~s & (s + 1)
  • Swap value: x ^= y; y ^= x; x ^= y;

Algorithm Complexity Analysis

Big O means

  • Big-O notation is  used to represent the upper bound of an algorithm, and is often used to describe the worst case.

Alt text

Small O means

  • Small O represents an asymptotic upper bound that describes an algorithm, but the two are tighter.

Big Ω means

  • Large Ω represents an asymptotic lower bound used to describe an algorithm.

Alt text

Small ω means

  • Little Omega Notation is used to describe the lower bound of a particular algorithm, but not necessarily very close.

Theta Θ means

  • Theta Notation is used to describe the bounds of a certain algorithm.

Alt text

video tutorial

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326489846&siteId=291194637