Interview Calisthenics

Algorithms

Sort

A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most frequently used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the efficiency of other algorithms (such as search and merge algorithms). More formally, the output of any sorting algorithm must satisfy two conditions:
  1. The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order);
  1. The output is a permutation (a reordering, yet retaining all of the original elements) of the input.
Sorting algorithms are often classified by:
  • Memory usage (and use of other computer resources). In particular, some sorting algorithms are "in-place". Strictly, an in-place sort needs only O(1) memory beyond the items being sorted; sometimes O(log(n)) additional memory is considered "in-place".
  • Recursion. Some algorithms are either recursive or non-recursive, while others may be both (e.g., merge sort).
  • Whether or not they are a comparison sort. A comparison sort examines the data only by comparing two elements with a comparison operator.
  • General method: insertion, exchange, selection, merging, etc. Exchange sorts include bubble sort and quicksort. Selection sorts include shaker sort and heapsort.
  • Whether the algorithm is serial or parallel. The remainder of this discussion almost exclusively concentrates upon serial algorithms and assumes serial operation.
  • Adaptability: Whether or not the presortedness of the input affects the running time. Algorithms that take this into account are known to be adaptive.
Quick Sort
Pick up a “pivot” element and place all greater elements after the pivot. Recursively  apply algorithm to sub-arrays.
Merge Sort
Split list into elements and repeatedly merge them into one sorted list. 4x1→2x2→1x4→2x2 sorted→4x1 sorted

Heapsort
Builds max heap (binary sorting tree) from an ARRAY and than rebuilds heap popping  one element (root node) at a time.

Path-search

Shortest route between two points.
Dijkstra
Initial node (first current node) has 0 distance, all other nodes have infinite distances → calculate distance through current to neighbours, replacing old value if found is smaller → current node removed from scope, if it was destination - we are done, if all neighbours infinite - we are done → select nearest neighbour and move on.
A* search
f(n) = h(n) + g(n), where n is the next node on the path, g(n) is the cost of the path from the start node to n, and h(n) is a heuristic function that estimates the cost of the cheapest path from n to the goal. A* terminates when the path it chooses to extend is a path from start to goal or if there are no paths eligible to be extended.

Merge

Graph Traversal

The process of visiting (checking and/or updating) each vertex in a graph.