# cards

front: What is Hamming Code?
back:
In telecommunication, Hamming codes are a family of linear error-correcting codes that generalize the Hamming(7,4)-code, and were invented by Richard Hamming in 1950. Hamming codes can detect up to two-bit errors or correct one-bit errors without detection of uncorrected errors.


front: What does ELF stand for?
back:
Executable and Linkable Format.
It's a common standard file format for executables, object code, shared libraries, and core dumps.


front: What is code generation?
back:
Producing a translation from a high-level program to assembly code. (Linker and Archiver taker over from here to produce machine code)


front: How can build heap be done in linear time?
back:
A tree of size n nodes, will have floor(n/2^h) nodes with height >= h.
The last half of nodes will be leaves, so they already satisfy the heap property. No work needs to be done on them.
going bottom-up (ignoring the last n/2 items) and satisfying the heap property one level at a time, each level going up the tree has to do at most 1 operation more than the level below it. But as you go up the tree, higher levels have fewer nodes, so you may be doing more operations, but it happens on fewer number of times.
This resembles a series:

n/2 - height 1: 1 operations
n/4 - height 2: 2 operation
n/8 - height 3: 3 operations
...
going to floor(n/2^h) - height h: h operations

n * (1/2 + 2/4 + 3/8 + 4/16 ....) = n * 1 = n


front: What is a Binary Search Tree?
back:
A binary tree is a data structure where each node has a comparable key and satisfies the restriction that the key in any node is larger than the keys in all nodes in that node's left subtree and smaller than the keys in all nodes in that node's right subtree.


front: What is a red-black tree?
back:
BSTs having red and black links satisfying:
- Red links lean left
- No node has two links connected to it
- The tree has perfect black balance: every path from the root to a null link has the same number of blacks


front: What is a splay tree?
back:
A self-adjusting binary search tree where recently accessed elements are moved to the root so they are quick to access again.


front: What is a treap?
back:
A random priority is assigned to every key and must maintain two properties:
-They are in order with respect to their keys, as in a typical binary search tree
-They are in heap order with respect to their priorities, that is, no key has a key of lower priority as an ancestor
O(log N) expected time for all operations, O(N) worst case.


front: What is typical cache line size?
back:
64 bytes.

-- extra below --

To know the sizes, you need to look it up using the documentation for the processor, afaik there is no programatic way to do it. On the plus side however, most cache lines are of a standard size, based on intels standards. On x86 cache lines are 64 bytes, however, to prevent false sharing, you need to follow the guidelines of the processor you are targeting (intel has some special notes on its netburst based processors), generally you need to align to 64 bytes for this (intel states that you should also avoid crossing 16 byte boundries).

To do this in C or C++ requires that you use aligned_malloc or one of the compiler specific specifiers such as __attribute__((align(64))) or __declspec(align(64)). To pad between members in a struct to split them onto different cache lines, you need on insert a member big enough to align it to the next 64 byte boundery


front: What is latency?
back:
Latency is the delay from input into a system to desired outcome. The time interval between between a stimulus and response.


front: What is a y-fast trie?
back:
A y-fast trie is a data structure for storing integers from a bounded domain. It supports exact and predecessor or successor queries in time O(log log M), using O(n) space, where n is the number of stored values and M is the maximum value in the domain. The structure was proposed by Dan Willard in 1982 to decrease the O(n log M) space used by an x-fast trie.


front: What is an x-fast trie?
back:
An x-fast trie is a data structure for storing integers from a bounded domain. It supports exact and predecessor or successor queries in time O(log log M), using O(n log M) space, where n is the number of stored values and M is the maximum value in the domain. The structure was proposed by Dan Willard in 1982, along with the more complicated y-fast trie, as a way to improve the space usage of van Emde Boas trees, while retaining the O(log log M) query time.


front: What is a van Emde Boas tree?
back:
The van Emde Boas tree supports insertions, deletions, lookups, successor queries, and predecessor queries in time O(log log U), where U is the universe of items to store. Items are stored in clusters of size sqrt(U). 

The van Emde Boas data structure divides the range {0,...,n−1} into blocks of size sqrt(n), which we call clusters. Each cluster is itself a vEB structure of size sqrt(n). In addition, there is a “summary” structure that keeps track of which clusters are nonempty.

More detail:
A van Emde Boas tree (or van Emde Boas priority queue), also known as a vEB tree, is a tree data structure which implements an associative array with m-bit integer keys. It performs all operations in O(log m) time, or equivalently in O(log log M) time, where M = 2m is the maximum number of elements that can be stored in the tree. The M is not to be confused with the actual number of elements stored in the tree, by which the performance of other tree data-structures is often measured. The vEB tree has good space efficiency when it contains a large number of elements, as discussed below. It was invented by a team led by Dutch computer scientist Peter van Emde Boas in 1975.


back: sed 's/ /\n/g' words.txt | sort
front:
Sed command to take a file separated by spaces, turn spaces into newlines, and then sort it alphabetically.


front: How does perfect hashing handle collisions?
back:
It creates a second hash table in the buckets where there are multiple items (k), using a second hash function, and k^2 space. The hash table has two hashing levels. k^2 is chosen because the Markov inequality (birthday paradox) ensures we will not have collisions in bucket.


front: What are the downsides of using an adjacency matrix to represent a graph?
back:
Finding all the outgoing edges from a vertex takes O(n) time even if there aren't very many, and the O(n^2) space cost is high for "sparse graphs," those with much fewer than n^2 edges.


front: When is using an adjacency list expensive?
back:
Finding predecessors of a node u is extremely expensive, requiring looking through every list of every node in time O(n + e), where e is the total number of edges, although if this is something we actually need to do often we can store a second copy of the graph with the edges reversed.


front: When are adjacency lists most useful?
back:
Adjacency lists are most useful when we mostly want to enumerate outgoing edges of each node. This is common in search tasks, where we want to find a path from one node to another or compute the distances between pairs of nodes. If other operations are important, we can optimize them by augmenting the adjacency list representation; for example, using sorted arrays for the adjacency lists reduces the cost of edge existence testing to O(log(d+ (u))), and adding a second copy of the graph with reversed edges lets us find all predecessors of u in O(d− (u)) time, where d− (u) is u's in-degree.


front: What is the complexity of Dijkstra's shortest-path algorithm?
back:
O(e log v), where e is the number of edges. 
It must scan each edge, and gets and updates values on the heap.


front: How would you implement a queue with a linked list?
back:
Use a tail pointer. Push new items at the tail, pop items at the head. Both operations are constant-time.


front: What can use to find if a graph is bipartite?
back:
BFS. Using only 2 colors. When you encounter a new vertex, if it has no color, give it the opposite color of its parent vertex. If it is already colored the same, the graph is not bipartite.


front: How can you find a cycle in a graph?
back:
DFS. If you discover an edge that connects to an ancestor (previously discovered vertex), you have a cycle.


front: How can you find an articulation vertex?
back:
DFS multiple times. Remove each edge one at a time, doing a DFS after each, so see if you end up with > 1 connected components. If you remove a node and then DFS and find you have fewer than m - 1 edges, you've deleted an articulation vertex. O(n(n+m))

A faster way, with a little more bookkeeping, can be done in O(n+m) time, if you do DFS and keep track of parents and make a note when you reach a back edge, which connects to an ancestor.


front:
How could you identify errors in a DNA fragment assembly given many pairs of sequences, where item A must appear before B in the larger sequence?

back:
Build a DAG representing all the left-right constraints. Any topological sort of the DAG is a consistent ordering. If there are cycles, there must be errors.


front: What should you avoid in your base case in recursion?
back:
Too many base case scenarios. Just have one base case so you can return as quickly as possible. Avoid "arm's length" recursion.


front: When talking dynamic programming, what is optimality?
back:
An algorithm has optimality if the subsolutions of an optimal solution of the problem are themsleves optimal solutions for their subproblems.


front: What is dynamic programming?
back:
Dynamic programming is a general-purpose algorithm design technique that is most often used to solve combinatorial optimization problems, where we are looking for the best possible input to some function chosen from an exponentially large search space.

There are two parts to dynamic programming. The first part is a programming technique: dynamic programming is essentially divide and conquer run in reverse: we solve a big instance of a problem by breaking it up recursively into smaller instances; but instead of carrying out the computation recursively from the top down, we start from the bottom with the smallest instances of the problem, solving each increasingly large instance in turn and storing the result in a table. The second part is a design principle: in building up our table, we are careful always to preserve alternative solutions we may need later, by delaying commitment to particular choices to the extent that we can.

The bottom-up aspect of dynamic programming is most useful when a straightforward recursion would produce many duplicate subproblems. It is most efficient when we can enumerate a class of subproblems that doesn't include too many extraneous cases that we don't need for our original problem.


front: What is a contiguously-allocated structures, and give examples.
back:
Contiguously-allocated structures are composed of single slabs of memory, and include arrays, matrices, heaps, and hash tables.


front: What are linked data structures and give examples.
back:
Linked data structures are composed of distinct chunks of memory bound together by pointers, and include lists, trees, and graph adjacency lists.


front: Why is memory locality important?
back:
Physical continuity between successive data accesses helps exploit the high-speed cache memory on modern computer architectures.


front: What are some advantages to linked lists over arrays?
back:
- Overflow on linked structures can never occur unless the memory is actually full.
- Insertions and deletions are simpler than for contiguous (array) lists.
- With large records, moving pointers is easier and faster than moving the items themselves.


front: What are some advantages to arrays over linked lists?
back:
- Linked structures require extra space for storing pointer fields.
- Linked lists do not allow efficient random access to items.
- Arrays allow better memory locality and cache performance than random pointer jumping.


back: How?
front:
Codeless question: Write a function to compare whether two binary trees are identical. Identical trees have the same key value at each position and the same structure.


back: How would you do it?
front:
Codeless Question: You are given a search string and a magazine. You seek to generate all the characters in search string by cutting them out from the magazine. Give an algorithm to efficiently determine whether the magazine contains all the letters in the search string.


front:
Codeless question: Give an algorithm for finding an ordered word pair (e.g., “New York”) occurring with the greatest frequency in a given webpage. Which data structures would you use?

back:
You could use a hash table, creating or updating an entry for each pair. 
Keep track of max_frequency and most_frequent_phrase.
Just increment the count, and when you see the new count is > than max_frequency, update max_frequency and most_frequent_phrase


front:
Codeless question: Given a set of n numbers, how do you find the pair of numbers that have the smallest difference between them?

back:
Sort them: Once the numbers are sorted, the closest pair of numbers must lie next to each other somewhere in sorted order. Thus, a linear-time scan through them completes the job, for a total of O(n log n) time including the sorting.


front: Codeless question: Are there any duplicates in a given set of n items?
back:
This is a special case of the closest-pair problem, where we ask if there is a pair separated by a gap of zero. The most efficient algorithm sorts the numbers and then does a linear scan though checking all adjacent pairs.


front:
Codeless question: Given a set of n items, which element occurs the largest number of times in the set? Bonus: How do you find out how many times some element k appears?

back:
If the items are sorted, we can sweep from left to right and count them, since all identical items will be lumped together during sorting.

To find out how often an arbitrary element k occurs, look up k using binary search in a sorted array of keys. Then use binary search in each direction to find where that run of the number begins and ends.


front:
Codeless question: Give an efficient algorithm to determine whether two sets (of size m and n, respectively) are disjoint.

back:
The small set can be sorted in O(m log m) time. We can now do a binary search with each of the n elements in the big set, looking to see if it exists in the small one. The total time will be O((n + m) log m).

This is better than sorting the larger array or sorting both sets and going through the list.


front: What is a uniform distribution?
back:
When a known finite number of outcomes are equally likely to occur. When graphed as a histogram of occurrences, it's a flat line. N items each have 1/n probability.


front: What is a normal distribution?
back:
The standard normal probability density function has the famous bell shape that is known to just about everyone.


front: Python: Default arguments are okay to use with the following caveat.
back:
Do not use mutable objects as default values in the function or method definition.

Lists are not ok.


front: What's the difference between foo.sort() and sorted(foo) where foo is a list?
back:
foo.sort() changes the list to its sorted state
sorted(foo) doesn't change foo, just returns a sorted list.


front: What's the probability of getting heads, tails, heads? P(HTH)
back:
1/8 
= 1/2 * 1/2 * 1/2
the ordering of HTH doesn't matter. It could be TTH or HHH or TTT, they are tall the same.


back: 26*26*26 = 26^3 = 17,576
front:
You have 26 letters in an alphabet. How many ways you can arrange 3 letters from that alphabet where repeated letters is OK?


back: 26*25*24 = 26 permute 3 = 15,600
front:
You have 26 letters in an alphabet. How many ways you can arrange 3 letters from that alphabet without repeating a letter?


back: 26*25*24 / 3! = 2600
front:
Given an alphabet of 26 letters, how many times you can you choose 3 letters, if ordering of the letters doesn't matter?


back: 6 = 4 choose 2 = 4 * 3 / 2
front:
There are 4 people. How many ways can they shake each other's hands without shaking the same person's hand twice?


front: What does NP mean? What is an NP algorithm?
back:
NP is the set of decision problem solvable in non-deterministric polynomial time. An NP problem can be solved by a lucky algorithm that magically always finds a yes decision. NP problems can be checked in polynomial time.


front: What makes a problem NP-Complete?
back:
If x is an element of NP and of NP-hard. NP-Complete is the narrow intersection of NP and NP-hard.

Because NP is such a large class of problems, most NP-hard problems you encounter will actually be complete.


front: What is a weak reference in Python?
back:
A weak reference to an object does not affect its reference count. 
When the only remaining references to a referent are weak references, garbage collection is free to destroy the referent and reuse its memory for something else. A primary use for weak references is to implement caches or mappings holding large objects, where it’s desired that a large object not be kept alive solely because it appears in a cache or mapping.


front: How does garbage collection work in CPython?
back:
CPython uses reference counting and generational garbage collection. There are 3 age stages where objects live in memory. They all start in the "nursery", stage0, then if they survive a garbage collection, they are moved to stage1, the oldest objects that continue to survive in stage1 are promoted to stage2. The gc module has thresholds 700, 10, 10 for each stage. In order to decide when to run, the collector keeps track of the number object allocations and deallocations since the last collection. When the number of allocations minus the number of deallocations exceeds threshold0, collection starts. If generation 0 has been examined more than threshold1 times since generation 1 has been examined, then generation 1 is examined as well. Similarly, threshold2 controls the number of collections of generation 1 before collecting generation 2.


front: What is reference counting?
back:
RC is a method of garbage collection. The runtime keeps track of references to an object by manipulating the reference count on each assignment and delete (del), and when the reference count reaches 0 it means the object is practically unreachable. When the next collection runs, the object's memory will be reserved to allocate for new objects.


front: What is a finalizer in Python?
back:
A finalizer is a destructor, named __del__. __del__() is run when the runtime is about to destroy the object.


front: What are 2 disadvantages of reference counting?
back:
- it cannot collect circular references
- manipulating reference counts at each assignment is very slow.


front: How is garbage collection done in PyPy?
back:
The GC implementation can be chosen at runtime. It's pluggable.
PyPy uses mark and sweep, and generational gc optimization. Marked objects are promoted from the nursery to an older generation.
PyPy uses incremental garbage collection, where major collection is split into multiple passes, each lasting only a few milliseconds.


front: How does mark and sweep work?
back:
(In Java) Perform a DFS on the graph of references to objects. This graph can have multiple roots. Each root is a reference that the program can access directly, such as a variable. Traverse the graph, setting a mark bit in each object. The sweep phase causes unmarked memory to be linked together in a list, so that memory can be reallocated. Sometimes this also triggers compaction, which moves used objects adjacent to each other in memory. The side effect of this is that free memory is also adjacent to free memory so large blocks can be allocated.


front: What is copying garbage collection (stop and copy)?
back:
Heap memory is split into 2 partitions: an old space and a new space. Find live objects by DFS of their reference graph, and move live objects into the new space. The new space is now called the old space. Unreachable objects are simply left in the old space to be overwritten the next time collection occurs. The movement of objects implicitly compacts the objects. Disadvantage: you can only use half of the heap space.


front: How could you implement an LRU cache?
back:
A fast lookup table, like a hash table or binary tree, and a linked list of items by use. When you access or add an item, you delete it from the linked list and add it to the head of the list.  Then to prune, traverse the linked list and remove trailing elements, and delete them from the storage (tree or hash table).
You can also use a splay tree, since it moves accesses to the root. To prune items, somehow find and remove the leaves, since the number of leaves will be about n/2.


front: What is a direct mapped cache?
back:
It's a type of cache used in the CPU, where the lower order bits of a given memory address are used modulo the number of cache lines to place or lookup in the cache. Collisions are treated as overwrites.


front: What is a fully-associative cache?
back:
It's a type of cache used in the CPU, where lookups are done on all cache lines in parallel to determine a hit or miss.

This requires a very large number of comparators that increase the complexity and cost of implementing large caches. Therefore, this type of cache is usually only used for small caches, typically less than 4K.


front: What is an N-way set associative cache?
back:
A Set-Associative cache scheme is a combination of Fully-Associative and Direct Mapped caching schemes. A set-associate scheme works by dividing the cache SRAM into equal sections (2 or 4 sections typically) called cache ways. The cache page size is equal to the size of the cache way. Each cache way is treated like a small direct mapped cache.


front: Describe the memory hierarchy of a Core i7 processor.
back:
Inside of each core is a 32 kB L1 instruction cache, a 32 kB L1 data cache (it's 8-way set associative), and a dedicated 256 kB L2 cache (also 8-way set associative). 
Outside of the cores is the L3, which is much larger at 8 MB and smarter (16-way associative) than the L2s.


front: Is DELETE idempotent?
back:
According to the REST spec, yes, but it's up to the developer to conform to that. It can be achieved by using a deleted flag for a resource instead of completely removing the resource.


front: What is idempotent?
back:
The property that a method has side-effects of making more than one identical requests is the same as for a single request.


front: What is HMAC?
back:
HMAC is a keyed-hash message authentication code used to provide a checksum for a message, sent along with the message to provide confidence that the message has not been tampered.


front: What is a MAC?
back:
MAC is a message authentication code used to provide a checksum for a message, sent along with the message to provide confidence that the message has not been tampered.


front: How does RSA work?
back:
It's a public/private key cryptography method. The public key can be used to encrypt a message into ciphertext that only the owner of the key can decrypt. The owner of the key uses their secret key to encrypt messages, and their secret key to decrypt messages encrypted with their public key.


front: What is symmetric key encryption?
back:
There is a known encryption function, and one key is used to encrypt and decrypt. The key has to be shared between 2 parties.


front: How does Diffie-Hellman key exchange work?
back:
2 parties agree on a G and a modulus p, and each party comes up with a number. One party does G^a and the other G^b. They pass this information. One party A computes the key from B as B^a mod p. B computes A^b mod p to get the key.


front: How is RSA (using product of large primes) better than using NP-Complete algorithms for encryption?
back:
NP-Complete algorithms are hard in the worst case, but can be sometimes solved in linear time in the average case. Compositing the product of large primes is hard in the average case.


front: What is Vigenere cipher?
back:
Improvement on Caesar cipher. Letters are shifted based on a shifted dictionary. "Polyalphabetic cipher"


front: What is a one-time pad encryption?
back:
The "perfect" simple encryption scheme. Pad/key is the same size as the message being encrypted. The key is randomly generated and xored against the plain text. Or key used to determine the amount each letter should be shifted.


front: What is block size in cryptography?
back:
Symmetric key ciphers are generally divided into stream ciphers and block ciphers. Block ciphers operate on a fixed length string of bits. The length of this bit string is the block size. Both the input (plaintext) and output (ciphertext) are the same length; the output cannot be shorter than the input – this is logically required by the Pigeonhole principle and the fact that the cipher must be invertible – and it is simply undesirable for the output to be longer than the input.


front: What is the limiting factor of compression?
back:
For lossless compression, it's entropy. For lossy compression, it's our acceptance with the amount of loss.


front: What is LZ* compression based on?
back:
Cataloging the positions and lengths of redundant patterns and combining the values with a dictionary.


front: What is LZMA?
back:
It's a variant of LZ77 that uses Markov chains. It's used in the 7z compression algorithms used in 7-zip.


front: What is DEFLATE?
back:
It's an lossless compression algorithm based on LZ77 used in Gzip, WinZip, and mod_deflate, which is bundled with Apache web server for automated gzip compression of HTTP served content. It uses LZ77 and Huffman coding.


front: How does LZ77-based compression work?
back:
LZ77 is a dictionary encoding algorithm, which is a statistical encoding algorithm. Compression in the LZ77 algorithm is based on the notion that strings of characters (words, phrases, etc.) occur repeatedly in the message being compressed.

The input is partitioned into 2 segments: a search buffer and a look-ahead buffer. The search buffer maxes out at 32KB. Starting with one character in the LA buffer, it looks back in the search buffer to find a copy of the symbol. If one is found, it looks at the second symbol of the LA buffer to see if it also matches the predecessor. Using this method, it can detect long phrases of symbols and encode them as one unit.

This process implicitly creates a rolling statistical probability for each symbol/phrase.


front: What is Huffman encoding?
back:
Huffman encoding algorithm analyzes the occurrence of individual symbols and creates a binary tree where the common symbols are closest to the root, using fewer bits to encode, and less common/rare symbols have longer paths on the tree, with longer encodings to accommodate. By traversing the tree, from root to leaf, and keeping track of 1 or 0 at each node, we can determine the encoding of the symbol.


front: What is the Burrows-Wheeler transform?
back:
It's a compression method involving the sorting of all possible rotations of the input text into lexicographic order. Take as output the last column and the index of the row that the original text appears in. 

To decode, take the single column and repeatedly add the final columns characters to each of the rows, sorting each time. Once you've reached the length of the column's height, use the index to find the output string.


front: How can you make JSON better compressable with Gzip?
back:
Transpose from multiple mini-dicts into one dict with arrays as the values. This allows the items in an array to fit within the 32KB search buffer common to LZ-based compression.


front: What are Dense Codes?
back:
A way to put symbols or words into a dictionary or array, and use the indices as the values in the text to save space so that words are not repeated.


front: What is OS hardware virtualization?
back:
The abstraction of heterogeneous hardware provided by the operating system, to hide the details of interfacing with various hardware so that they share a common interface for that type.


front: What is a process?
back:
An instance of an executing program consisting of an address space and one or more threads of control. It has restricted rights. It owns a region of memory. It owns file descriptors, file system context. It encapsulates one or more threads sharing the process' resources. It is isolated from other processes.


front: What causes operating systems to crash most of the time?
back:
Device drivers. They are in the kernel and very low level. They have access to all the hardware, including memory. They are written by authors outside of the operating system.


front: What is a process VM?
back:
A virtual environment that supports the execution of a single program. This is provided by an operating system.


front: What is a system VM?
back:
A virtual environment that supports the execution of an entire operating system and its applications.


front: What is the execution sequence for a program?
back:
- Fetch instruction
- Decode
- Execute (perhaps using ALU and writing result to a register)
- Write results to registers or memory
- Increment program counter
- Repeat


front: What triggers a context switch?
back:
Timer interrupt, hardware interrupt, I/O where we're waiting for a read or write to complete (OS doesn't want to waste time waiting), voluntary yield.


front: How does the OS run a program?
back:
It does an exec from kernel mode (system mode 1). We go to system mode 0, user mode. When the program exits, we switch back to kernel mode.


front: How does a user program interact with the kernel?
back:
It makes a system call, and the kernel takes over, completes the action, and the kernel returns to the process (back to user mode).


front: What happens when the hardware needs something to happen?
back:
It does an interrupt, and the kernel takes control, switches the process, and once hardware task is complete, does a return from interrupt back to user mode.


front: What is base and bound?
back:
It's a memory addressing restriction where a processes are only allowed access to the memory between a base address and the bound + base addresses. Each process has its own base and bound. A drawback is you don't get address 0. Address translation fixes this.


front: How does the OS know how to handle an interrupt?
back:
It keeps an interrupt vector in the memory of the OS. Each interrupt type is mapped to an address to execute. They are just pointers to code in the OS.


front: What is the difference between PC and uPC?
back:
PC is the program counter for the current process. uPC is the PC of the user process we will return to once an interrupt or other OS process switches context.


front: How does x86 do segments and stacks?
back:
Each process' individual segments (heap, stack, code, static data) get their own base and bound. They don't even need to be adjacent in memory and their location is hidden by memory addressing.


front: When a process forks, what happens?
back:
The process is paused, and a complete copy is made: code, stack, heap, data, program counter and registers.
The child and parent resume with returning from fork syscall.


front: What is a shell?
back:
A job control program. It allows a programmer to create and manage a set of programs to do some task.


front: How does the kernel handle reads and writes?
back:
It buffers reads so they can be handled as a stream in your program. Writes are buffered and are not written until the kernel flushes the buffer.


front: What's the difference between the fread, read, fwrite, write I/O calls?
back:
The ones with f are high-level I/O and streamed and buffered by the kernel. The non-f are low-level I/O.


front: What is a socket?
back:
It's an abstraction of a network I/O queue.  It's a method of communication where a producer writes to one side, and a consumer reads from the other side. It's similar to writing and reading a file, but no file is involved.


front: What is the GIL?
back:
It's the Global Interpreter Lock. It's is a part of CPython. It ensures only one thread runs in the interpreter at once. Having the GIL simplifies many low-level details (memory management, callouts to C extensions, etc.)


front: What happens every 100 "ticks" in the CPython interpreter?
back:
A thread check occurs during which the thread releases the GIL then attempts to reacquire it. Other Python threads will contend for the the GIL. This is no longer the case in 3.4.


front: What can happen with thread stacks if one goes into a deep recursion?
back:
One thread's stack can grow into another thread's stack and write over it. A guard page can help to protect from that.


front: What can cause a thread to give control back to the dispatcher?
back:
Thread returns control voluntarily (yield, requesting I/O (which blocks), wait for signal from another thread) or gets preempted by an interrupt.


front: What's the difference between parallelism and concurrency?
back:
Concurrency means running multiple blocks of instructions independently. Parallelism means running instructions at the same time, as on multiple cores at once.


front: What is a race condition?
back:
When the outcome of a deterministic procedure becomes non-deterministic based on differences in subprocess timing.


front: How do you use a mutex in Python?
back:
import threading

lock = threading.Lock()

[first process]
global lock
lock.release()
...
[other process]
global lock
lock.acquire() // attempts to get access, waits if it can't


front: What is livelock?
back:
It occurs when multiple processes are attempting to deal with the current state, but neither makes progress. This can happen when a system is attempting to resolve a deadlock situation but another or the same process continue to trigger it.
Starvation is another example.


front: In Python, what can you use to fork a process?
back:
The multiprocessing module. It supports process Pool and Process for making a pool of worker processes or forking temporary subprocesses.


front: What happens during an interrupt?
back:
The currently running process' state is saved. We switch to kernel mode, the interrupt handler runs, and once its complete, the system goes back to user mode and the process continues.


front: What really happens when you fork a process?
back:
A fork doesn't copy everything, it just duplicates the page table pointers, which are all set at read-only. Called copy-on-write. Once you write to memory, then it copies the state.


front: What does a PCB contain?
back:
Everything about a process:
- status
- register state (when not in ready state)
- PID, User, Executable, Priority
- Execution time
- Memory space, translation


front: What are the five states a process can be in?
back:
- new (when being created)
- ready
- running
- waiting (for I/O or event coordination)
- terminated (waits for parent process to receive its exit code)


front: What is the difference between filter() and map()?
back:
Filter uses a function that returns true or false (predicate).
Map uses a function that returns a value.


front: What is a critical section?
back:
A block of code that multiple threads within a process could try to access at the same time. To ensure correct processing, the critical section should be locked before entering, then unlocked when leaving. This creates a mutual exclusion on shared data.


front: What is the priority inversion problem?
back:
A thread that is busy-waiting for a lock to be released ends up stealing CPU and getting a higher priority than the thread with the lock. SO since the waiting thread gets higher priority, the thread holding the lock can't complete and release the lock.


front: What is busy-waiting?
back:
One or more threads is using a lot of CPU by continuously checking a value, or test&set() checking and writing a value in wiating for a lock to release, thus stealing CPU from the thread holding the lock.


front: What is a semaphore?
back:
A semaphore limits access to a maximum number of threads (maximum concurrency) to have access to a source at the same time. It is commonly used to limit database connections.

A semaphore (defined by Dijkstra) is kind of signaling solution for handling concurrency data integrity problems that arise in multi-threaded applications. It has a non-negative integer that supports 2 operations:
- P() [proberen, to test/probe] - atomic operation that waits for semaphore to become > 1, then decrements it by 1 (wait)
- V() [verhogen, to increment] - an atomic operation that increments the semaphore by 1, waking up any P (signal)

The initial semaphore value will determine how many threads can run in the critical section at once.


front: What is a drawback of context switching?
back:
CPU cache misses as thread comes back from switching and finds the CPU cache doesn't have the values it had before.


front: What is the round robin scheduling scheme?
back:
Each process gets a time quantum q milliseconds to run. 10-100ms, the q is tunable. Each process runs for that time slice (or until completion if close to done) and then goes back on the ready queue.


front: What are some methods of avoiding deadlock?
back:
- don't allow waiting for a resource (means a lot of retries)
- make all threads request everything they'll need at the beginning
- force all threads to request resources in a particular order preventing any cyclic uses of resources (so no cycle exists)
- temporarily expand resources when a deadlock is detected


front: What is the banker's algorithm for preventing deadlock?
back:
- allocate resources dynamically
- evaluate each request and grant if some ordering of threads is still deadlock-free afterward
- do so by pretending the request was granted, then running a simulation to see if a deadlock would occur


front: How does the banker's algorithm solve the dining lawyers problem?
back:
When you try to grab a chopstick, it's either:
- not the last chopstick
- is last chopstick but someone else will have two afterwards


front: What is a PDU?
back:
Protocol data unit
- generic term used to describe the information at a given layer in the TCP/IP stack


front: What is involved in the 3 way handshake (TCP)?
back:
SYN=1 - synchronize, gives a Seq number and expects that number + 1 in response
ACK=1 - sent by acknowledging server with incremented number, who also sends a SYN=1 and a Seq
SYN=0 ACK=1 and the Seq (incremented number) back to the server

Now you're talking!


front: What are the 4 layers of TCP/IP?
back:
- application (application, presentation, session in OSI)
- transport
- internet (network in OSI)
- network access (data link & physical in OSI)


front: How is an SSL certificate generated by the certificate authority (CA)?
back:
The common name and public key for a given domain name, signed by the certificate authority's secret key.
The browser can verify the cert with CA's public key.


front: What is the secure flag on a cookie?
back:
When set on a cookie, it will only be sent on https requests.
When not set, cookie will be sent on both http and https requests.


front: Give an example of the thread-per-connection pattern.
back:
A web server might spawn a thread per connection, then reuse that thread once the connection ends, or terminate the thread.


front: Give an example of the thread pool model.
back:
A pool of threads can be maintained in order to quickly provide one as a resource for a database connection.


front: What is special about TCP?
back:
It manages the sending and receiving of packet data.
It acknowledges receipt of packets.
If packets are missing, the source will resend the missing packets.


front: What does SNMP do?
back:
Gathers info from machines on the network when each box has an SNMP agent installed. Can send a large amount of info about machines, software installed, and machine configuration.


front: What is special about UDP?
back:
It's connectionless, packets are only sent once and not re-sent if dropped. Packets may not arrive in the right order, and there is no ordering mechanism to fix on the receiving end. No congestion control.


front: What's special about TCP?
back:
It does a 3-way handshake before data is sent.
Delivery is acknowledged by receiver.
Packets missing within a certain time window are re-requested.
Packets are put in order on receipt.
Congestion control: can delay delivery until network is uncongested.
IPv4 and IPv6: error detection, checksum mandatory.


front: What happens in the Session layer of the OSI model?
back:
This layer handles configuration of the data:
- encryption
- compression
- translation to and from different character encodings


front: What happens at the Session layer of the OSI model?
back:
This layer controls the communication's access via:
- login rights
- permissions
- rights
- roles


front: What happens at the Transport layer of the OSI model?
back:
This layer guarantees end-to-end delivery of data:
- packet ordering
- error detection
- acknowledgements


front: What happens at the Network layer of the OSI model?
back:
This layer's function is to find the shortest path through the network to the destination network.
Deals with congestion, bandwidth, etc.


front: What happens at the Data Link layer of the OSI model?
back:
It decides whose turn it is to talk on the network using bus arbitration methods.
It finds the physical device on the network.


front: What happens at the Physical layer of the OSI model?
back:
It's the physical network that deals with the physical transmission of electricity through wire:
- cables
- voltages
- frequencies
- connectors
- bits
- transfer rates
- and much more


front: How does HTTP/2 save bandwidth?
back:
Headers are compressed and do not need to send the same headers in a session if they haven't changed.
Servers can send assets referenced in a document without waiting for discrete requests for them.


front: How are headers and body treated differently in HTTP/2?
back:
They are split into a header frame and a data frame. Multiple requests can be interleaved in a connection, so a request doesn't block.


front: How many host IDs are supported on a Class A network?
back:
2^24 = 16 million
There are 8 bits for the network ID, and the remaining 24 bits are for host IDs.
So there are 16 million per network.


front: What is a limited broadcast address?
back:
The limited broadcast address is the address formed by setting all 32 bits of the IP address to 1 (255.255.255.255). The limited broadcast address is used when an IP node must perform a one-to-everyone delivery on the local network but the network ID is unknown.


front: What does a /24 CIDR mean?
back:
The first 24 bits of the IP address are masked. Only hosts with addresses in the unmasked portion are reachable.


front: What is a block cipher?
back:
A block cipher is a method of encrypting text (to produce ciphertext) in which a cryptographic key and algorithm are applied to a block of data (for example, 64 contiguous bits) at once as a group rather than to one bit at a time.


front: What is QUIC?
back:
QUIC is a new transport which reduces latency compared to that of TCP. On the surface, QUIC is very similar to TCP+TLS+HTTP/2 implemented on UDP.


front: What is Capsicum?
back:
A sandboxing framework that adds capability-based security to unix-like kernels and denies access to global namespaces.


front: What is Google Native Client?
back:
Also called NaCl, Native Client is a sandbox for running compiled C and C++ code in the browser efficiently and securely, independent of the user’s operating system.


front: What is the same-origin policy?
back:
Goal: Two websites should not be able to tamper with each other.
Strategy: each resource is assigned an origin. JS can only access resources from its own origin.
Origin: scheme + hostname + port


front: How can 2 origins (let's say 2 frames) communicate?
back:
window.postMessage (HTML5) allows for sending data messages between two windows/frames across domains.


front: What is JIT compilation?
back:
JIT compilation, also known as dynamic translation, is compilation done at run-time rather than ahead of time (AOT).


front: What is PyPy?
back:
PyPy is a replacement for CPython. It is built using the RPython language that was co-developed with it. RPython is a subset of Python and can be translated to C. The main reason to use it instead of CPython is speed: it runs generally faster due to JIT compilation.
PyPy implements Python 2.7.10. It supports all of the core language, passing the Python test suite (with minor modifications that were already accepted in the main python in newer versions). It supports most of the commonly used Python standard library modules.


front: What is an SMT solver?
back:
A satisfiability modulo theories solver solves for large interconnected logic formulas to determine if a given formula can be satisfied. These are helpful for determining the outcome or inputs for a program using symbolic execution.


front: How do we find that a condition exists that will cause a program to have a predicted outcome?
back:
Using an SAT solver (using logic tests) and an SMT solver (that plugs in numbers), and tells the SAT solver where it got it wrong. They provide feedback in a cycle to each other until a condition can be found.


front: What is transactional memory?
back:
Transactional memory attempts to simplify concurrent programming by allowing a group of load and store instructions to execute in an atomic way. It is a concurrency control mechanism analogous to database transactions for controlling access to shared memory in concurrent computing.


front: What is software transactional memory?
back:
Software transactional memory provides transactional memory semantics in a software runtime library or the programming language, and requires minimal hardware support (typically an atomic compare and swap operation, or equivalent). As the downside, software implementations usually come with a performance penalty, when compared to hardware solutions.


front: Does PyPY have a GIL?
back:
Yes. The GIL is very difficult to remove. You can use pypy-stm instead, which uses software transactional memory, but will suffer a performance penalty.


front: How can a server deal with a SYN flood attack?
back:
When it detects a large number of SYN packets at once, or the size of its SN (sequence number) data structure reaches a certain threshold of entries, it can switch to a stateless version, where it send SN responses as signed values with a timestamp, and if it receives one back it lets them through without needing a lookup table.


front: What is a stack canary?
back:
It's a buffer overflow defense where a random value is pushed onto the stack after the saved EBP, and before tearing down the stack frame, the value is checked. Any buffer flow targeting the return instruction pointer would have to have overwritten this value.


front: What type of buffer overflow protection does gcc and Visual Studio employ?
back:
They use a stack check guard of bytes before and after the buffer's allocated memory. Once values are written to the buffer, the bytes are checked to ensure they are still the same.


front: What is scalability?
back:
Scalability is the measure to which a system can adapt to a change in demand for resources, without negatively impacting performance.


front: What is Akka?
back:
An open source project that provides a simpler, single programming model - one way of coding for concurrent and distributed applications - the actor programming model.
Akka’s primary goal is to make it simpler to build applications that are deployed in the cloud or run on devices with many cores and that efficiently leverage the full capacity of the computing power available. It’s a toolkit that provides an actor model, runtime, and required supporting tools for building scalable applications.


front: What is an actor?
back:
Briefly, actors are a lot like message queues without the configuration and message broker installation overhead. They’re like programmable message queues shrunk to microsize—you can easily create thousands, even millions of them. They don’t “do”
anything unless they’re sent a message.
Messages are simple data structures that can’t be changed after they’ve been created, or in a single word, they’re immutable.
Actors can receive messages one at a time and execute some behavior whenever a message is received. Unlike queues, they can also send messages (to other actors).
Everything an actor does is executed asynchronously. Simply put, you can send a message to an actor without waiting for a response. Actors aren’t like threads, but messages sent to them are pushed through on a thread at some point in time. How actors are connected to threads is configurable - this is not a hardwired relationship.
For now the most important aspect of actors is that you build applications by sending and receiving messages. A message could be processed locally on some available thread, or remotely on another server. Exactly where the message is processed and where the actor lives are things you can decide later, which is very different compared to hardcoding threads and RPC style networking. Actors make it easy to build your application out of small parts that resemble networked services, only shrunk to microsize in footprint and administrative overhead.


front: What is an IDL-based encoding?
back:
An interface description language or interface definition language (IDL) encoding. 
It requires a schema definitions. They offer peace of mind with respect to data format and validation for consumers while sacrificing flexibility in the schema’s evolution.


front: What is a coroutine?
back:
An object representing activity that eventually completes. Also refers the the function we call that returns a coroutine.
In Python, coroutines are generators.


front: What is AQP?
back:
Approximate query processing. It means pulling a sample of data instead of taking time to process an exact result. It is often used when a data storage involves terabytes or more.


front: How would you visualize billions of items in a graph?
back:
In many cases, you don't need to graph every point, just use visualization-aware sampling. Sometime 1% or less will do.


front: How does Google use multi-homed datacenters?
back:
A multi-homed system runs live in multiple datacenters all the time. Each datacenter processes work all the time, and work is dynamically shared between datacenters to balance load. When one datacenter is slow, some fraction of work automatically moves to faster datacenters. When a datacenter is completely unavailable, all its work is automatically distributed to other datacenters.


front: What is an example of a circuit breaker?
back:
Start sending 503s if your service is choked to avoid numerous simultaneous retries that just make the system worse.


front: What does an eviction policy try to predict?
back:
An eviction policy tries to predict which entries are most likely to be used again in the near future, thereby maximizing the hit ratio


front: What is request coalescing?
back:
When many requests arrive for some content that’s missing in the cache (cache miss), only one instance request will proceed to the backend to fetch the content on behalf of all to avoid a flood.


front: When might you need to use a NoSQL database
back:
You don’t have any relational data.
If you need to store > 5 TB of data or you have an incredibly data intensive workload.
Your application has super low-latency requirements.
You need really high throughput.


front: What is AMP?
back:
AMP is a restricted subset of HTML designed to make the web fast on mobile devices.
Provides a shared library of scripts so they don’t have to downloaded every time.
Sets standards for the architecture of advertising so advertising doesn't infect pages.
Shared mechanism for collecting data for analytics so you don’t have N different packages on a page gathering stats.


front: What is a benefit of making processes asynchronous?
back:
Flexibility in the architecture. Getting users on hooked on synchronous low-latency interactions doesn't allow for architecture flexibility.


front: What is BASE?
back:
basically available
soft state
eventually consistent

A BASE based system is more tolerant to latency because it is an inherently partitioned and loosely coupled architecture and it uses eventual consistency.


front: What is the CAP theorem?
back:
The CAP theorem, also named Brewer's theorem, states that it is impossible for a distributed computer system to simultaneously provide all three of the following guarantees:

- Consistency (all nodes see the same data at the same time)
- Availability (every request receives a response about whether it succeeded or failed)
- Partition tolerance (the system continues to operate despite arbitrary partitioning due to network failures)


front: What is zero copy?
back:
Applications that use zero copy request that the kernel copy the data directly from the disk file to the socket, without going through the application. Zero copy greatly improves application performance and reduces the number of context switches between kernel and user mode.


front: What is privilege separation?
back:
Separating an application into different areas so a vulnerability in one area doesn't affect the entire application.


front: When is security enforced on a file?
back:
Security is checked when the file descriptor is created. Then it's up to the user to be careful and secure the file descriptor.


front: What is ptrace?
back:
ptrace is a system call found in several Unix and Unix-like operating systems. By using ptrace, one process can control another, enabling the controller to inspect and manipulate the internal state of its target.


back: setuid()
front:
During system bootstrapping, what call is performed by the system to give a non-root user the ownership of a process?


front: What does chroot do?
back:
Changes the root directory (/) for a user to be a directory on the filesystem where they can't escape.


front: What is a confused deputy?
back:
A confused deputy is a computer program that is innocently fooled by some other party into misusing its authority. It is a specific type of privilege escalation. In information security, the confused deputy problem is often cited as an example of why capability-based security is important, as capability systems protect against this whereas access control list-based systems do not.
The classic example is a Fortran compiler that creates a billing record for each use. A user was able to tell the compiler to output a binary with the same name as the billing file, overwriting it.


front: What is ambient authority, or ambient privilege?
back:
The decision about whether a process or agent can perform an action is based on information not explicitly stated, but inherited instead.


front: What is a capability?
back:
The privilege to act upon something given your ownership of it, and the inability to act on something using an intermediate process' privileges. An example would be a function where you pass a file descriptor as an argument and the function uses your capability, not its own.


front: What is a requirement of enabling sandboxing?
back:
The kernel must be able to support it by disallowing system calls that reference global namespaces:
- file paths starting at root - must be relative
- network
- PIDs - use process descriptors instead


front: How is RSA decryption optimized for speed?
back:
- c^d mod p and c^d mod q are processed in parallel and merged at the end using the Chinese remainder theorem
- put into Montgomery format
- sliding windows to exponentiate on bits of exponent
- perhaps a single extra reduction
- convert back from Montgomery format
- merge using CRT


front: What is RISC?
back:
Reduced Instruction Set Architecture. 
- more registers
- fewer, fixed-sized instructions
- used in PowerPC, ARM, SPARC, MIPS


front: What is Little Endianness?
back:
The least significant bytes of a word or larger are stored in the lowest address. All bytes are the same. There is no Endianness within a byte.


front: What is Chef?
back:
A configuration tool. You write or reuse recipes that declare the state you wish your server to be in. It calculates the delta and builds out for you.


front: What is an example of a non-cryptographic hash function?
back:
MurmurHash is an efficient, non-cryptographic hash function suitable for general hash-based lookup. The name comes from two basic operations, multiply (MU) and rotate (R), used in its inner loop. It has an avalanche effect. The current version is MurmurHash3 which yields a 32-bit or 128-bit hash value.


front: How could you process calculations on elements of an array in parallel?
back:
Using recursion, divide and conquer, breaking down the array into smaller segments, then merging the values as the recursion unwinds. Non-mutation of the array means locking is not required.


back: The memory bandwidth of your RAM. It can become a bottleneck.
front:
What factor should you keep in mind when doing parallel computation on different parts of a large data structure?


front: Why should you avoid starting and joining a task or thread on the same line or proximity?
back:
Execution on the current thread will block until it completes, thereby obviating the concurrent call.


front: What factors affect performance?
back:
- processor speed 
- number of processors
- memory access latency and throughput
- cache behavior
- runtime behavior (garbage collection, JIT compilation, thread scheduling)


front: What is Amdahl's Law?
back:
It gives the theoretical speedup in latency of the execution of a task at fixed workload that can be expected of a system whose resources are improved.


front: What aspects are important in measuring and benchmarking performance, parallel or otherwise?
back:
- multiple repetitions
- statistical treatment - mean and variance
- eliminate statistical outliers
- ensuring steady-state (warm-up)
- preventing anomalies (garbage collection, JIT compilation, compiler optimizations in benchmark code that misrepresents real-world)


front: What is the commutative property?
back:
The condition that a group of quantities connected by operators gives the same result whatever the order of the quantities involved. So order of operands doesn't matter, but grouping may matter.


front: What are some examples of operations that are both associative and commutative?
back:
- Addition and multiplication of integers
- Union, intersection, and  symmetric difference of sets
- Addition of vectors
- Addition of matrices of fixed dimension
- Addition and multiplication of polynomials


front: What is the rule of thumb before optimizing or parallelizing?
back:
Ensure the code works correctly first. Then, if you want to get fancy, you can begin to optimize your code for greater speed.


front: Does Python have an opcode cache?
back:
In a way. It outputs a .pyc file, containing the bytecode. When a module is imported for the first time, or when the source is more recent than the current compiled file, a .pyc file containing the compiled code will usually be created in the same directory as the .py file. When you run the program next time, Python uses this file to skip the compilation step.


back: ask:
front:
How would you design the feature in LinkedIn where it computes how many hops there are between you and another person?


front: What are some key things to remember when scaling a large system?
back:
1) Asynchronous is good  (use queues, topics/pub-sub)
2) Parallel is good  (multi-threading, load balancing etc.)
3) Avoid points of contention e.g. synchronization
4) Avoid writing to disk until you must - cache like crazy
5) Scale out not up
6) At web scale the speed of light is a problem
7) At web scale everything fails - networks, load balancers etc.


front: What is Thrift?
back:
Apache Thrift is a framework for scalable cross-language services development. It combines a software stack with a code generation engine to build services that work efficiently and seamlessly between different languages. It handles serialization and has its own communication protocol.
IDL-based.


front: How does/did Facebook use memcache and mySQL in 2009?
back:
No joins in production. They have many logical databases for all of their types: people, events, place info, etc. They treat the web tier as a CPU, memcache as system memory, and the database as disk. Everything has an ID and you use the ID to query memcache using a multiget. Any misses are fetched from the database and cached in memcache.


front: What is an out-of-band cache?
back:
A cache layer that does not synch with persistent storage. When changes are made to the database, there are no notifications to synchronize with the cache. The cache entry would need to be updated or evicted by other means.


front: What is a monad?
back:
In functional programming, monads are a way to build computer programs by joining simple components in predictable and robust ways. A monad is a structure that represents computations defined as sequences of steps: a type with a monad structure defines what it means to chain operations together, or nest functions of that type. This allows the programmer to build pipelines that process data in a series of steps (i.e. a series of actions applied to the data), in which each action is decorated with additional processing rules provided by the monad. A monad is defined by a return operator that creates values, and a bind operator used to link the actions in the pipeline.


front: Theta represents what?
back:
A tight asymptotic bound on a function, in other words if both f and g have approximately the same rate of growth.


front: What is data normalization?
back:
Normalization is a systematic approach of decomposing tables to eliminate data redundancy and undesirable characteristics like insertion, update and deletion anomalies.

Normalization is used for mainly two purpose,
- eliminating redundant (useless) data
- ensuring data dependencies make sense


front: What are the requirements for first normal form?
back:
- each cell has a single value
- all items in a column must be of the same type
- rows are uniquely identified by a unique ID or a composite key


front: What are the requirements for fourth normal form?
back:
No multi-valued dependencies, meaning records should not be duplicated in a table just because more than one item is associated. This creates records that are duplicates except for one field.


front: What are some solid principles to keep in mind for scaling?
back:
- Keep it very simple
- Don’t re-invent the wheel
- Go with boring, proven and well-supported technologies when you can
- Build for what you will need over the next 12-18 months
- Make different things look the same
- Cache to protect the database
- Good enough is good enough


front: What is WSGI?
back:
WSGI is the Web Server Gateway Interface. It is a specification that describes how a web server communicates with web applications, and how web applications can be chained together to process one request. It was outlined in PEP 3333.

A WSGI server (meaning WSGI compliant) only receives the request from the client, pass it to the application and then send the response returned by the application to the client.


front: What is Fabric?
back:
Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.


front: What is Apache Solr?
back:
Solr is an open source enterprise search platform built on Apache Lucene.
Solr is highly reliable, scalable and fault tolerant, providing distributed indexing, replication and load-balanced querying, automated failover and recovery, centralized configuration and more.


back: Fan-in, where you can overload routers due to increased internal network traffic.
front:
What tends to become an issue as you add services in a datacenter and have to ping more caches and services to fulfill a request?


front: What is a benefit of sharding a database as it grows, and what determines the size?
back:
Keeping the index in cache ensures a user lookup doesn't have to hit the disk, lookups can be served from RAM. How much RAM you have will determine the index size which will hint at the underlying data size.


front: What is a message broker?
back:
Message broker is an intermediary program module that translates a message from the formal messaging protocol of the sender to the formal messaging protocol of the receiver. Message brokers are elements in telecommunication networks where software applications communicate by exchanging formally-defined messages. Message brokers are a building block of Message oriented middleware.


front: What are some examples of message brokers?
back:
Apache ActiveMQ
Apache Kafka
Apache Qpid
Celery
Gearman
HornetQ (Red Hat)
IBM Integration Bus
JBoss Messaging (JBoss)
JORAM
Microsoft BizTalk Server (Microsoft)
Microsoft Azure Service Bus (Microsoft)
NATS (MIT Open Source License, written in Go)
Open Message Queue
Oracle Message Broker (Oracle Corporation)
QDB (Apache License 2.0, supports message replay by timestamp)
RabbitMQ (Mozilla Public License, written in Erlang)
SAP PI (SAP AG)
Spread Toolkit
Tarantool, a NoSQL database, with a set of stored procedures for message queues
WSO2 Message Broker
Enduro/X Transactional Message Queue (TMQ)


front: What is Ehcache?
back:
Ehcache is an open source, standards-based cache that boosts performance, offloads your database, and simplifies scalability. It's the most widely-used Java-based cache.


front: What is Kafka?
back:
Apache Kafka is pub-sub messaging rethought as a distributed commit log.
Kafka is a distributed, partitioned, replicated commit log service. It provides the functionality of a messaging system, but with a unique design.
A single Kafka broker can handle hundreds of megabytes of reads and writes per second from thousands of clients.


front: What is GAE?
back:
Google App Engine is a platform for building scalable web applications and mobile backends. App Engine provides you with built-in services and APIs such as NoSQL datastores, memcache, and a user authentication API, common to most applications.


front: What is GDS?
back:
Google Cloud Datastore is a NoSQL document database built for automatic scaling, high performance, and ease of application development. Cloud Datastore features include:

Atomic transactions.
Massive scalability with high performance.
Flexible storage and querying of data.
Balance of strong and eventual consistency. 
Encryption at rest.
Fully managed with no planned downtime.


front: What is the problem that serialization introduces?
back:
The overhead of serializing and deserializing. It's all expensive, and for Python, it can be terribly slow.


front: What does the Python bisect module do?
back:
The bisect module, part of the standard library, provides support for maintaining a list in sorted order without having to sort the list after each insertion. For long lists of items with expensive comparison operations, this can be an improvement over the more common approach.


front: What is PycURL?
back:
PycURL is a Python interface to libcurl. PycURL can be used to fetch objects identified by a URL from a Python program, similar to the urllib Python module. PycURL is mature, very fast, and supports a lot of features.
PycURL is targeted at an advanced developer - if you need dozens of concurrent, fast and reliable connections or any of the sophisticated features listed above then PycURL is for you.
The main drawback of PycURL is that it is a relatively thin layer over libcurl without any of those nice Pythonic class hierarchies. This means it has a somewhat steep learning curve unless you are already familiar with libcurl's C API.


front: How does PycURL compare to requests?
back:
PycURL can handle a large number of multiple concurrent requests. When reusing connections, it can perform more than 2,000 requests per second.

pycurl takes about 73 CPU-microseconds to issue a request when reusing a connection
requests takes about 526 CPU-microseconds to issue a request when reusing a connection
pycurl takes about 165 CPU-microseconds to open a new connection and issue a request (no connection reuse), or ~92 microseconds to open
requests takes about 1078 CPU-microseconds to open a new connection and issue a request (no connection reuse), or ~552 microseconds to open


front: What is ZooKeeper?
back:
Apache ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kinds of services are used in some form or another by distributed applications. Each time they are implemented there is a lot of work that goes into fixing the bugs and race conditions that are inevitable. Because of the difficulty of implementing these kinds of services, applications initially usually skimp on them, which make them brittle in the presence of change and difficult to manage. Even when done correctly, different implementations of these services lead to management complexity when the applications are deployed.
Has Java and C interfaces.


front: What is orthogonality?
back:
In mathematical terms, it means being perpendicular.

Orthogonality in programming language design is the ability to use various language features in arbitrary combinations with consistent results.

Orthogonality is a system design property which guarantees that modifying the technical effect produced by a component of a system neither creates nor propagates side effects to other components of the system. Typically this is achieved through the separation of concerns and encapsulation, and it is essential for feasible and compact designs of complex systems. The emergent behavior of a system consisting of components should be controlled strictly by formal definitions of its logic and not by side effects resulting from poor integration, i.e., non-orthogonal design of modules and interfaces. Orthogonality reduces testing and development time because it is easier to verify designs that neither cause side effects nor depend on them.


front:
When dealing with scaling, how can you deal with rapidly increasing counters, like YouTube video views?

back:
You can add randomness to a monotonic counter, because as long as people can see it is increasing somewhat monotonically, it doesn't need to be 100% accurate. And avoids need to lock it in a transaction.


front: What is exponential backoff and when is it used?
back:
Binary exponential backoff or truncated binary exponential backoff refers to an algorithm used to space out repeated retransmissions of network or other service requests, often as part of congestion avoidance.


front: What does adding jitter in system design help you avoid?
back:
If your system doesn’t jitter then you get thundering herds. Distributed applications are really weather systems. Debugging them is as deterministic as predicting the weather. Jitter introduces more randomness because surprisingly, things tend to stack up.


front: What is an example of adding jitter to a caching system?
back:
Cache expirations. For a popular video they cache things as best they can. The most popular video they might cache for 24 hours. If everything expires at one time then every machine will calculate the expiration at the same time. This creates a thundering herd.
By jittering you are saying randomly expire between 18-30 hours. That prevents things from stacking up. They use this all over the place. Systems have a tendency to self synchronize as operations line up and try to destroy themselves. Fascinating to watch. You get slow disk system on one machine and everybody is waiting on a request so all of a sudden all these other requests on all these other machines are completely synchronized. This happens when you have many machines and you have many events. Each one actually removes entropy from the system so you have to add some back in.


front:
What Python package provides tools for adapting or extending functions and other callable objects, without completely rewriting them?

back:
functools
offers:
Decorators
Comparison
Rich Comparison
Collation Order
Caching
Reducing a Data Set
Generic Functions


front: What is a proxy server?
back:
A proxy server is an intermediate piece of hardware/software that receives requests from clients and relays them to the backend origin servers. Typically, proxies are used to filter requests, log requests, or sometimes transform requests (by adding/removing headers, encrypting/decrypting, or compression).


front: What is collapsed forwarding?
back:
A proxy server can collapse the same (or similar) requests together into one request, and then return the single result to the requesting clients.

Another great way to use the proxy is to not just collapse requests for the same data, but also to collapse requests for data that is spatially close together in the origin store (consecutively on disk).


front: What should be handling requests first, a proxy server or a cache?
back:
Generally it is best to put the cache in front of the proxy. This is because the cache is serving data from memory, it is very fast, and it doesn't mind multiple requests for the same result. But if the cache was located on the other side of the proxy server, then there would be additional latency with every request before the cache, and this could hinder performance.


front: What is the role of a load balancer?
back:
The role is to distribute load across a set of nodes responsible for servicing requests. This allows multiple nodes to transparently service the same function in a system. Their main purpose is to handle a lot of simultaneous connections and route those connections to one of the request nodes, allowing the system to scale to service more requests by just adding nodes.


front: What is a drawback of advanced features in a load balancer?
back:
Advanced features can make problem diagnosis cumbersome. For example, when it comes to high load situations, load balancers will remove nodes that may be slow or timing out (because of too many requests), but that only exacerbates the situation for the other nodes.


front: What is a benefit of adding asynchrony to a system?
back:
It keeps clients from holding connections to servers when waiting for a response for a task that could simply be deferred. This reduces load on servers that are performing writes, for example, and lets them perform a task when ready, not on-demand.


front: What is the difference between a synchronous system and an asynchronous system?
back:
In a synchronous system, there is no differentiation between request and reply, and they therefore cannot be managed separately. In an asynchronous system the client requests a task, the service responds with a message acknowledging the task was received, and then the client can periodically check the status of the task, only requesting the result once it has completed.

While the client is waiting for an asynchronous request to be completed it is free to perform other work, even making asynchronous requests of other services.


front: Does asynchronous code tend to be CPU-bound or I/O bound?
back:
Asynchronous code tends to be CPU bound, because anything that would block is simply deferred to later, until the blocking operation completes. This means that threads in asynchronous / non-blocking applications are much more likely to use their full time quantum before the kernel scheduler preempts them.


front: What is the optimal number of threads?
back:
And if there's the same number of runnable threads as there are hardware threads, the kernel is very likely to reschedule threads on the same core, which significantly helps performance.


front: What is the typical time slice for a process on a Linux box?
back:
Linux kernels are often compiled with HZ=100, which entails that processes are given time slices of 10ms.


front: How does Linux handle CPU affinity?
back:
Default Linux kernels don't do a good job at keeping CPU affinity, even on idle machines. You must explore alternative schedulers or use taskset or cpuset to control affinity yourself.


front: What is futex?
back:
A futex (short for "fast userspace mutex") is a Linux kernel system call that programmers can use to implement basic locking, or as a building block for higher-level locking abstractions such as semaphores and POSIX mutexes or condition variables.


front: How do context switches perform under virtualization?
back:
On average, it's 2.5x to 3x more expensive to do a context switch when using virtualization. My guess is that this is due to the fact that the guest OS can't update the page table itself, so when it attempts to change it, the hypervisor intervenes, which causes an extra 2 context switches (one to get inside the hypervisor, one to get out, back to the guest OS).


front: What is a Frame Check Sequence?
back:
The Frame Check Sequence (FCS) field is used to determine if errors occurred in the transmission and reception of the frame. Error detection is added at the Data Link layer because this is where data is transferred across the media.


front: How does Google handle a search request?
back:
Scatter/gather
In this model, the dispatcher multicast the request to all workers of the pool. Each worker will compute a local result and send it back to the dispatcher, who will consolidate them into a single response and then send back to the client.


front: What is an example of bulk synchronous processing?
back:
Parallel Graph Transformation

The main goal of Graph transformation is to modify the graph. This include modifying the properties of existing nodes and arcs, creating new arcs / nodes and removing existing arcs / nodes. The modification logic is provided by a user-defined function, which will be applied to all active nodes.


front: Execution Orchestrator
back:
This model is based on an intelligent scheduler / orchestrator to schedule ready-to-run tasks (based on a dependency graph) across a clusters of dumb workers.

example: Microsoft Dryad


front: What are balancing methods used by load balancers?
back:
assignment of a request: random, round-robin, random with weighting for machine capacity, etc
matched with current status (available for requests, not responding, elevated error rate, etc).


front: What are the 3 choices in load balancers? The 3 kinds, not balancing mechanisms.
back:
- Smart client, a software based mechanism for determining the availability of a server.
- Hardware load balancer, such as Citrix NetScaler, are remarkably expensive, and they are also "non-trivial" to configure.
- Software load balancer: HAProxy


front: What is write-through cache?
back:
Write-through cache directs write I/O onto cache and through to underlying permanent storage before confirming I/O completion to the host. This ensures data updates are safely stored on, for example, a shared storage array, but has the disadvantage that I/O still experiences latency based on writing to that storage. Write-through cache is good for applications that write and then re-read data frequently as data is stored in cache and results in low read latency.


front: What is write-back cache?
back:
Write-back cache is where write I/O is directed to cache and completion is immediately confirmed to the host. This results in low latency and high throughput for write-intensive applications, but there is data availability exposure risk because the only copy of the written data is in cache. As we will discuss later, suppliers have added resiliency with products that duplicate writes. Users need to consider whether write-back cache solutions offer enough protection as data is exposed until it is staged to external storage. Write-back cache is the best performing solution for mixed workloads as both read and write I/O have similar response time levels.


front: What is read-through cache?
back:
An item is accessed from cache, and if it's a cache miss, the data will be read from persistent storage (perhaps with a callback) and then placed into cache. The response is then sent back to the host.


front: What is HDFS?
back:
Hadoop File System (HDFS) is a Java-based file system that provides scalable and reliable data storage, and it was designed to span large clusters of commodity servers.


front: What is Hortonworks?
back:
Hortonworks is a software company focused on the development and support of Apache Hadoop, a framework that allows for the distributed processing of large data sets across clusters of computers.


front: What is a platform layer?
back:
Web applications to communicate with a platform layer which in turn communicates with your databases. 

First, separating the platform and web application allow you to scale the pieces independently. If you add a new API, you can add platform servers without adding unnecessary capacity for your web application tier. 

Second, adding a platform layer can be a way to reuse your infrastructure for multiple products or interfaces (a web application, an API, an iPhone app, etc) without writing too much redundant boilerplate code for dealing with caches, databases, etc.

Third, a sometimes underappreciated aspect of platform layers is that they make it easier to scale an organization. At their best, a platform exposes a crisp product-agnostic interface which masks implementation details. If done well, this allows multiple independent teams to develop utilizing the platform's capabilities, as well as another team implementing/optimizing the platform itself.


front: What is the Paxos algorithm?
back:
Paxos is a family of protocols for solving consensus in a network of unreliable processors. Consensus is the process of agreeing on one result among a group of participants. This problem becomes difficult when the participants or their communication medium may experience failures.


front: What problem does consistent hashing help solve?
back:
If you're using a caching scheme like server = hash(i) mod m, and one server in the cluster drops out, consistent hashing is needed to avoid swamping your servers when all the caches need to rehash their entities.


front: What is the relationship between consistent hashing and memcache?
back:
Consistent hashing can be used with memcache not even knowing about it.  It is interesting to note that it is only the client that needs to implement the consistent hashing algorithm - the memcached server is unchanged.


front: What are some examples of NoSQL solutions?
back:
Google BigTable
HBase (based on Hadoop)
Hypertable
Amazon DynamoDB
Voldemort
Cassandra
Riak
Redis
CouchDB
MongoDB


front: What is MVCC?
back:
Multiversion concurrency control (MCC or MVCC), is a concurrency control method commonly used by database management systems to provide concurrent access to the database and in programming languages to implement transactional memory.


front: What is the S in SOLID?
back:
The single responsibility principle. There should never be more than one reason for a class to change. We can relate the “reason to change” to “the responsibility of the class”. So each responsibility would be an axis for change.


front: What does concurrent.futures do?
back:
The concurrent.futures modules provides interfaces for running tasks using pools of thread or process workers. The APIs are the same, so applications can switch between threads and processes with minimal changes.


front: What is the O in SOLID?
back:
The Open/Closed Principle (OCP) states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged.
"open for extension / closed for modifications"

--

During the 1990s, the open/closed principle became popularly redefined to refer to the use of abstracted interfaces, where the implementations can be changed and multiple implementations could be created and polymorphically substituted for each other.

In contrast to Meyer's usage, this definition advocates inheritance from abstract base classes. Interface specifications can be reused through inheritance but implementation need not be.


front: What is the L in SOLID?
back:
The Liskov substitution principle (LSP)
We must make sure that the new derived classes just extend without replacing the functionality of old classes. Otherwise the new classes can produce undesired effects when they are used in existing program modules.

Liskov's Substitution Principle states that if a program module is using a Base class, then the reference to the Base class can be replaced with a Derived class without affecting the functionality of the program module.

This principle is just an extension of the Open Close Principle and it means that we must make sure that new derived classes are extending the base classes without changing their behavior.


front: What is the I in SOLID?
back:
The Interface Segregation Principle (ISP) states that clients should not be forced to implement interfaces they don't use. Instead of one fat interface many small interfaces are preferred based on groups of methods, each one serving one submodule.


front: What is the D in SOLID?
back:
Dependency inversion principle (DIP)
High-level modules should not depend on low-level modules. Both should depend on abstractions.
Abstractions should not depend on details. Details should depend on abstractions.


front: What are 3 things CDNs use to ensure availability?
back:
Local clustering can improve fault-tolerance and scalability. Mirroring (deploying clusters in a few locations) and multihoming (using multiple ISPs to connect to the Internet). 
Clustering, mirroring, and multihoming are common approaches for sites with stringent reliability and scalability needs.


front: What is hyper-threading?
back:
Hyper-threading enables a single processor core to be used for two or more concurrent executions with just a little extra hardware.


front: What is DMA?
back:
DMA (Direct Memory Access) allows devices, with the help of the Northbridge, to store and receive data in RAM directly without the intervention of the CPU.


front: Where are SRAM and DRAM used?
back:
SRAMs are used in Caches because of higher speed and DRAMs are used for main memory in a PC because of higher densities.


front: What is the difference between SRAM and DRAM?
back:
DRAM stands for Dynamic Random Access Memory. It is a type of semiconductor memory in which the memory is stored in the form of a charge. Each memory cell in a DRAM is made of a transistor and a capacitor. The data is stored in the capacitor. Capacitors loose charge due to leakage and hence DRAM's are volatile devices. To keep the data in the memory, the device must be regularly refreshed whereas SRAM is static, so it will retain a value as long as power is supplied. SRAM is typically faster than DRAM since it doesn't have refresh cycles. Since each SRAM memory cell is comprised of 6 Transistors unlike a DRAM memory cell, which is comprised of 1 Transistor and 1 Capacitor, the cost per memory cell is far greater in an SRAM compared to a DRAM.


front: What is the difference between a CPU core and a CPU thread?
back:
The difference between a core and a thread is that separate cores have separate copies of (almost) all the hardware resources. The cores can run completely independently unless they are using the same resources–e.g., the connections to the outside - at the same time. Threads, on the other hand, share almost all of the processor’s resources.
Intel’s implementation of threads has only separate registers for the threads and even that is limited, some registers
are shared.


front: What is SMP?
back:
symmetric multi-processor

In symmetric multi-processor (SMP) systems the caches of the CPUs cannot work independently from each other. All processors are supposed to see the same memory content at all times. The maintenance of this uniform view of memory is called “cache coherency”.


front: How does a multi-processor CPU maintain cache coherency?
back:
- A dirty cache line is not present in any other processor’s cache.
- Clean copies of the same cache line can reside in arbitrarily many caches.


front: What is Spanner?
back:
Spanner is a scalable, globally-distributed database designed, built, and deployed at Google. At the highest level of abstraction, it is a database that shards data across many sets of Paxos state machines in datacenters spread all over the world. Replication is used for global availability and geographic locality; clients automatically failover between replicas. Spanner automatically reshards data across machines as the amount of data or the number of servers changes, and it automatically migrates data across machines (even across datacenters) to balance load and in response to failures. Spanner is designed to scale up to millions of machines across hundreds of datacenters and trillions of database rows.


front: What is Marzullo's algorithm?
back:
Marzullo's algorithm, is an agreement algorithm used to select sources for estimating accurate time from a number of noisy time sources. A refined version of it, renamed the "intersection algorithm", forms part of the modern Network Time Protocol.


front: What is Google File System?
back:
A scalable distributed file system for large distributed data-intensive applications. It provides fault tolerance while running on inexpensive commodity hardware, and it delivers high aggregate performance to a large number of clients.
It deals with multi-GB and TB files by appending to files, as that is the predominant usage pattern of large data files.


front: What is BNF?
back:
BNF (Backus Normal Form or Backus–Naur Form) is one of the two main notation techniques for context-free grammars, often used to describe the syntax of languages used in computing, such as computer programming languages, document formats, instruction sets and communication protocols; the other main technique for writing context-free grammars is the van Wijngaarden form.


front: What is MapReduce?
back:
MapReduce, developed by Google in 2004, is a programming model and an associated implementation for processing and generating large data sets. Users specify a map function that processes a key/value pair to generate a set of intermediate key/value pairs, and a reduce function that merges all intermediate values associated with the same intermediate key.


front: What is a Zipf distribution?
back:
The Zipf distribution, sometimes referred to as the zeta distribution, is a discrete distribution commonly used in linguistics, insurance, and the modeling of rare events.


front: What is AddressSanitizer?
back:
AddressSanitizer is a fast memory error detector. AddressSanitizer finds out-of-bounds (for heap, stack, and globals) accesses and use-after-free bugs at the cost of 73% slowdown on average and a 3.4x memory size; the tool has no false positives.
AddressSanitizer uses shadow memory to provide accurate and immediate bug detection. The conventional wisdom is that shadow memory either incurs high overhead through multi-level mapping schemes or imposes prohibitive address space requirements by occupying a large contiguous region. Our novel shadow state encoding reduces our shadow space footprint enough that we can use a simple mapping, which can be implemented with low overhead.
It has been included as a compilation option in LLVM since 3.1.


front: What is transitive closure?
back:
transitive closure can be thought of as constructing a data structure that makes it possible to answer reachability questions. That is, can one get from node a to node d in one or more hops?


front: What is CUDA?
back:
CUDA (Compute Unified Device Architecture) is a parallel computing platform and application programming interface (API) model created by NVIDIA.[1] It allows software developers and software engineers to use a CUDA-enabled graphics processing unit (GPU) for general purpose processing – an approach known as GPGPU. The CUDA platform is a software layer that gives direct access to the GPU's virtual instruction set and parallel computational elements, for the execution of compute kernels.[2]


front: What is Borg?
back:
The first unified container-management system developed at Google. It was built to manage both long-running services and batch jobs.


front: What is MPM?
back:
Within Google, MPM (Midas Package Manager) is used to build and deploy container images. It corresponds to the Docker image registry for Docker containers.


front: What are 3 benefits of containers?
back:
1. Containers encapsulate the application environment, abstracting away many details of machines and operating systems from the application developer and the deployment infrastructure.
2. Because well-designed containers and container images are scoped to a single application, managing containers means managing applications rather than machines. This shift of management APIs from machine-oriented to application oriented dramatically improves application deployment and introspection.
3. Decoupling of image and OS makes it possible to provide the same deployment environment in both development and production, which, in turn, improves deployment reliability and speeds up development by reducing inconsistencies and friction.


front: What is Protocol buffers?
back:
Protocol buffers (aka protobuf) are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data. IDL-based.

You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.


front: Why is using Protocol buffers better than pickling?
back:
Python pickling doesn't deal well with schema evolution, and also doesn't work very well if you need to share data with applications written in C++ or Java.


front: What is gRPC?
back:
It’s an open source framework for RPC by Google. gRPC uses HTTP/2 and Google’s own Protobuf to provide a scalable and low latency communication. With gRPC comes a new version of Protobuf (proto3) for high performance binary serialization which includes new features and is easier to use than its predecessors.


front: What is Redis?
back:
Redis is an in-memory data structure store, used as database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.


front: What commands can be used to make a Redis queue somewhat reliable?
back:
LPUSH
RPOPLPUSH - pop right, pushing onto another list for processing, atomically,
BRPOPLPUSH - same as above, but blocking
RPOP

Redis is more susceptible to data loss in the event of abrupt termination or power failures.


front: What is RabbitMQ?
back:
RabbitMQ is a messaging broker - an intermediary for messaging.

Messages are routed through exchanges before arriving at queues. RabbitMQ features several built-in exchange types for typical routing logic. For more complex routing you can bind exchanges together or even write your own exchange type as a plugin.

It can be used as a durable queue, work queues, pub/sub, topic handler, and even for rpc.


front: What is pika?
back:
Pika is a pure-Python implementation of the AMQP 0-9-1 protocol that tries to stay fairly independent of the underlying network support library. Can use with RabbitMQ.


front: What is Celery?
back:
Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation, but supports scheduling as well. You use it with a message broker, and it manages the task execution.

The execution units, called tasks, are executed concurrently on a single or more worker servers using multiprocessing, Eventlet, or gevent. Tasks can execute asynchronously (in the background) or synchronously (wait until ready).

The recommended message broker is RabbitMQ, but support for Redis, Beanstalk, MongoDB, CouchDB, and databases (using SQLAlchemy or the Django ORM) is also available.


front: What is ZeroMQ?
back:
A socket-based system, can be used as a queue, pub/sub, etc.

Carries messages across inproc, IPC, TCP, TIPC, multicast.
Smart patterns like pub-sub, push-pull (pipeline), and router-dealer.


front: What is IPC?
back:
Inter-process communication or interprocess communication (IPC) refers specifically to the mechanisms an operating system provides to allow processes it manages to share data. Typically, applications can use IPC categorized as clients and servers, where the client requests data and the server responds to client requests.


front: What is Kafka?
back:
Apache Kafka is a distributed, partitioned, replicated commit log service. It provides the functionality of a messaging system, but with a unique design.


front: What is MessagePack?
back:
MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON. But it's faster and smaller. Small integers are encoded into a single byte, and typical short strings require only one extra byte in addition to the strings themselves.

No IDL.


front: What is Avro?
back:
Apache Avro is a data serialization system. IDL-based.
Rich data structures. 
A compact, fast, binary data format. 
A container file, to store persistent data. 
Remote procedure call (RPC).
Code generation is not required to read or write data files nor to use or implement RPC protocols. Code generation as an optional optimization, only worth implementing for statically typed languages.


front: What is a Bloom filter?
back:
A Bloom filter is a data structure used to quickly test membership in a set where the number and size of possible elements would be very large. Too large to keep in memory.
A Bloom filter is a space-efficient probabilistic data structure, conceived by Burton Howard Bloom in 1970, that is used to test whether an element is a member of a set. False positive matches are possible, but false negatives are not, thus a Bloom filter has a 100% recall rate. In other words, a query returns either "possibly in set" or "definitely not in set". Elements can be added to the set, but not removed (though this can be addressed with a "counting" filter). The more elements that are added to the set, the larger the probability of false positives.


front: How can you easily generate multiple hashes for the same element?
back:
Double hashing. This method gives you as many hashes as you need:
hash(x,m) = (hasha(x) + i * hashb(x)) mod m

In Python:
import mmh3
mmh3.hash64('foo') # two 64 bit signed ints, in a tuple
now you have 2 64-bit hashes. Substituting for i gives you multiple hashes for a Bloom filter.


front: What is DFT?
back:
Discrete Fourier Transform - converts a finite sequence of equally-spaced samples of a function into an equivalent-length sequence of equally-spaced samples of the discrete-time Fourier transform (DTFT), which is a complex-valued function of frequency.


front: What is a cache-oblivious algorithm?
back:
A cache-oblivious algorithm does not mean that the algorithm does not take advantage of the cache; to the contrary, it does so quite effectively. What it means is that the algorithm does not need to know the cache line size; it works effectively for all cache line sizes simultaneously, removing the need to tune or optimize for a given machine.

Optimal cache-oblivious algorithms are known for the Cooley–Tukey FFT algorithm, matrix multiplication, sorting, matrix transposition, and several other problems.


front: How can you augment a splay tree so you can find how many items are between x and y?
back:
Store size of subtrees at each node. 
Find x, splay to root. Each splay, insert, and delete must maintain size in node.
Find y, and along the way add up the sizes in the left subtrees, and 1 for each visited left-hand node.
Splay y to root to ensure balance.


front: What is the Ford-Fulkerson algorithm?
back:
The Ford–Fulkerson method or Ford–Fulkerson algorithm (FFA) is a greedy algorithm that computes the maximum flow in a flow network. It is called a "method" instead of an "algorithm" as the approach to finding augmenting paths in a residual graph is not fully specified or it is specified in several implementations with different running times. The name "Ford–Fulkerson" is often also used for the Edmonds–Karp algorithm, which is a specialization of Ford–Fulkerson.


front: What is the running time for the disjoint set data structure?
back:
Due to merging smaller disjoint sets into larger ones (called union by rank) (during union) and performing path compression (during find), the amortized time per operation is only O(alpha(n)), where alpha(n) is the inverse of the function and A is the extremely fast-growing Ackermann function. Since alpha(n) is the inverse of this function, alpha(n) is less than 5 for all remotely practical values of n. Thus, the amortized running time per operation is effectively a small constant.

The worst-case for find() is Theta(log u) where u is the number of unions, and no finds have been done to allow for path compression yet.


front: In testing, what is an oracle?
back:
Software testers and software engineers can use an oracle as a mechanism for determining whether a test has passed or failed. The use of oracles involves comparing the output(s) of the system under test, for a given test-case input, to the output(s) that the oracle determines that product should have.


front: What do you test interfaces with a trust boundary?
back:
With lots of different inputs, including random inputs to ensure that the system can handle or appropriately error out, not crash.


front: What should be avoided to ensure testing is easier/possible?
back:
- static methods and properties
- final keyword
- use of new in methods (use dependency injection)


front: What are some guidelines to keep in mind to not violate the dependency inversion principle?
back:
- No variable should have a concrete class type. An abstract type is better.
- No class should derive from a concrete class.
- No method should override an implemented method of any of its base classes.

These are guidelines and may not be feasible all the time.


front: What is a trap/exception?
back:
A special instruction that a program performs to interrupt the process and give control to the kernel.


front: What does the scheduler do?
back:
Schedulers are special system software which handles process scheduling in various ways. Their main task is to select the jobs to be submitted into the system and to decide which process to run.


front: What is separate chaining?
back:
In hash table conflict resolution, each bucket is independent and has some sort of linked list of entries with the same index. The time for hash table operations is the time to find the bucket (which is constant) plus the time for the list operation.

In a good hash table, each bucket has zero or one entries, and sometimes two or three, but rarely more than that. Therefore, structures that are efficient in time and space for these cases are preferred. Structures that are efficient for a fairly large number of entries per bucket are not needed or desirable. If these cases happen often, the hashing function needs to be fixed.


front: What is open addressing?
back:
In hash table conflict resolution, all entry records are stored in the bucket array itself. When a new entry has to be inserted, the buckets are examined, starting with the hashed-to slot and proceeding in some probe sequence, until an unoccupied slot is found. When searching for an entry, the buckets are scanned in the same sequence, until either the target record is found, or an unused array slot is found, which indicates that there is no such key in the table. The name "open addressing" refers to the fact that the location ("address") of the item is not determined by its hash value. (This method is also called closed hashing; it should not be confused with "open hashing" or "closed addressing" that usually mean separate chaining.)


front: Where can octet order (Endianness) problems crop up in files?
back:
In binary files, if you aren't consistent with choosing an order. Files encoded in utf8 or ASCII don't have this issue.


front: What defines a complete binary tree, and give an example?
back:
The same number of nodes at all levels of the tree, except at leaf level where it fills in left to right. A heap stored as an array is an example.


front: What is a 2-3-4 tree?
back:
2–3–4 tree (also called a 2–4 tree) is a self-balancing data structure that is commonly used to implement dictionaries. The numbers mean a tree where every node with children (internal node) has either two, three, or four child nodes:

- 2-node has one data element, and if internal has two child nodes;
- 3-node has two data elements, and if internal has three child nodes;
- 4-node has three data elements, and if internal has four child nodes.


front: In a b-tree, how many children are there per node?
back:
root: 1 to 2t-1 keys
non-root: t-1 to 2t-1 keys
t could be up to 100, or more.
There are n keys and n+1 children.
Leaves are all the same level.


front: Give an example of how a b-tree might be organized.
back:
1024 children per node.
Store root in memory.
3 nodes accessed gets us 1024^3 disk pages.
4 nodes accessed gets us 1024^4 disk pages.


front: What is a suffix tree?
back:
A suffix tree is a compressed trie containing all the suffixes of the given text as their keys and positions in the text as their values. Suffix trees allow particularly fast implementations of many important string operations.

The construction of such a tree for the string S takes time and space linear in the length of S. Once constructed, several operations can be performed quickly, for instance locating a substring in S, locating a substring if a certain number of mistakes are allowed, locating matches for a regular expression pattern etc. Suffix trees also provide one of the first linear-time solutions for the longest common substring problem. These speedups come at a cost: storing a string's suffix tree typically requires significantly more space than storing the string itself.


front: In brief, how does selection sort work?
back:
Find the minimum item on each pass, past the previous minimum, and swap it into the leftmost position after the previous minimum.


front: What is counting sort?
back:
Counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the difference between the maximum and minimum key values, so it is only suitable for direct use in situations where the variation in keys is not significantly greater than the number of items. However, it is often used as a subroutine in another sorting algorithm, radix sort, that can handle larger keys more efficiently.


front: What is radix sort?
back:
Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. 
Two classifications of radix sorts are least significant digit (LSD) radix sorts and most significant digit (MSD) radix sorts. LSD radix sorts process the integer representations starting from the least digit and move towards the most significant digit. MSD radix sorts work the other way around.


front: How to choose q for radix sort?
back:
Choose q within a power of 2 of n. Ensures the number of passes is small. Best rule is n rounded down to the next power of 2.
To save memory, round sqrt(n) down to the next power of 2. Twice as many passes.


front: What is the Day–Stout–Warren (DSW) algorithm?
back:
The Day–Stout–Warren (DSW) algorithm is a method for efficiently balancing binary search trees — that is, decreasing their height to O(log n) nodes, where n is the total number of nodes. Unlike a self-balancing binary search tree, it does not do this incrementally during each operation, but periodically, so that its cost can be amortized over many operations.


back: Topological sort
front:
What do you call a linear ordering of a directed graph of its vertices such that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering?


front: What is a good method for performing a topological sort?
back:
1. Calculate in-degree for each node. O(v + e)
2. Go through 0s, add to queue.
3. For each item in queue, look at each connection, and decrement in-degree of each, if they got to 0, add to queue, repeat.


front: What is Kruskal's algorithm?
back:
Kruskal's algorithm is a minimum-spanning-tree algorithm which finds an edge of the least possible weight that connects any two trees in the forest. It is a greedy algorithm in graph theory as it finds a minimum spanning tree for a connected weighted graph adding increasing cost arcs at each step. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. If the graph is not connected, then it finds a minimum spanning forest (a minimum spanning tree for each connected component).


front: How can you get the strongly connected components of a graph?
back:
1. DFS - calculate the finish times for each node
2. Reverse the edges in the graph
3. Call DFS on nodes in reverse graph in reverse order of finishing times.


front: How to find the longest path on a weighted DAG?
back:
1. Set all edges to their negative weight.
2. Topological sort 
3. follow the topological sort, relaxing edges


front: What is the diameter of a graph?
back:
The shortest path of the farthest nodes. That is, it is the greatest distance between any pair of vertices. To find the diameter of a graph, first find the shortest path between each pair of vertices. The greatest length of any of these paths is the diameter of the graph.


front: In plain words, how does Kruskal's algorithm work?
back:
1. Create a set T and list for result
2. Make a list of all edges in G
3. Sort edges by weight, from least to greatest.
4. Iterate edges in sorted order.
5. For each edge, if u and v are not in T, add u and v to T, and add edge to result list.


front: What can most dynamic programming problems be expressed as?
back:
Finding the shortest path in a DAG. Formulating it this way ensures you can solve it in linear or linearithmic time.


front: What kinds of problems is dynamic programming best suited for?
back:
- optimizing left to right sequences (strings, tree nodes as array, permutations)
- search all possibilities while storing results to avoid recomputing


front: What is the Floyd-Warshall algorithm?
back:
The Floyd–Warshall algorithm is a dynamic programming algorithm for finding shortest paths in a weighted graph with positive or negative edge weights (but with no negative cycles).


front: What is a Markov chain?
back:
A Markov chain consists of states linked by transitions labeled with probabilities. The states do not have to be words. They could represent any state.


front: What does it mean when a problem is NP-Hard?
back:
It is as hard as any other problem in NP. A problem X is NP-Hard if every problem Y in NP-Hard reduces to X.


front: Name some NP-Complete problems.
back:
- tsp
- knapsack problem (decision problem)
- satisfiability
- 3D matching
- tricoloring
- subset sum
- rectangle packing
- bin packing
- vertex cover
- set cover


front: What is one way of doing approximate traveling salesman?
back:
Select a vertex as root.
Build a MST.
Do a preorder traversal, store nodes in H.
Return H (a Hamiltonian cycle)


front: How can an LRU cache be implemented with a linked list?
back:
When an item is accessed, it moves to the head of the list.
The trailing items can be overwritten with new items, or removed.


front: What does Skylake architecture look like?
back:
Skylake: 

L1 Data cache = 32 KB, 64 B/line, 8-WAY. 64 cache lines per cache way
L1 Instruction cache = 32 KB, 64 B/line, 8-WAY. 64 cache lines per cache way
L2 cache = 256 KB, 64 B/line, 4-WAY
L3 cache = 8 MB, 64 B/line, 16-WAY


front: What is a TLB?
back:
A translation lookaside buffer (TLB) is a cache that memory management hardware uses to improve virtual address translation speed. The majority of desktop, laptop, and server processors includes one or more TLBs in the memory management hardware, and it is nearly always present in any hardware that utilizes paged or segmented virtual memory.


front: In a direct mapped or set associative cache, what is special about the cache size?
back:
Main memory is divided into pages, and a memory page maps directly to the cache way size. So an item in a cache page can be mapped to any one of 8 cache ways in an 8-way associative cache. A direct mapped cache simply has one cache way, but it works the same way.


front: Is it always letters we are looking for in compression?
back:
No. Hardly.
We are looking to compress discrete symbols, not letters. If we have a few words, they could be symbols.
We try to balance longest chains of symbols and the lowest entropy.


front: What is Zopfli?
back:
Zopfli is data compression software that encodes data into DEFLATE, gzip and zlib formats. It achieves higher compression than other DEFLATE/zlib implementations, but takes much longer to perform the compression. It was first released in February 2013 by Google.


front: What is the Chinese remainder theorem?
back:
If one knows the remainders of the division of an integer n by several integers, then one can determine uniquely the remainder of the division of n by the product of these integers, under the condition that the divisors are pairwise coprime.


front: What is polymorphism in OO?
back:
A property of OO in which an abstraction operation may be performed in different ways in different classes, but share an interface.
Also can mean multiple methods of the same name but different signature.


front: What is dynamic binding?
back:
Dynamic binding, also called dynamic dispatch, is the process of linking procedure call to a specific sequence of code (method) at run-time. Dynamic binding is also known as late binding or run-time binding. Dynamic binding is an object oriented programming concept and it is related with polymorphism and inheritance.


front: What is an interface in OO?
back:
Similar to an abstract data type, but simply defines the expected behaviors of a class, but does not suggest an implementation.


front: What is a framework?
back:
A reusable piece of software that implements a generic solution to a generalized problem. It saves time by being a close model of the problem domain and can reach 100% with details coded by the implementer.


front: What is an OCL?
back:
Object constraint language. A specification language designed to formally specify constraints in software modules. An OCL expression specifies a logical fact about the system that must remain true.


front: What is the Chain of Responsibility pattern?
back:
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.


front: What is the Command pattern?
back:
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.


back: Chain of Responsibility
front:
What design pattern avoids coupling the sender of a request to its receiver by giving more than one object a chance to handle the request?


back: Command pattern
front:
What design pattern encapsulates a request as an object, thereby letting you parameterize clients with different requests?


front: What is the Interpreter pattern?
back:
Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.


back: Interpreter pattern
front:
What design pattern, given a language, defines a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language?


front: What is the Iterator pattern?
back:
Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.


back: Iterator pattern
front:
What design pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation?


front: What is the Mediator pattern?
back:
Defines an object that encapsulates how a set of objects interact. Promotes loose coupling by keeping objects from referring to each other explicitly and it lets you vary their interactions independently.


front: What is the Memento pattern?
back:
Without violating encapsulation, capture and externalize an object's internal state so that the object can be restore to this state later.


back: Memento pattern
front:
What design pattern captures and externalize an object's internal state so that the object can be restore to this state later?


front: What is the Observer pattern?
back:
Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.


back: Observer pattern
front:
What design pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically?


front: What is the State pattern?
back:
Allows an object to alter its behavior when its internal state changes. The object will appear to change its class.


front: What is the Strategy pattern?
back:
Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Lets the algorithm vary independently from clients that use it.


back: Strategy pattern
front:
What design pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable, and lets the algorithm vary independently from clients that use it?


front: What is the Template Method pattern?
back:
Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.


back: Template Method pattern
front:
What design pattern defines the skeleton of an algorithm in an operation, deferring some steps to subclasses?


front: What is the Visitor pattern?
back:
Represents an operation to be performed on the elements of an object structure. Lets you define a new operation without changing the classes of the elements on which it operates.


back: Visitor pattern
front:
What design pattern represents an operation to be performed on the elements of an object structure and lets you define a new operation without changing the classes of the elements on which it operates?


front: What is the Adapter pattern?
back:
Converts the interface of a class into another interface clients expect. Lets classes work together that couldn't otherwise because of incompatible interfaces.


back: Bridge pattern
front:
What design pattern decouples an abstraction from its implementation so that the two can vary independently?


front: What is the Composite pattern?
back:
Compose objects into tree structures to represent part-whole hierarchies. Lets clients treat individual objects and compositions of objects uniformly.


front: What is the Decorator pattern?
back:
Attaches additional responsibilities to an object dynamically. Provides a flexible alternative to sub-classing for extending functionality.


back: Decorator pattern
front:
What design pattern attaches additional responsibilities to an object dynamically and provides a flexible alternative to sub-classing for extending functionality?


front: What is the Facade pattern?
back:
Provides a unified interface to a set of interfaces in a subsystem. Defines a high-level interface that makes the subsystem easier to use.


back: Facade pattern
front:
What design pattern provides a unified interface to a set of interfaces in a subsystem and defines a high-level interface that makes the subsystem easier to use.


front: What is the Abstract Factory pattern?
back:
Provides an interface for creating families of related or dependent objects without specifying their concrete class.


back: Abstract Factory pattern
front:
What design pattern provides an interface for creating families of related or dependent objects without specifying their concrete class?


front: What is the Builder pattern?
back:
Separates the construction of a complex object from its representation, so the same construction process can create different representations.


back: Builder pattern.
front:
What design pattern separates the construction of a complex object from its representation, so the same construction process can create different representations?


front: What is the Factory Method pattern?
back:
(Technically it's an idiom)
Defines an interface for creating an object, but lets subclasses decide which class to instantiate. Lets a class defer instantiation to subclasses.


back: Factory method (technically an idiom, not a design pattern)
front:
What design pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate?


front: What is the Prototype pattern?
back:
Specifies the kinds of objects to create using a prototypical instance, and creates new objects by copying this prototype.


back: Prototype pattern
front:
What design pattern specifies the kinds of objects to create using a prototypical instance, and creates new objects by copying this prototype?


back: Singleton pattern
front:
What design pattern ensures a class only has one instance and provides a global point of access to it?


front: What is the Law of Demeter?
back:
Each unit should have only limited knowledge about other units - only units "closely" related to the current unit. Each unit should only talk to friends, not strangers. Only talk to immediate friends.


front: What does an exclusive cache style?
back:
An eviction from L1D pushes the cache line down into L2, which has the same cache line size. Each eviction is progressively more expensive.


front: What is the Fast Fourier Transform?
back:
A fast Fourier transform (FFT) algorithm computes the discrete Fourier transform (DFT) of a sequence, or its inverse. Fourier analysis converts a signal from its original domain (often time or space) to a representation in the frequency domain and vice versa. An FFT rapidly computes such transformations by factorizing the DFT matrix into a product of sparse (mostly zero) factors.


front: What is a skip list?
back:
A data structure that allows fast search within an ordered sequence of elements. Fast search is made possible by maintaining a linked hierarchy of subsequences, with each successive subsequence skipping over fewer elements than the previous one. Searching starts in the sparsest subsequence until two consecutive elements have been found, one smaller and one larger than or equal to the element searched for.

A skip list is built in layers. The bottom layer is an ordinary ordered linked list. Each higher layer acts as an "express lane" for the lists below, where an element in layer i appears in layer i+1 with some fixed probability p (two commonly used values for p are 1/2 or 1/4).


front: What operations does a van Emde Boas tree support and what are the time complexities?
back:
All are O(log log M), where M is the total number of items that can be stored = 2^m
Or O(log m) where m is the actual number of items stored
Space: O(M)

Search
Insert
Delete
Predecessor
Successor


front: What are the complexities for treap operations?
back:
For all the basic maintenance operations, they are O(log n) average case and O(n) worst case.
- Search
- Insert
- Delete

For these operations, O(m log n/m) for treaps of sizes m and n, with m ≤ n. 
- union
- intersection
- difference


front: What are Catalan numbers?
back:
Catalan numbers form a sequence of natural numbers that occur in various counting problems, often involving recursively-defined objects. They can be thought of as the set of balanced parentheses.


front: What is Karatsuba multiplication?
back:
It reduces the multiplication of two n-digit numbers to at most n^1.585 single-digit multiplications in general (and exactly n^log(base2)3 when n is a power of 2). The Karatsuba algorithm was the first multiplication algorithm asymptotically faster than the quadratic "grade school" algorithm.


front: How does a Graham scan work in finding convex hull?
back:
At O(n log n), uses a sort and then a simple single pass of all the points, and making only left turns as it goes around the perimeter counter-clockwise. When the next point is a right turn, it backtracks past all points (using a stack and popping points off) until that turn turns into a left turn.


front: How does the Jarvis march work in finding convex hull?
back:
Starting with the leftmost point p: 
Go through each point to the right of that point, and using p as a pivot, find which point is the most clockwise. O(n)
Get the most clockwise point as the new p - O(1)
Loop again with new p

This continues until the starting point is reached O(h) - where h is the number of hull points


front: What are 3 situations where timing of inputs is important in testing?
back:
- when SUT interacts directly with hardware devices
- when SUT interfaces with other machines on the network
- SUT is multithreaded


front: What is a mock object?
back:
An object used to replicate the interfaces and interactions of a larger system that can be bolted on to the SUT.


front: What is system testing?
back:
- not concerned with internals
- does system as a while perform acceptably for the important use cases


front: What is differential testing?
back:
Running test inputs into 2 different implementations and then testing for equality. This is useful for testing multiple versions of the software.


front: What does code coverage not tell you?
back:
- missing errors of omission
- code that misses a specification item
- does not tell us that code looped the correct number of times


front: What should you do when you discover that not all code is covered?
back:
Don't blindly write tests to cover all cases.
Rethink the tests. Perhaps they are interpreting the functionality incorrectly.


front: What are some coverage metrics?
back:
- line coverage
- statement coverage (multiple statements on a line)
- branch coverage
- mc/dc coverage
- loop coverage


front: What is mc/dc coverage?
back:
modified condition / decision

- Required for certain types of avionics software
- Tests every possible branch and condition


front: What is infeasible code?
back:
Code that can never be true.
Infeasible code is code that is either not forward- or not backward-reachable. Infeasible code is not necessarily an error but it is a good indicator that something is wrong with the code and, e.g., in avionics, infeasible code is forbidden by current standards.


front: What is a skip list?
back:
A data structure for storing a sorted list of items using a hierarchy of linked lists that connect increasingly sparse subsequences of the items.
O(log N) expected time for all operations, O(N) worst case.


front: What is a treap?
back:
Tree + heap
A random priority is assigned to every key and must maintain two properties:
- They are in order with respect to their keys, as in a typical binary search tree
- They are in heap order with respect to their priorities, that is, no key has a key of lower priority as an ancestor
O(log N) expected time for all operations, O(N) worst case.


front: What is a max-heap?
back:
A queue in which each element has a "priority" assigned to it. Elements with higher priorities are served before lower priorities.


front: What is a binary heap?
back:
A collection of keys arranged in a complete heap-ordered binary tree, represented in level order in an array (not using the first entry). The parent of the node in position k is in position [k/2] and the two children of the node in position k are in position 2k and 2k+1.


front: What are heuristics?
back:
Any approach to problem solving, learning, or discovery that employs a practical method not guaranteed to be optimal or perfect, but sufficient for the immediate goals. Where finding an optimal solution is impossible or impractical, heuristic methods can be used to speed up the process of finding a satisfactory solution. Heuristics can be mental shortcuts that ease the cognitive load of making a decision. Examples of this method include using a rule of thumb, an educated guess, an intuitive judgment, stereotyping, profiling, or common sense


front: What is internal sorting?
back:
An internal sort is any data sorting process that takes place entirely within the main memory of a computer. This is possible whenever the data to be sorted is small enough to all be held in the main memory. For sorting larger datasets, it may be necessary to hold only a chunk of data in memory at a time, since it won't all fit. The rest of the data is normally held on some larger, but slower medium, like a hard-disk. Any reading or writing of data to and from this slower media can slow the sortation process considerably.


front: What is external sorting?
back:
External sorting is a term for a class of sorting algorithms that can handle massive amounts of data. External sorting is required when the data being sorted do not fit into the main memory of a computing device (usually RAM) and instead they must reside in the slower external memory (usually a hard drive). External sorting typically uses a hybrid sort-merge strategy. In the sorting phase, chunks of data small enough to fit in main memory are read, sorted, and written out to a temporary file. In the merge phase, the sorted subfiles are combined into a single larger file.

Mergesort is typically preferred.


front: What are long-term issues involved in machine learning?
back:
- technical debt
- lack of clear abstraction barriers 
- changing anything changes everything
- feedback loop - usage based on your model changes the model
- attractive nuisance (using a successful model in one domain where it doesn't fit in another)
- non-stationarity - stick with current data, or get new, and how much of old data to reuse
- tracking data dependencies - where did the data come from, how to get new data


front: What is supervised learning?
back:
Supervised learning is the machine learning task of inferring a function from labeled training data. The training data consist of a set of training examples. In supervised learning, each example is a pair consisting of an input object (typically a vector) and a desired output value (also called the supervisory signal).


front: What is a regression problem?
back:
One that predicts a continuously valued output. May refer specifically to the estimation of continuous response variables, as opposed to the discrete response variables used in classification.


front: What is a classification problem?
back:
The problem of identifying to which of a set of categories (sub-populations) a new observation belongs, on the basis of a training set of data containing observations (or instances) whose category membership is known.


front: What is unsupervised learning?
back:
Unsupervised learning is the machine learning task of inferring a function to describe hidden structure from unlabeled data. Since the examples given to the learner are unlabeled, there is no error or reward signal to evaluate a potential solution. This distinguishes unsupervised learning from supervised learning and reinforcement learning.


front: What is gradient descent?
back:
Gradient descent is a first-order iterative optimization algorithm. To find a local minimum of a cost function using gradient descent, one takes steps proportional to the negative of the gradient (partial derivative or tangent) of the function at the current point.


front: Why is feature scaling important?
back:
Without feature scaling, it can take gradient descent much longer to find the local minimum. The function may oscillate in small movements for much longer.


front: How can you calculate mean normalization over a set of features?
back:
for each x: xi = (xi - avg(x)) / (max_x - min_x)

(max_x - min_x) can be replaced with the standard deviation


front: How can you get your data to fit better using higher order terms in linear regression?
back:
You can take the data and square it, cube it, etc. 

x1 = (somevalue)^2
x7 = (somevalue)^4

This is applied to data, not the function. The function still is theta1*x1 + theta2*x2, etc


front: What can affect invertibility in a matrix?
back:
- 2 more more features are linearly dependent or redundant (size in meters and size in ft) - remove redundant features
- too few training examples (m) to features (n): m <= n  (remove some features or use regularization)


front: What is the normal equation?
back:
An equation that can minimize J(theta), solving for theta, instead of using gradient descent.

Also called ordinary least squares (OLS) or linear least squares, it's a method for estimating the unknown parameters in a linear regression model, with the goal of minimizing the sum of the squares of the differences between the observed responses in the given dataset and those predicted by a linear function of a set of explanatory variables.


front: What is a dot product?
back:
The dot product of two vectors is the sum of their componentwise products.
v_1 * w_1 + ... + v_n * w_n


front: What is the mode?
back:
Most-common value(s) in a set of data. Could have more than one if there are 2 subsets with the same number of values.


front: What is dispersion?
back:
Dispersion refers to measures of how spread out our data is. Typically they’re statistics for which values near zero signify not spread out at all and for which large values (whatever that means) signify very spread out.


front: What is a random variable?
back:
A random variable is a variable whose possible values have an associated probability distribution.

A very simple random variable equals 1 if a coin flip turns up heads and 0 if the flip turns up tails. A more complicated one might measure the number of heads observed when flipping a coin 10 times or a value picked from range(10) where each number is equally likely.


front: What is PCA and what is it used for?
back:
Principal component analysis. We use it to extract one or more dimensions that capture as much of the variation in the data as possible.


front: What is a model?
back:
A specification of a mathematical (or probabilistic) relationship that exists between different variables.


front: What is quick select?
back:
A selection algorithm to find the kth smallest element in an unordered list. Quickselect uses the same overall approach as quicksort, choosing one element as a pivot and partitioning the data in two based on the pivot, accordingly as less than or greater than the pivot. However, instead of recursing into both sides, as in quicksort, quickselect only recurses into one side - the side with the element it is searching for. This reduces the average complexity from O(n log n) to O(n).


front: What is preemption?
back:
Preemption is the act of temporarily interrupting a task being carried out by a computer system, without requiring its cooperation, and with the intention of resuming the task at a later time. Such a change is known as a context switch.


front: What is an inverted index?
back:
An index data structure storing a mapping from content, such as words or numbers, to its locations in a database file, or in a document or a set of documents (named in contrast to a Forward Index, which maps from documents to content). The purpose of an inverted index is to allow fast full text searches, at a cost of increased processing when a document is added to the database.


front: What is set partition?
back:
A partitioning of elements of some universal set into a collection of disjointed subsets. Thus, each element must be in exactly one subset.


front: What is a maximum spanning tree?
back:
A spanning tree of a weighted graph having maximum weight. It can be computed by negating the edges and running either Prim's or Kruskal's algorithms.


front: What is a minimum product spanning tree and when would you use it?
back:
The cost of a tree is the product of all the edge weights in the tree, instead of the sum of the weights. Since log(a*b) = log(a) + log(b), the minimum spanning tree on a graph whose edge weights are replaced with their logarithms gives the minimum product spanning tree on the original graph.
You would use it to minimize the product.


front: What is a rolling hash?
back:
A rolling hash (also known as a rolling checksum) is a hash function where the input is hashed in a window that moves through the input.

One of the main applications is the Rabin-Karp string search algorithm, which uses the rolling hash.


front: What is the Rabin-Karp algorithm?
back:
Compute hash codes of each substring whose length is the length of s, such as a function with the property that the hash code of a string is an additive function of each individual character. Get the hash code of a sliding window of characters and compare if the hash matches.


front: What is sharding?
back:
Sharding is a type of database partitioning that separates very large databases the into smaller, faster, more easily managed parts called data shards.


front: What is von Neumann Architecture?
back:
A model for modern computer organization created by John von Neumann, that had two main features:
1) Both data & instructions are stored in the same place
2) Units that store information are different from units that process information


front: What is the fetch-execute cycle?
back:
A 4 part system that describes how actions are performed in the CPU. There are 4 parts to this cycle:
1) Fetch instructions
2) Decode instructions
3) Get data if needed
4) Execute the instructions


front: What does a Control Unit (CU) do?
back:
Controls, organizes and deals with all the process and instruction the CPU receives. It is also in charge of the Fetch-Execute Cycle. Has two special purpose registers: the Instruction Register and the Program Counter.


front: What is the Bellman–Ford algorithm?
back:
An algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph. It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers.


front: What is a Hamiltonian cycle?
back:
Given an undirected graph G = (V, E), does there exist a simple
cycle Γ that contains every node in V ?
Certificate is a permutation of the n nodes, contain each node in v exactly once, there is an edge btw each pair of adj nodes in the permutation.


front: What is the set cover problem?
back:
Given a set U of elements, a collection S1, S2, ..., Sm of subsets of
U, and an integer k, does there exist a collection of ≤ k of these sets whose union is equal to U ?


front: How would you divide up a data set for training and testing?
back:
Split your data set, so that two-thirds of it is used to train the model, after which we test/measure the model’s performance on the remaining third.


front: How would you split up a data set in order to choose from multiple models?
back:
In such a situation, you should split the data into three parts: a training set for building models, a validation set for choosing among trained models (called the cross-validation set), and a test set for judging the final model.


front: What is the trade-off between precision and recall?
back:
A model that predicts “yes” when it’s even a little bit confident will probably have a high recall but a low precision; a model that predicts “yes” only when it’s extremely confident is likely to have a low recall and a high precision.

Alternatively, you can think of this as a trade-off between false positives and false negatives. Saying “yes” too often (high recall) will give you lots of false positives; saying “no” too often will give you lots of false negatives (high precision).


back: One thing to try is adding more features.
front:
What can you do when your model has high bias (which means it performs poorly even on your training data)?


front: What can you do if your model suffers from overfitting due to high variance?
back:
You can remove features. Another solution is to obtain more training examples (if you can).
Or use regularization.


front: What is L-BFGS?
back:
Limited-memory BFGS (L-BFGS or LM-BFGS) is an optimization algorithm in the family of quasi-Newton methods that approximates the Broyden–Fletcher–Goldfarb–Shanno (BFGS) algorithm using a limited amount of computer memory. It is a popular algorithm for parameter estimation in machine learning.


front: How should you initialize Theta for a neural network?
back:
Initialize as a matrix of random reals between 0 and 1. Constrain within a range of +/- epsilon using Theta * 2*epsilon - epsilon.


back: The number of classes you are classifying.
front:
What are the number of neurons (units) at the output layer of a neural network performing classification?


front: How many hidden layers should there be in a neural network?
back:
Start with 1 as a default, and if more than one, have the same number of units at each layer. The more the better. The number of units in each hidden layer should be more than, or a multiple of, the input units.


back: The error decreases, but too high a degree of polynomial will cause overfitting.
front:
Machine learning: What tends to happen with the training error in a linear model as the degree of polynomial increases?


back: It starts high (high bias) and decreases, reaching a minimum, and then increases (high variance).
front:
Machine learning: What tends to happen with the cross-validation error in a linear model as the degree of polynomial increases?


back: traceroute
front:
What command line tool allows you to see the internet connections between you and a given server IP or domain name?


front: Is there much difference between grep, egrep, and grep -P?
back:
Yes, grep doesn't support fancy regex.
egrep does extended
grep -P does perl-style but that's pretty close to egrep - can't see a difference


front: What are the 4 parts of compiling a C program?
back:
- preprocessor
- compiler - outputs assembly
- assembler - outputs machine/object code
- linked - links in dependent object files


back: awk '{ if($2 ~ /foo/) print $1,$2}' somefile
front:
How can you print the 1st and 2nd column of a file only when the second column contains the word foo?


front: What is strace?
back:
strace is a diagnostic, debugging and instructional userspace utility for Linux. It is used to monitor interactions between processes and the Linux kernel, which include system calls, signal deliveries, and changes of process state. The operation of strace is made possible by the kernel feature known as ptrace.


front: What is tcpdump?
back:
tcpdump is the premier network analysis tool for information security professionals. Having a solid grasp of this über-powerful application is mandatory for anyone desiring a thorough understanding of TCP/IP.


front:
In Linux, how can you search a set of files of phone numbers, which are badly formatted, and output the numbers, using regular phone number formatting and no blank lines?

back:
cat numbers-* | sed 's/[() -]//g' | grep -P '^\d{10}$' | sed -r 's/([0-9]{3})([0-9]{3})(.*)/(\1) \2-\3/'


front: How can we avoid deadlock?
back:
We can prevent deadlock by assigning an order to locks and require that the locks be acquired in that order. However, this approach is not often used in practice.


front: How can you tell if 2 rectangles do not overlap?
back:
The intersection of R1 and R2 will be a rectangle R3 whose bottom-left corner is at (max(x1, x3), max(y1, y3)) and top-right corner at (min(x2, x4), min(y2, y4)). If max(x1, x3) > min(x2, x4) or max(y1, y3) > min(y2, y4) then R3 does not exist, ie R1 and R2 do not intersect.


front: What happens to a class when you add __slots__?
back:
Space is saved because __dict__ is not created for each instance. __slots__ reserves space for the declared variables and prevents the automatic creation of __dict__ and __weakref__ for each instance.
If weak reference support is needed, then add '__weakref__' to the sequence of strings in the __slots__ declaration.


front: What happens when inheriting from a class without __slots__?
back:
When inheriting from a class without __slots__, the __dict__ attribute of that class will always be accessible, so a __slots__ definition in the subclass is meaningless.


front: If you use __slots__ on a class, what happens in subclasses?
back:
The action of a __slots__ declaration is limited to the class where it is defined. As a result, subclasses will have a __dict__ unless they also define __slots__ (which must only contain names of any additional slots).


front: What 4 things should you check when given a debugging problem?
back:
- is the input valid and correct?
- check that each line is correct and in the right order
- check that the output is correct
- check for common errors, and special cases for and algorithm or data structure


front: What determines the height of a tree?
back:
The height of a tree equals the height of its tallest subtree plus one.
Use 1 + max(subtree1, subtree2)


front: What is Timsort?
back:
A hybrid stable sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data.
The algorithm finds subsequences of the data that are already ordered, and uses that knowledge to sort the remainder more efficiently. This is done by merging an identified subsequence, called a run, with existing runs until certain criteria are fulfilled. Timsort has been Python's standard sorting algorithm since version 2.3.


front: What is concurrency?
back:
Running tasks at almost the same time in an unspecified order. This is achieved through context switches by the operating system.


front: What is a mutex?
back:
A mutually exclusive access to a resource. It's a special case of a semaphore with a maximum concurrency of 1.


front: What is busy waiting?
back:
Busy-waiting, busy-looping or spinning is a technique in which a process repeatedly checks to see if a condition is true, such as whether keyboard input or a lock is available.


back: Merge step of merge sort.
front:
What should you do when you have one or more lists that are sorted and you need to combine them into a sorted list?


back: Selection sort guarantees no more than n - 1 swaps.
front:
What sort can you use when you have data you want to sort and the comparisons of keys are cheap but moving the data is expensive?


back: An unstable, in-place sort like quicksort.
front:
What kind of sort can you use when the data is guaranteed to have no duplicates and needs to be done in-place?


front: What can you use to merge an unsorted list into an already sorted list?
back:
If the new list is short, insertion sort would be O(n).
If both lists are long, sort the short list and then do a merge.


front: How can you sort a list of Employee objects by surname then given_name, case-sensitive?
back:
sorted_firstnames = sorted(directory, key=lambda emp: emp.given_name.lower())
pprint(sorted(sorted_firstnames, key=lambda emp: emp.surname.lower()))


front: What should you know about the cmp argument in sort/sorted?
back:
In general, the key and reverse conversion processes are much faster than specifying an equivalent cmp function. This is because cmp is called multiple times for each list element while key and reverse touch each element only once. 

The cmp argument was removed in 3.0.

Use functools.cmp_to_key() to convert an old-style cmp function to a key function.


front: How can you get a stable sort using an unstable sorting algorithm?
back:
Add a sequence number to the data elements, like adding a new sequence property to the objects to be sorted.

In your comparator, if the compared keys are equal (difference is 0), break ties by returning e1.sequence - e2.sequence


front: What are green threads?
back:
- implemented by a virtual machine
- faster but cannot take advantage of multiple cores
- going away due to prevalence of multicore machines


front: How can an event-driven handler manage a long-running process, like waiting for a network response?
back:
Spawn a thread to call the network resource. Then queue the result once complete so the parent process (or main event thread) can respond and access the data without polling.


front: What is a monitor?
back:
A monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become true. Monitors also have a mechanism for signalling other threads that their condition has been met. A monitor consists of a mutex (lock) object and condition variables. Monitors provide a mechanism for threads to temporarily give up exclusive access in order to wait for some condition to be met, before regaining exclusive access and resuming their task.


front: How do you solve the Dining Philosophers problem?
back:
- critical section is picking up one fork, second fork, and then eating then putting down forks.
- one philosopher should pick up fork 2 then fork 1. if (i == 0) swap fork 1 and 2
- one will get to eat more, the one to left of 0
- to even it out, just change order in which each picks up: if (i % 2 == 0) swap fork 1 and 2


front: What should you do to make a Singleton thread-safe?
back:
It's best to make getInstance() thread-safe, or make generating the instance thread-safe so getInstance does not suffer from thread-safety overhead each time it's called.


front: What are some differences between C++ and Java?
back:
C++:
- programmer control with low-level access
- backwards compatibility with C
- multiple inheritance
- pass by value

Java:
- security
- portability
- runs in a VM, compiles to bytecode
- JIT speeds it up considerably
- garbage collection
- single inheritance (multiple inheritance simulated by using interfaces)
- pass by reference


front: What is the difference between a pointer and a reference?
back:
- must dereference a pointer: foo->prop
- pointer can be null, a reference cannot
- deal with reference as if it was declared in the same scope: foo.prop


front: Difference between inline functions and macros?
back:
Each reduces overhead of function calls at the expense of program size

Macro:
- text replacement by preprocessor
- single line only
- unexpected side effects if not careful

Inline:
- replaces the call to an inline function with that function's body
- ok for multiple lines
- increases size, cuts down on overhead of repetitive function calls


front: What are the pros and cons of garbage collection?
back:
Good:
- no dangling pointers
- no double allocation
- no memory leaks

Bad:
- can over-allocate memory
- stop and copy can cause pauses at inopportune times
- slower performance
- rapid development
- circular references


front: What is a tracing garbage collector?
back:
Mark and sweep: pauses execution in order to mark all objects referenced by any thread of the program.


front: What is tri-color marking?
back:
Because of these performance problems, most modern tracing (mark and sweep) garbage collectors implement some variant of the tri-color marking abstraction. This avoids the execution pause common to mark and sweep.

Three sets are created – white, black and gray:

- The white set, or condemned set, is the set of objects that are candidates for having their memory recycled.
- The black set is the set of objects that can be shown to have no outgoing references to objects in the white set, and to be reachable from the roots. Objects in the black set are not candidates for collection.
- The gray set contains all objects reachable from the roots but yet to be scanned for references to "white" objects. Since they are known to be reachable from the roots, they cannot be garbage-collected and will end up in the black set after being scanned.


front: What are the difference between 32-bit and 64-bit applications?
back:
32-bit:
- 32-bit memory addresses and registers

64-bit:
- 64-bit memory addressed and registers
- requires 64-bit processor, 64-bit OS
- can run 32-bit programs in compatibility mode
- pointers require 64 bits so they take more space
- fewer items fit in cache
- x86-64 in 64-bit has an additional 8 additional general purpose registers


front: What is memory-mapping?
back:
Memory-mapping a file uses the operating system virtual memory system to access the data on the file system directly, instead of using normal I/O functions. Memory-mapping typically improves I/O performance because it does not involve a separate system call for each access and it does not require copying data between buffers – the memory is accessed directly by both the kernel and the user application.


front: What is bcrypt?
back:
bcrypt is a password hashing function based on the Blowfish cipher. Besides incorporating a salt to protect against rainbow table attacks, bcrypt is an adaptive function: over time, the iteration count can be increased to make it slower, so it remains resistant to brute-force search attacks even with increasing computation power.


front: How can you solve a set cover problem?
back:
- pick largest set that covers most of the uncovered spots
- add it to covering set
- then next largest, even if overlap

O(n^2)
greedy but it works


front: What is a suffix array?
back:
A suffix array is a sorted array of all suffixes of a string.

The suffix array of a string can be used as an index to quickly locate every occurrence of a substring pattern P within the string S. Finding every occurrence of the pattern is equivalent to finding every suffix that begins with the substring. Thanks to the lexicographical ordering, these suffixes will be grouped together in the suffix array and can be found efficiently with two binary searches. The first search locates the starting position of the interval, and the second one determines the end position.


front: Is knapsack problem NP-complete?
back:
The decision problem form of the knapsack problem (Can a value of at least V be achieved without exceeding the weight W?) is NP-complete, thus there is no known algorithm both correct and fast (polynomial-time) on all cases.

There is a pseudo-polynomial time algorithm using dynamic programming.


front: What is a hard problem that is not in NP?
back:
Two-player games such as chess provide examples of problems that are not in NP. 
The only way to check if a move would eventually win the game is to construct the full tree of all your possible moves with his irrefutable replies and demonstrate that you, in fact, cannot win from the current position. This full tree will have a number of nodes exponential in its height, which is the number of moves before you lose playing your most spirited possible defense.

This tree cannot be constructed and analyzed in polynomial time, so the problem is not in NP.


front: Why are Fibonacci heaps special?
back:
Fibonacci heaps support insert and decrease-key operations in constant amortized time, with O(lg n) amortized time extract-min and delete operations.


front: What makes van Emde Boas trees special?
back:
Van Emde Boas priority queues support O(lg lg n) insertion, deletion, search, max, and min operations where each key is an element from 1 to n.


front: Why is an adjacency list for the best choice for a planar graph?
back:
Planar graphs are always sparse, since any n-vertex planar graph can have at most 3n − 6 edges. Thus they should be represented using adjacency lists.


front: Can you use when your graph is extremely large?
back:
If your graph is extremely large, it may become necessary to switch to a hierarcchical representation, where the vertices are clustered into subgraphs that are compressed into single vertices. Two approaches exist to construct such a hierar-chical decomposition. The first breaks the graph into components in a natural or application-specific way. For example, a graph of roads and cities suggests a natural decomposition—partition the map into districts, towns, counties, and states.


front: Is bandwidth minimization NP-Complete?
back:
Yes. It stays NP-complete even if the input graph is a tree whose maximum vertex degree is 3, which is about as strong a condition as I have seen on any problem. Thus our only options are a brute-force search and heuristics.


front: Using an adjacency matrix how can you see how many paths exist between two points of length K?
back:
Matrix multiplication has a particularly interesting interpretation. Now consider the square of this matrix, A^2 = A × A. If A^2[i, j] ≥ 1. This means that there must be a vertex k such that A[i, k] = A[k, j] = 1, so i to k to j is a path of length 2 in G. More generally, A^k[i, j] counts the number of paths of length exactly k between i and j. This count includes nonsimple paths, where vertices are repeated, such as i to k to i to j.


front: What does #P-complete mean?
back:
#P is the class of problems solvable on a “counting” machine in polynomial time. A counting machine returns the number of distinct solutions to a problem. Counting the number of Hamiltonian cycles in a graph is a #P-complete problem that is trivially NP-hard (and presumably harder), since any count greater than zero proves that the graph is Hamiltonian. Counting problems can be #P-complete even if the corresponding decision problem can be solved in polynomial time, as shown by the permanent and perfect matchings.


front: What can be said of integer programming in and NP-completeness?
back:
NP-complete to solve integer or mixed programs to optimality. However, there are integer programming techniques that work reasonably well in practice.


front: How can you convert a linear programming maximization problem into a minimization problem?
back:
To convert a maximization problem to a minimization one, simply multiply each coefficient of the objective function by −1. The remaining problems can be solved by adding slack variables to the model. See any textbook on linear programming for details


front: What is NC?
back:
NC (Nick's class) can be thought of as the problems that can be efficiently solved on a parallel computer. NC is a subset of P because polylogarithmic parallel computations can be simulated by polynomial-time sequential ones.


front: What is P-Complete?
back:
A decision problem is P-complete (complete for the complexity class P) if it is in P and every problem in P can be reduced to it by an appropriate reduction.


front: Why is periodicity important in random rumber generation?
back:
The period defines how long a random number generator will generate numbers before it repeats the sequence. 
The period of a PRNG is defined thus: the maximum, over all starting states, of the length of the repetition-free prefix of the sequence. The period is bounded by the number of the states, usually measured in bits. However, since the length of the period potentially doubles with each bit of "state" added, it is easy to build PRNGs with periods long enough for many practical applications.


front: What are Carmichael numbers?
back:
Carmichael numbers are composite integers that always satisfy Fermat's little theorem. They are pseudo-primes.


front: What is a convolution?
back:
A convolution is a mathematical operation on two functions (f and g); it produces a third function, that is typically viewed as a modified version of one of the original functions.


front: What is in mathematical application of FFT?
back:
FFT-based algorithm that multiplies two n-bit numbers in O(n lg n lg lg n) time is due to Schonhagen and Strassen.


front: How can you sort extremely large amount of data on disk with limited memory?
back:
The simplest approach to external sorting loads the data into a B-tree and then does an in-order traversal of the tree to read the keys off in sorted order. Real high-performance sorting algorithms are based on multiway-mergesort. Files containing portions of the data are sorted into runs using a fast internal sort, and then files with these sorted runs are merged in stages using 2- or k-way merging. Complicated merging patterns and buffer management based on the properties of the external storage device can be used to optimize performance.


front: How can you optimize quicksort?
back:
• Use randomization – randomly permute the keys before sorting.
• Median of three – For your pivot element, use the median of the first, last, and middle elements of the array. 
• Leave small subarrays for insertion sort – Terminating the quicksort recursion and switching to insertion sort makes sense when the subarrays get small, say fewer than 20 elements.
• Do the smaller partition first – Assuming that your compiler is smart enough to remove tail recursion.


front: What is a radix?
back:
The base, or radix, is the value that we raise to successive powers for each digit to the left of the radix point (note that the term decimal point only applies to decimal numbers).


front: What is a weakly connected graph?
back:
A directed graph is weakly connected if it would be connected by ignoring the direction of edges. Thus, a weakly connected graph consists of a single piece. A directed graph is strongly connected if there is a directed path between every pair of vertices. This distinction is best made clear by considering the network of one- and two-way streets in any city. The network is strongly connected if it is possible to drive legally between every two positions. The network is weakly connected when it is possible to drive legally or illegally between every two positions. The network is disconnected if there is no possible way to drive from a to b.


front: How can you tell if a graph is a tree?
back:
Depth-first search can be used to test whether it is connected. If the graph is connected and has n − 1 edges for n vertices, it is a tree.


front: What kind of graph do you have to have in order to perform a topological sort?
back:
Only DAGs can be topologically sorted, since any directed cycle provides an inherent contradiction to a linear order of tasks.


front: What is important about every DAG?
back:
Every DAG can be topologically sorted, so there must always be at least one schedule for any reasonable precedence constraints among jobs.


front: What can you say about the number of possible topological sorts on a graph?
back:
The problem of counting the number of linear extensions (the number of ways to order a topological sort) is NP-hard.


front: In removing edges in order to make a graph acyclic, is this a hard problem?
back:
Removing the minimum number of edges in order to make a graph acyclic so that you can topological sort is NP complete.


front: Which is better: Prim's algorithm or Kruskal's algorithm?
back:
Prim's algorithm runs in O(n^2), while Kruskal's algorithm takes O(m log m) time. Thus Prim's algorithm is faster on dense graphs, while Kruskal's is faster on sparse graphs.


front: How can you find shortest path in an undirected graph?
back:
If your graph is unweighted, a simple breadth-first search starting from the source vertex will find the shortest path to all other vertices in linear time.


front: How can you find shortest paths on a DAG?
back:
Shortest paths in directed acyclic graphs can be found in linear time. Perform a topological sort to order the vertices such that all edges go from left to right starting from source s.


front: How can you find the shortest simple cycle?
back:
To find the shortest simple cycle, the easiest approach is to compute the lengths of the shortest paths from i to all other vertices, and then explicitly check whether there is an acceptable edge from each vertex back to i.


front: How can you find the longest cycle in a graph?
back:
Finding the longest cycle in a graph includes Hamiltonian cycle as a special case, so it is NP-complete.


front: How does preprocessing affect A* times?
back:
Goldberg, Kaplan, and Werneck describe an implementation of A∗ capable of answering point-to-point queries in one millisecond on national-scale road networks after two hours of preprocessing.


front: What is transitive closure and how is it recorded?
back:
Transitive closure can be thought of as establishing a data structure that makes it possible to solve reachability questions (can I get to x from y?) efficiently. After constructing the transitive closure, all reachability queries can be answered in constant time by simply reporting the appropriate matrix entry.


front: What is transitive reduction?
back:
Transitive reduction (also known as minimum equivalent digraph) is the inverse operation of transitive closure, namely reducing the number of edges while maintaining identical reachability properties. The transitive closure of G is identical to the transitive closure of the transitive reduction of G.


front: How can you tell if an undirected graph contains a Eulerian cycle?
back:
An undirected graph contains an Eulerian cycle iff 
(1) it is connected, and 
(2) each vertex is of even degree.


front: How can you tell if an undirected graph contains an Eulerian path?
back:
An undirected graph contains an Eulerian path iff 
(1) it is connected, and 
(2) all but two vertices are of even degree. These two vertices will be the start and end points of any path.


front: How can you determine if a directed graph contains an Eulerian cycle?
back:
A directed graph contains an Eulerian cycle iff (1) it is strongly-connected, and (2) each vertex has the same in-degree as out-degree.


front: How can you tell if a directed graph contains a Eulerian path?
back:
A directed graph contains an Eulerian path from x to y iff 
(1) it is connected, and 
(2) all other vertices have the same in-degree as out-degree, with x and y being vertices with in-degree one less and one more than their out-degrees, respectively.


front: What is the importance of minimum vertex degree?
back:
The minimum vertex degree is an upper bound for both edge and vertex connectivity, since deleting all its neighbors (or cutting the edges to all its neighbors) disconnects the graph into one big and one single-vertex component.


front: What does a biconnected graph mean?
back:
We say that G is biconnected if no single vertex deletion is sufficient to disconnect G. Any vertex that is such a weak point is called an articulation vertex. A bridge is the analogous concept for edges, meaning a single edge whose deletion disconnects the graph.


front: What is the relation of maximum flow and minimum cut?
back:
The maximum flow between vi, vj in G is exactly the weight of the smallest set of edges to disconnect vi from vj.


front: What is one heuristic to draw "nice" graphs for a given graph?
back:
As a first quick and dirty drawing, I recommend simply spacing the vertices evenly on a circle, and then drawing the edges as straight lines between vertices. Such drawings are easy to program and fast to construct. They have the substantial advantage that no two edges will obscure each other, since no three vertices will be collinear. Such artifacts can be hard to avoid as soon as you allow internal vertices into your drawing. An unexpected pleasure with circular drawings is the symmetry sometimes revealed because vertices appear in the order they were inserted into the graph. Simulated annealing can be used to permute the circular vertex order to minimize crossings or edge length, and thus significantly improve the drawing. A good, general purpose graph-drawing heuristic models the graph as a system of springs and then uses energy minimization to space the vertices. Let adjacent vertices attract each other with a force proportional to (say) the logarithm of their separation, while all nonadjacent vertices repel each other with a force proportional to their separation distance. These weights provide incentive for all edges to be as short as possible, while spreading the vertices apart. The behavior of such a system can be approximated by determining the force acting on each vertex at a particular time and then moving each vertex a small amount in the appropriate direction. After several such iterations, the system should stabilize on a reasonable drawing.


front: What is branch and bound?
back:
It usually of the form of a decision tree where each decision is represented by an edge. The leaves of this tree is the set of all possible solutions. To find the most optimized solution(i.e. one of the leaves) the following is the basis of evaluation.
The intuition is that you start from the root and calculate an upper bound if the problem asks the function to be maximized (or lower bound for function minimization in case of TSP where distance is to be minimized) for a function you want to optimize.


front: What is a heuristic for solving max independent set?
back:
The simplest reasonable heuristic is to find the lowest-degree vertex, add it to the independent set, and then delete it and all vertices adjacent to it. Repeating this process until the graph is empty gives a maximal independent set, in that it can't be made larger by just adding vertices.


front: What is a simple heuristic for vertex cover?
back:
The simplest heuristic for vertex cover selects the vertex with highest degree, adds it to the cover, deletes all adjacent edges, and then repeats until the graph is empty. With the right data structures, this can be done in linear time, and should “usually” produce a “pretty good” cover. However, this cover might be lg n times worse than the optimal cover for certain input graphs.


front: What is a heuristic for vertex cover?
back:
A maximal matching can be constructed incrementally, by picking an arbitrary edge e in the graph, deleting any edge sharing a vertex with e, and repeating until the graph is out of edges. Taking both of the vertices for each edge in a maximal matching gives us a vertex cover.


front: What is a special case of TSP on an unweighted graph?
back:
If the graph is unweighted, or all the edges have one of two possible cost values, the problem reduces to finding a Hamiltonian cycle.


front: What is a heuristic for TSP?
back:
The heuristic starts by finding the minimum spanning tree (MST) of the sites, and then does a depth-first search of the resulting tree. In the course of DFS, we walk over each of the n − 1 edges exactly twice: once going down to discover a new vertex, and once going up when we backtrack. Now define a tour by ordering the vertices by when they were discovered. If the graph obeys the triangle inequality, the resulting tour is at most twice the length of the optimal TSP tour. In practice, it is usually better, typically 15% to 20% over optimal. Furthermore, the running time is bounded by that of computing the MST, which is only O(n lg n) in the case of points in the plane.


front: What is HyperLogLog?
back:
HyperLogLog is an algorithm for the count-distinct problem, approximating the number of distinct elements in a multiset.

The basis of the HyperLogLog algorithm is the observation that the cardinality of a multiset of uniformly distributed random numbers can be estimated by calculating the maximum number of leading zeros in the binary representation of each number in the set. If the maximum number of leading zeros observed is n, an estimate for the number of distinct elements in the set is 2^n.

In the HyperLogLog algorithm, a hash function is applied to each element in the original multiset, to obtain a multiset of uniformly distributed random numbers with the same cardinality as the original multiset. The cardinality of this randomly distributed set can then be estimated using the algorithm above.


front: What is graph partitioning?
back:
Partition the vertices into m roughly equal-sized subsets such that the total edge cost spanning the subsets is at most k.


front: Is graph partition NP-complete?
back:
A better partition criterion seeks a small cut that partitions the vertices into roughly equal-sized pieces. Unfortunately, this problem is NP-complete. Fortunately, the heuristics discussed below work well in practice.


front: What is the difficulty difference between 3-coloring and 4-coloring a graph?
back:
The famous four-color theorem states that every planar graph can be vertex colored using at most four distinct colors. Efficient algorithms for finding a four-coloring on planar graphs are known, although it is NP-complete to decide whether a given planar graph is three-colorable.


front: Is determining the chromatic number of a graph NP-Complete?
back:
Computing the chromatic number of a graph is NP-complete, so if you need an exact solution you must resort to backtracking, which can be surprisingly effective in coloring certain random graphs.


front: What is Brook's theorem?
back:
Brook's theorem states that the chromatic number χ(G) ≤ Δ(G) + 1, where Δ(G) is the maximum degree of a vertex of G. Equality holds only for odd-length cycles (which have chromatic number 3) and complete graphs.


front: What is the edge-chromatic number?
back:
The minimum number of colors needed to edge color a graph is called its edge-chromatic number by some and its chromatic index by others. Note that an even-length cycle can be edge-colored with 2 colors, while odd-length cycles have an edge-chromatic number of 3.


front: What is Vizing's theorem?
back:
Vizing's theorem states that any graph with a maximum vertex degree of Δ can be edge colored using at most Δ + 1 colors.


front: What can you say about the difficulty/complexity of graph isomorphism?
back:
No polynomial-time algorithm is known for graph isomorphism, but neither is it known to be NP-complete. Along with integer factorization, it is one of the few important algorithmic problems whose rough computational complexity is still not known. The conventional wisdom is that isomorphism is a problem that lies between P and NP-complete if P <> NP.


front: How can adjacency matrices be used to compute paths?
back:
Taking the adjacency matrix of G and raising it to the kth power gives a matrix where Gk[i, j] counts the number of (non-simple) paths from i to j.


front: What is the difficulty of minimum Steiner tree?
back:
The general minimum Steiner tree problem is NP-hard, and remains so under a broad range of restrictions.


front: What is a heuristic for finding Steiner trees?
back:
Construct a graph modeling your input, setting the weight of edge (i, j) equal to the distance from point i to point j. Find an MST of this graph. You are guaranteed a provably good approximation for both Euclidean and rectilinear Steiner trees.


front: Is Euclidean Steiner tree in NP?
back:
Euclidean Steiner tree is not known to be in NP, because of numerical issues in representing distances.


front: How can I find a good feedback edge set?
back:
An effective linear-time heuristic constructs a vertex ordering (topological sort) and then deletes any arc going in the wrong direction. At least half the arcs must go either left-to-right or right-to-left for any vertex order, so take the smaller partition as your feedback set.


front: What can be said of the relationship between convex hull and graph diameter?
back:
The diameter of a set of points is the pair of points that lie a maximum distance apart. 
The diameter must be between two points on the convex hull.


front: Triangulation - how can you partition the interior of the point set or polyhedron into triangles?
back:
A triangulation in the plane is constructed by adding nonintersecting chords between the vertices until no more such chords can be added. The simplest such O(n lg n) algorithm first sorts the points by x-coordinate. It then inserts them from left to right as per the convex-hull algorithm, building the triangulation by adding a chord to each point newly cut off from the hull.


front: What is a Voronoi diagram?
back:
Voronoi diagrams represent the region of influence around each of a given set of sites. If these sites represent the locations of McDonald's restaurants, the Voronoi diagram partitions space into cells around each restaurant. For each person living in a particular cell, the defining McDonald's represents the closest place to get a Big Mac.


front: How can you find the nearest neighbor of query point q from among a fixed set of points?
back:
Finding the nearest neighbor of query point q from among a fixed set of points S is simply a matter of determining the cell in the Voronoi diagram of S that contains q.
Or use a kd-tree to divide and conquer the points.


front:
Suppose McDonald's wants to open another restaurant. To minimize interference with existing McDonald's, it should be located as far away from the closest restaurant as possible.  How can you find it?

back:
This location is always at a vertex of the Voronoi diagram, and can be found in a linear-time search through all the Voronoi vertices.


front: What is a Delaunay triangulation?
back:
The Delaunay triangulation maximizes the minimum angle over all triangulations (avoids skinny triangles). Furthermore, it is easily constructed as the dual of the Voronoi diagram.


front: What code can you use for 2D Voronoi diagrams and Delaunay triangulations?
back:
Fortune's Sweep2 is a widely used 2D code for Voronoi diagrams and Delaunay triangulations, written in C. This code is simple to work with if all you need is the Voronoi diagram.


front: What is a popular library for convex hull as well as other 2D geometric algorithms?
back:
Qhull is a popular low-dimensional convex-hull code, useful for from two to about eight dimensions. It is written in C and can also construct Delaunay triangulations, Voronoi vertices, furthest-site Voronoi vertices, and half-space intersections.


front: What is the problem in using a high-dimension kd-tree for finding nearest neighbors?
back:
Searches in high-dimensional spaces become hard because a sphere of radius r, representing all the points with distance ≤ r from the center, progressively fills up less volume relative to a cube as the dimensionality increases. Thus, any data structure based on partitioning points into enclosing volumes will become progressively less effective.


front: What is a library for approximate nearest neighbor searching in arbitrarily high dimensions?
back:
ANN is a C++ library for both exact and approximate nearest neighbor searching in arbitrarily high dimensions. It performs well for searches over hundreds of thousands of points in up to about 20 dimensions. It supports all lp distance norms, including Euclidean and Manhattan distance.


front:
What is a tool for visualizing and experimenting with nearest-neighbor and orthogonal-range queries in high-dimensional data sets?

back:
Ranger is a tool for visualizing and experimenting with nearest-neighbor and orthogonal-range queries in high-dimensional data sets, using multidimensional search trees. Four different search data structures are supported by Ranger: naive kd-trees, median kd-trees, nonorthogonal kd-trees, and the vantage point tree.


front: What turns out to be the only viable solution for high-dimensional range search?
back:
kd-trees are likely to work just fine in the plane. In higher dimensions, they provide the only viable solution to the problem.


front: What is the best heuristic for bin packing?
back:
Analytical and empirical results suggest that first-fit decreasing is the best heuristic. Sort the objects in decreasing order of size, so that the biggest object is first and the smallest last. Insert each object one by one into the first bin that has room for it. If no bin has room, we must start another bin. In the case of one-dimensional bin packing, this can never require more than 22% more bins than necessary and usually does much better. First-fit decreasing has an intuitive appeal to it, for we pack the bulky objects first and hope that little objects can fill up the cracks.
First-fit decreasing is easily implemented in O(n lg n + bn) time, where b ≤ min(n, m) is the number of bins actually used. Simply do a linear sweep through the bins on each insertion. A faster O(n lg n) implementation is possible by using a binary tree to keep track of the space remaining in each bin.


front: What is medial-axis transformation?
back:
The medial-axis transformation is useful in thinning a polygon, or as is sometimes said, finding its skeleton. The goal is to extract a simple, robust representation of the shape of the polygon.

The medial-axis transform of a polygon P is simply the portion of the line-segment Voronoi diagram that lies within P. Any line-segment Voronoi diagram code thus suffices to do polygon thinning.


front: What is triangulation?
back:
Triangulation is the mother of all polygon partitioning problems, where we partition the interior of the polygon completely into triangles. Triangles are convex and have only three sides, making them the most elementary possible polygon.


front: What is the Hertel-Mehlhorn heuristic for convex decomposition?
back:
The Hertel-Mehlhorn heuristic for convex decomposition using diagonals is simple and efficient. It starts with an arbitrary triangulation of the polygon and then deletes any chord that leaves only convex pieces. A chord deletion creates a non-convex piece only if it creates an internal angle that is greater than 180 degrees.


front: What is a heuristic for shape similarity of graphs?
back:
A more powerful approach to shape similarity uses thinning to extract a tree-like skeleton for each object. This skeleton captures many aspects of the original shape. The problem now reduces to comparing the shape of two such skeletons, using such features as the topology of the tree and the lengths/slopes of the edges. This comparison can be modeled as a form of subgraph isomorphism, with edges allowed to match whenever their lengths and slopes are sufficiently similar.


front: What is a Minkowski sum and what is it used for?
back:
Minkowski sums are useful geometric operations that can fatten objects in appropriate ways. For example, a popular approach to motion planning for polygonal robots in a room with polygonal obstacles fattens each of the obstacles by taking the Minkowski sum of them with the shape of the robot. This reduces the problem to the (more easily solved) case of point robots.


front: What is the difficulty of vertex cover and what is the limit of current approximation algorithms?
back:
Set cover must be at least as hard as vertex cover, so it is also NP-complete. In fact, it is somewhat harder. Approximation algorithms do no worse than twice optimal for vertex cover, but the best approximation algorithm for set cover is Θ(lg n) times optimal.


front: What is an effective heuristic of set cover?
back:
Greedy is the most natural and effective heuristic for set cover. Begin by selecting the largest subset for the cover, and then delete all its elements from the universal set. We add the subset containing the largest number of remaining un-covered elements repeatedly until all are covered.


front: What is a heuristic of set packing?
back:
The right heuristics for set packing are greedy, and similar to those of set cover. If we seek a packing with many (few) sets, then we repeatedly select the smallest (largest) subset, delete all subsets from S that clash with it, and repeat. As usual, augmenting this approach with some exhaustive search or randomization (in the form of simulated annealing) is likely to yield better packings at the cost of additional computation.


front: What does the Boyer-Moore algorithm do that saves time?
back:
The Boyer-Moore algorithm matches the pattern against the text from right to left, and can avoid looking at large chunks of text on a mismatch. Suppose the pattern is abracadabra, and the eleventh character of the text is x. This pattern cannot match in any of the first eleven starting positions of the text, and so the next necessary position to test is the 22nd character. If we get very lucky, only n/m characters need ever be tested. The Boyer-Moore algorithm involves two sets of jump tables in the case of a mismatch: one based on pattern matched so far, the other on the text character seen in the mismatch.


front: What is the performance and caveats of Boyer-Moore?
back:
It is worth it in practice for patterns of length m > 5, unless the pattern is expected to occur many times in the text. Its worst-case performance is O(n + rm), where r is the number of occurrences of p in t.


front: What is an efficient use of suffix trees/arrays?
back:
Suppose you are building a program to repeatedly search a particular text database, such as the Bible. Since the text remains fixed, it pays to build a data structure to speed up search queries. The suffix tree and suffix array data structures are the right tools for the job.


front: What is a better method for searching many texts using the same patterns?
back:
Suppose you are building a program to screen out dirty words from a text stream. Here, the set of patterns remains stable, while the search texts are free to change. In such applications, we may need to find all occurrences of any of k different patterns where k can be quite large.

Performing a linear-time scan for each pattern yields an O(k(m + n)) algorithm. If k is large, a better solution builds a single finite automaton that recognizes all of these patterns and returns to the appropriate start state on any character mismatch. The Aho-Corasick algorithm builds such an automaton in linear time. Space savings can be achieved by optimizing the pattern recognition automaton. This approach was used in the original version of fgrep.


front: What can you say about the performance of Rabin-Karp?
back:
The Rabin-Karp algorithm uses a hash function to perform string matching in linear expected time. Its worst-case time remains quadratic, and its performance in practice appears somewhat worse than other character comparison methods.


front: What can you use to take advantage of large word sizes (in registers) to do string matching?
back:
A recent approach to string-matching exploits the fact that modern computers can do operations on (say) 64-bit words in a single gulp. This is long enough to hold eight 8-bit ASCII characters, providing motivation to design bit-parallel algorithms, which do more than one comparison with each operation.

The basic idea is quite clever. Construct a bit-mask Bα for each letter α of the alphabet, such that ith-bit Bα[i] = 1 iff the ith character of the pattern is α. Now suppose you have a match bit-vector Mj for position j in the text string, such that Mj[i] = 1 iff the first i bits of the pattern exactly match the (j − i + 1)st through jth character of the text. We can find all the bits of Mj+1 using just two operations by (1) shifting Mj one bit to the right, and then (2) doing a bitwise AND with Bα, where α is the character in position.


front: How does the Burrows-Wheeler transform work and what is its performance?
back:
A particularly interesting simplification results from applying the Burrows-Wheeler transform to the input string. This transform sorts all n cyclic shifts of the n character input, and then reports the last character of each shift. As an example, the cyclic shifts of ABAB are ABAB, BABA, ABAB, and BABA. After sorting, these become ABAB, ABAB, BABA, and BABA. Reading the last character of each of these strings yields the transform result: BBAA.

Provided the last character of the input string is unique (e.g., end-of-string), this transform is perfectly reversible to the original input! The Burrows-Wheeler string is typically 10-15% more compressible than the original text, because repeated words turn into blocks of repeated characters. Further, this transform can be computed in linear time.


front: What are Huffman codes?
back:
Huffman codes replace each alphabet symbol by a variable-length code string. Using eight bits-per-symbol to encode English text is wasteful, since certain characters (such as “e”) occur far more frequently than others (such as “q”). Huffman codes assign “e” a short code word, and “q” a longer one to compress text.


front: How do Lempel-Ziv algorithms work?
back:
Lempel-Ziv algorithms (including the popular LZW variant) compress text by building a coding table on the fly as we read the document. The coding table changes at every position in the text. A clever protocol ensures that the encoder and decoder are both always working with the exact same code table, so no information is lost.


front: What can you say about the tradeoffs in compression algorithms?
back:
There is a natural tradeoff between compression ratio and compression time. Another choice is bzip2, which uses the Burrows-Wheeler transform. It produces tighter encodings than gzip at somewhat greater cost in running time. Going to the extreme, other compression algorithms devote enormous run times to squeeze every bit out of a file.


front: How can finite automata be represented in a directed graph?
back:
Finite state machines are defined by directed graphs. Each vertex represents a state, and each character-labeled edge defines a transition from one state to another on receipt of the given alphabet symbol. Such automata can be represented using any graph data structure.


front: What is a caveat and hardness of converting non-deterministic to deterministic finite automata?
back:
Converting an NFA to a DFA might cause an exponential blowup in the number of states, which perversely might then be eliminated when minimizing the DFA. This exponential blowup makes most NFA minimization problems PSPACE-hard, which is even worse than NP-complete.


front: How can you compute the longest common substring of a set of strings in linear time?
back:
The longest common substring of a set of strings can be identified in linear time using suffix trees. The trick is to build a suffix tree containing all the strings, label each leaf with the input string it represents, and then do a depth-first traversal to identify the deepest node with descendants from each input string.


front: How can you convert edit distance into common scattered subsequence?
back:
This algorithm is a special case of the dynamic program edit-distance computation.

Let M[i, j] denote the number of characters in the longest common substring of S[1], . . . , S[i] and T [1], . . . , T [j]. When S[i] <> T[j], there is no way the last pair of characters could match, so M[i, j] = max(M[i, j − 1], M[i − 1, j]). But if S[i] = T[j], we have the option to select this character for our substring, so M[i, j] = max(M[i − 1, j − 1] + 1, M[i − 1, j], M[i, j − 1]).

This recurrence computes the length of the longest common subsequence in O(nm) time. We can reconstruct the actual common substring by walking backward from M[n, m] and establishing which characters were matched along the way.


front: What is the standard approach to approximating shortest common superstring?
back:
The greedy heuristic provides the standard approach to approximating shortest common superstring. Identify which pair of strings have the maximum overlap. Replace them by the merged string, and repeat until only one string remains. This heuristic can actually be implemented in linear time. The seemingly most time-consuming part is in building the overlap graph. The brute-force approach to finding the maximum overlap of two length-L strings takes O(L2) for each of O(n^2) string pairs. However, faster times are possible by using suffix trees. Build a tree containing all suffixes of all strings of S. String Si overlaps with Sj iff a suffix of Si matches the prefix of Sj — an event defined by a vertex of the suffix tree. Traversing these vertices in order of distance from the root defines the appropriate merging order.


front: What is the smallest addressable data item on many CPUs?
back:
A byte is eight bits and it is the smallest addressable data item on many CPUs. That is, the CPU can efficiently retrieve data on an 8-bit boundary from memory.


front: What happens when you negate the smallest negative value in the two's complement numbering system?
back:
It becomes itself.
In general, you cannot negate the smallest negative value in the two's complement numbering system.


front:
Why should you be careful about mixing variables of different sizes within the same arithmetic expression or assignment statement?

back:
The important thing to realize about sign and zero extension is that they aren't always free. Assigning a smaller integer to a larger integer may require more machine instructions (taking longer to execute) than moving data between two like-sized integer variables.


front: What conditions do you have to have in order to sign contract a numeric value?
back:
First, the HO bytes must all contain either zero or 0xFF. If you encounter any other values, you cannot sign contract the value. Second, the HO bit of your resulting value must match every bit you've removed from the number.


front: What is saturation?
back:
Saturation is another way to reduce the size of an integer value. Saturation is useful when you want to convert a larger object to a smaller object and you're willing to live with possible loss of precision.


front: On the Intel 80x86 processor family, what do the MMX instruction extensions provide?
back:
The MMX instruction extensions provide saturation capabilities. Most CPUs' standard instruction sets, as well as most high-level languages, do not provide direct support for saturation, but saturation is not difficult.


front: What is BCD?
back:
The binary-coded decimal (BCD) format, as its name suggests, encodes decimal values (0-9) using a binary representation.


front: What are BCD values?
back:
BCD values consist of a sequence of nibbles, with each nibble representing a value in the range 0..9.


front: What is possible with 16-bit integer, 16-bit fractional format?
back:
Certain 3D gaming applications may produce faster computations using a 16:16 (16-bit integer, 16-bit fractional) format rather than a 32-bit floating-point format.


front: What is an advantage of binary decimal format over 2 digit decimal format?
back:
Given the accuracy most financial computations require (generally four digits to the right of the decimal point is the minimum precision serious financial transactions require), it's usually better to use a binary format.


front: What is an advantage of scaled numeric format?
back:
One advantage of the scaled numeric format is that you can choose any base, not just decimal, for your format.


front: What is rational representation?
back:
Rational representation uses pairs of integers to represent fractional values. One integer represents the numerator (n) of a fraction, and the other represents the denominator (d). The actual value is equal to n/d.


front: How can you test if a bit is set?
back:
If you logically AND a value with a bit string that contains a one in a certain bit position, the result of the logical AND will be zero if the corresponding bit contains a zero, and the result will be nonzero if the bit is set.


front: What is one way to minimize floating point error accumulation?
back:
If you are performing a chain calculation involving addition and subtraction, you should attempt to group the operations so that you can add or subtract values whose magnitudes are close to one another before adding or subtracting values whose magnitudes are not as close.


front: What is a caveat for floating-point addition and subtraction?
back:
Whenever subtracting two numbers with the same signs or adding two numbers with different signs, the accuracy of the result may be less than the precision available in the floating-point format.


front:
What can you do to avoid floating-point error accumulation issues involving addition, subtraction, multiplication, and division?

back:
When performing a chain of calculations involving addition, subtraction, multiplication, and division, try to perform the multiplication and division operations first.


front: What can you do to avoid floating point error issues when you multiply and divide numbers?
back:
When multiplying and dividing sets of numbers, try to multiply and divide numbers that have the same relative magnitudes.


front: What can you do to compare 2 floating-point numbers?
back:
If you determine that the values are not equal to one another within the desired error tolerance, then you can compare them to see if one value is less than or greater than the other. This is known as a miserly approach to comparing for less than or greater than (that is, we try to find as few values that are less than or greater than as possible).


front: What is the eager approach to floating-point comparison?
back:
An eager approach attempts to make the result of the comparison true as often as possible. Given two values that you want to compare, and an error tolerance you’re interested in achieving, here’s how you’d eagerly compare the two values for less than or greater than:

if( A < (B + error)) then Eager_A_lessthan_B; 
if( A > (B − error)) then Eager_A_greaterthan_B;


front: What is special about extended precision?
back:
Extended precision contains 16 extra bits that the calculations can use as guard bits before rounding down to a double-precision value when storing the result.


front: What is the format of single-precision?
back:
The single-precision format uses a 24-bit mantissa and an 8-bit exponent. The mantissa usually represents a value between 1.0 and just less than 2.0. The mantissa usually represents a value between 1.0 and just less than 2.0. The HO bit of the mantissa is always assumed to be one and represents a value just to the left of the binary point. The remaining 23 mantissa bits appear to the right of the binary point and represent the value:
1.mmmmmmm mmmmmmmm mmmmmmmm


front: What is the double-precision format?
back:
The double-precision format helps overcome the problems of the single-precision floating-point. Using twice the space, the double-precision format has an 11-bit excess-1,023 exponent and a 53-bit mantissa (including an implied HO bit of one) plus a sign bit. This provides a dynamic range of about 10±308 and 14 1/2 digits of precision, which is sufficient for most applications.


front: What is the extended-precision format?
back:
The extended-precision format provides a 64-bit mantissa, a 15-bit excess-16,383 exponent, and a 1-bit sign.


front: How does Intel treat extended-precision?
back:
Intel always works in extended-precision format. It converts to extended precision before doing computations. By doing so, Intel guarantees a large number of guard bits are present to ensure the accuracy of your computations. By performing all computations using 80 bits, Intel helps ensure (but not guarantee) that you will get full 32- or 64-bit accuracy in your computations.


front: How do you normalize a floating-point value?
back:
Almost any unnormalized value can be normalized by shifting the mantissa bits to the left and decrementing the exponent until a one appears in the HO bit of the mantissa.


front: How does rounding-up affect a floating-point value?
back:
Rounding up leaves the value alone if the guard bits are all zero, but if the current mantissa does not exactly fit into the destination bits, then rounding up sets the result to the smallest possible larger value in the floating-point format.


front: How does rounding down affect a floating-point value and how is it different from truncation?
back:
Rounding down is just like rounding up, except it rounds the result to the largest possible smaller value. This may sound like truncation, but there is a subtle difference between truncation and rounding down. Truncation always rounds towards zero. For positive numbers, truncation and rounding down do the same thing. However, for negative numbers, truncation simply uses the existing bits in the mantissa, whereas rounding down will actually add a one bit to the LO position if the result was negative.


front: How can you represent NaN in floating point?
back:
If the exponent contains all ones and the mantissa is nonzero (discounting the implied bit), then the HO bit of the mantissa (again discounting the implied bit) determines whether the value represents a quiet not-a-number (QNaN) or a signaling not-a-number (SNaN) (see Table 4-1). These not-a-number (NaN) results tell the system that some serious miscalculation has taken place and that the result of the calculation is completely undefined.


front: How can you represent infinity in floating-point?
back:
Two other special values are represented when the exponent contains all one bits, and the mantissa contains all zeros. In such a case, the sign bit determines whether the result is the representation for +infinity or −infinity.


front: How can you convert an ASCII character between uppercase and lowercase?
back:
You can use this fact to quickly convert an alphabetic character between upper- and lowercase by simply inverting bit five. If you have an uppercase character, you can force it to lowercase by setting bit five to one. If you have a lowercase character and you wish to force it to uppercase, you can do so by setting bit five to zero.


front: How can you obtain the binary representation of an ASCII numeric value?
back:
By stripping away (setting to zero) the HO nibble of the ASCII code, you obtain the binary representation of that digit. Conversely, you can convert a binary value in the range 0..9 to its ASCII character representation by simply setting the HO nibble to %0011, or the decimal value 3.


front: What are length-prefixed strings?
back:
Length-prefixed strings overcome some of the problems with zero-terminated strings. Length-prefixed strings are common in languages like Pascal; they generally consist of a single byte that specifies the length of the string, followed by zero or more 8-bit character codes. In a length-prefixed scheme, the string “abc” would consist of four bytes: the length byte (0x03), followed by a, b, and c.


front: What are static strings?
back:
Arrays of characters that you will use to hold zero-terminated strings in C/C++. While the program is running, there is no way to increase the maximum sizes of these static strings. Nor is there any way to reduce the storage they will use.


front: What are pseudo-dynamic strings?
back:
Pseudo-dynamic strings are those whose length the system sets at run time by calling a memory-management function like malloc to allocate storage for the string. However, once the system allocates storage for the string, the maximum length of the string is fixed.


front: What are dynamic strings?
back:
Dynamic string systems, which typically use a descriptor-based format, will automatically allocate sufficient storage for a string object whenever you create a new string or otherwise do something that affects an existing string. This is called copy-on-write?


front: Is factoring numbers in NP?
back:
The prime factorization problem is in the NP class, but we don't know if it is NP-hard. In other words, there is currently no proof that prime factorization problem cannot be solved polynomial time (in P). Subsequently there is no proof that algorithms that rely on prime factorization being hard are rendered ineffective.


back: Computing the crossing number of a graph is NP-Complete.
front:
For non-planar graphs, it is useful to minimize the number of crossing edges to represent the graph in a flat manner. What is a problem with this approach?


front: Is set cover NP-Complete?
back:
The decision version of set covering is NP-complete, and the optimization/search version of set cover is NP-hard.


front: Is set packing NP-Complete?
back:
Yes. It's not only NP-complete, but its optimization version (general maximum set packing problem) has been proven as difficult to approximate as the maximum clique problem; in particular, it cannot be approximated within any constant factor.


front: What is copy on write with strings?
back:
Whenever a string function needs to change some characters in a dynamic string, the function first makes a copy of the string and then makes whatever modifications are necessary to the copy of the data.


front: What are the three components of a von Neumann system?
back:
A typical von Neumann system has three major components: the central processing unit (CPU), memory, and input/output (I/O)


front: What is the data bus?
back:
CPUs use the data bus to shuffle data between the various components in a computer system. The size of this bus varies widely among CPUs. Indeed, bus size is one of the main attributes that defines the “size” of the processor.


front: What is the size of a modern data bus?
back:
For the most part, the CPUs in personal computers tend to use 32-bit or 64-bit data buses (and 64-bit data buses are the most prevalent).


front: What is the address bus and how does it work?
back:
When the software wants to access a particular memory location or I/O device, it places the corresponding address on the address bus. Circuitry within the device checks this address and transfers data if there is an address match.


front: How much memory can be addresses with n address lines?
back:
With n address lines, the processor can access 2^n unique addresses (because there are 2^n unique values in an n-bit binary number). Therefore, the number of bits on the address bus will determine the maximum number of addressable memory and I/O locations. A 64-bit address range is truly infinite as far as memory is concerned. No one will ever put 2^64 bytes of memory into a computer system and feel that they need more. It's more than atoms in the universe.


front: What is the control bus?
back:
The control bus is an eclectic collection of signals that control how the processor communicates with the rest of the system. The system uses two lines on the control bus, read and write, to determine the data flow direction (CPU to memory, or memory to CPU). So when the CPU wants to write data to memory, it asserts (places a signal on) the write control line. When the CPU wants to read data from memory, it asserts the read control line.


front: What are the byte enable lines?
back:
The byte enable lines appear on the control bus of some CPUs that support byte-addressable memory. These control lines allow 16-, 32-, and 64-bit processors to deal with smaller chunks of data by communicating the size of the accompanying data.


front: What are the 2 address spaces supported by the one address bus?
back:
The 80x86 family, unlike many other processors, provides two distinct address spaces: one for memory and one for I/O.However, it does not have two separate physical address buses (for I/O and memory). Instead, the system shares the address bus for both I/O and memory addresses. Additional control lines decide whether the address is intended for memory or I/O.


front: What does the CPU to save data like this: Memory[125] = 0
back:
To execute the equivalent of the statement Memory[125] = 0; the CPU places the value zero on the data bus, the address 125 on the address bus, and asserts the write line on the control bus.


front: How does the 80x86 family store a data word?
back:
The 80x86 family stores the LO byte of a word at the address specified and the HO byte at the next location. Therefore, a word consumes two consecutive memory addresses (as you would expect, because a word consists of two bytes). Similarly, a double word consumes four consecutive memory locations.


front: What is a byte-addressable memory array?
back:
The term byte-addressable memory array means that the CPU can address memory in chunks as small as a single byte. It also means that this is the smallest unit of memory you can access at once with the processor.


front: What is the address alignment on a 32-bit memory interface?
back:
With a 32-bit memory interface, the address placed on the address bus is always some multiple of four.


front: How can a 32-bit CPU can access a double word in a single memory operation?
back:
A 32-bit CPU can access a double word in a single memory operation only if the address of that value is evenly divisible by four.


front: How do modern Intel processors deal with non-aligned data?
back:
The Pentium and later processors provide a 64-bit data bus and special cache memory that reduces the impact of non-aligned data access. Although there may still be a penalty for accessing data at an inappropriate address, modern x86 CPUs suffer from the problem less frequently than the earlier CPUs.


front: What is the endianness of TCP/IP and some other network protocols?
back:
When transmitting data across networks, the canonical form is usually big endian because TCP/IP and some other network protocols use the big endian format.


front: What are the 4 steps to ensure n log n performance on quick sort?
back:
- randomize input first
- use median of three to determine pivot
- at fewer than 20 elements, end recursion early and do insertion sort
- do the smaller partition first


front: How can you convert from one endianness to another?
back:
To convert between the endian forms, you must do a mirror-image swap of the bytes in the object. To cause a mirror-image swap, you must swap the bytes at opposite ends of the binary number, and then work your way towards the middle of the object swapping pairs of bytes as you go along.


front: What is the system clock?
back:
The system clock is an electrical signal on the control bus that alternates between zero and one at a periodic rate. All activity within the CPU is synchronized with the edges (rising or falling) of this clock signal.


front: What is the system clock frequency and system clock period?
back:
The frequency with which the system clock alternates between zero and one is the system clock frequency and the time it takes for the system clock to switch from zero to one and back to zero is the clock period. One full period is also called a clock cycle.


front: What is the clock period for a 1 GHz processor?
back:
A CPU running at 1 GHz would have a clock period of one nanosecond (ns), or one billionth of a second.


front: How does the clock limit a CPU?
back:
Because all CPU operations are synchronized with the clock, the CPU cannot perform tasks any faster than the clock runs. However, just because a CPU is running at some clock frequency doesn't mean that it executes that many operations each second.


front: What is the memory access time?
back:
The memory access time is the number of clock cycles between a memory request (read or write) and when the memory operation completes. This is an important value, because longer memory access times result in lower performance.


front: Why does the CPU use wait states for memory accesses?
back:
The CPU doesn't wait for memory. The access time is specified by the bus clock frequency. If the memory subsystem doesn't work fast enough to keep up with the CPU's expected access time, the CPU will read garbage data on a memory read operation and will not properly store the data on a memory write. This will surely cause the system to fail.


front: How do memory access waits work?
back:
Almost every general-purpose CPU in existence provides a pin (whose signal appears on the control bus) that allows the insertion of wait states. If necessary, the memory address decoding circuitry asserts this signal to give the memory sufficient access time.


front: How does the CPU avoid excessive wait states?
back:
However, we're not doomed to slow execution because of added wait states. There are several tricks hardware designers can employ to achieve zero wait states most of the time. The most common of these is the use of cache memory.


front: What are temporal locality of reference and spatial locality of reference?
back:
If you look at a typical program, you'll discover that it tends to access the same memory locations repeatedly. Furthermore, you'll also discover that a program often accesses adjacent memory locations. The technical names given to these phenomena are temporal locality of reference and spatial locality of reference. When exhibiting spatial locality, a program accesses neighboring memory locations within a short period after the initial memory access.


front: What is temporal locality of reference?
back:
When displaying temporal locality of reference, a program accesses the same memory location repeatedly during a short time.


front: How can a CPU keep data in cache?
back:
Cache memory can dynamically reassign addresses. This allows the system to keep recently accessed values in the cache. Addresses that the CPU has never accessed or hasn't accessed in some time remain in main (slow) memory. Because most memory accesses are to recently accessed variables (or to locations near a recently accessed location), the data generally appears in cache memory.


front: How can CPUs optimize when a cache miss occurs?
back:
When a cache miss occurs most caching systems will read several consecutive bytes of main memory (engineers call this block of data a cache line). 80x86 CPUs, for example, read between 16 and 64 bytes upon a cache miss.


front: What is the benefit of a large, third-level cache?
back:
For programs that manipulate considerable data, yet exhibit locality of reference, a third-level caching subsystem can be very effective.


front: What is direct addressing mode?
back:
The direct addressing mode encodes a variable's memory address as part of the actual machine instruction that accesses the variable.


front: What is indirect addressing mode?
back:
The indirect addressing mode typically uses a register to hold a memory address (there are a few CPUs that use memory locations to hold the indirect address, but this form of indirect addressing is rare in modern CPUs).


front: What is indexed addressing mode?
back:
The indexed addressing mode combines the direct and indirect addressing modes into a single addressing mode. Specifically, the machine instructions using this addressing mode encode both an offset (direct address) and a register in the bits that make up the instruction. At run time, the CPU computes the sum of these two address components to create an effective address.


front: How does a CPU lay out an array?
back:
Many optimizing compilers will attempt to place an array starting at a memory address that is an even multiple of some common size like two, four, or eight bytes. This effectively adds padding bytes before the beginning of the array or, if you prefer to think of it this way, it adds padding bytes to the end of the previous object in memory.


front: What is the flags register?
back:
The flags register, also known as the condition-codes register or program-status word, is an array of Boolean variables in the CPU that tracks whether the previous instruction produced an overflow, a zero result, a negative result, or other such condition.


front: What is the BIU and what does it do?
back:
Whenever the execution unit is not using the BIU (Bus Interface Unit), the BIU can fetch additional bytes from the memory that holds the machine instructions and store them in the prefetch queue.


front: What can the CPU optimize when the prefetch queue is not full?
back:
If the prefetch queue is not full (generally it can hold between 8 and 32 bytes, depending on the processor) and the BIU is idle on the current clock cycle, fetch the next double word located at the address found in the EIP register at the beginning of the clock cycle.


front: What can a CPU do when the instruction decoder is idle?
back:
If the instruction decoder is idle and the current instruction does not require an instruction operand, the CPU should begin decoding the opcode at the front of the prefetch queue. If the current instruction requires an instruction operand, then the CPU begins decoding the byte just beyond that operand in the prefetch queue.


front: How is it best to organize branches/jumps in your code?
back:
Therefore, if you can determine, while writing the program, which jump condition occurs most frequently, you should arrange your program so that the most common condition causes the program to continue with the next instruction rather than jump to a separate location.


front: How do memory access and instruction prefetches contend for resources?
back:
Don't forget, the CPU needs to use the bus for other purposes. Instructions that access memory compete with the prefetch queue for access to the bus.


front: How can bus contention occur?
back:
Bus contention can occur whenever an instruction needs to access an item in memory. Contention for the address and data bus may develop because the CPU will be trying to fetch data from memory and write data to memory simultaneously.


front: What is a pipeline stall and what does it help to solve?
back:
One simplistic way to handle bus contention is through a pipeline stall. The CPU, when faced with contention for the bus, gives priority to the instruction farthest along in the pipeline. This causes the later instruction in the pipeline to stall, and it takes two cycles to execute that instruction


front: What is an example of pipeline stalls involving a jump?
back:
As another example of a pipeline stall, consider what happens when an instruction modifies the value in the EIP register. For example, the jnz (jump if not zero) instruction might change the value in the EIP register if it conditionally transfers control to its target label. This, of course, implies that the next set of instructions to be executed does not immediately follow the instruction that modifies EIP. By the time the instruction jnz completes execution (assuming the zero flag is clear, so that the branch is taken), we've already started five other instructions and we're only one clock cycle away from the completion of the first of these. Obviously, the CPU must not execute those instructions, or it will compute improper results. The only reasonable solution is to flush the entire pipeline and begin fetching opcodes anew. Doing so causes a severe execution-time penalty.


front: When does a control hazard occur?
back:
A control hazard occurs whenever the CPU branches to some new location in memory and consequently has to flush from the pipeline the instructions that are in various stages of execution.


front: When does a data hazard occur?
back:
Note that a data hazard occurs when the source operand of one instruction was a destination operand of a previous instruction.


front: What is special about a superscalar CPU?
back:
A superscalar CPU has several execution units. If it encounters in the prefetch queue two or more instructions that can execute independently, it will do so.


front: What is out-of-order execution?
back:
The CPU can execute instructions prior to the completion of instructions appearing previously in the code stream.


front: How can a CPU use more registers than it has?
back:
Register renaming is a sneaky way to give a CPU more registers than it actually has. Programmers will not have direct access to these extra registers, but the CPU can use them to prevent hazards in certain cases.


front: What is special about a VLIW system?
back:
In a VLIW computer system, the CPU fetches a large block of bytes (41 bits in the case of the IA-64 Itanium CPU) and decodes and executes this block all at once. This block of bytes usually contains two or more instructions (three in the case of the IA-64). VLIW computing requires the programmer or compiler to properly schedule the instructions in each block so that there are no hazards or other conflicts, but if properly scheduled, the CPU can execute three or more instructions per clock cycle.


front: What is SIMD and what does it do?
back:
In the SIMD (Single Instruction Multiple Data) model, the CPU executes a single instruction stream, just like the pure SISD model. However, in the SIMD model, the CPU operates on multiple pieces of data concurrently rather than on a single data object.

An SIMD version of add, would compute the sum of several values simultaneously. The Pentium III's MMX and SIMD instruction extensions, and the PowerPC's AltaVec instructions, operate in exactly this fashion. With the paddb MMX instruction, for example, you can add up to eight separate pairs of values with the execution of a single instruction.


front: How are MMX registers different from a general purpose register?
back:
MMX registers (MM0 and MM1) actually hold eight independent byte values (the MMX registers are 64 bits wide but are treated as eight 8-bit values rather than as a single 64-bit value)


front: What is MIMD?
back:
Multiple Instruction, Multiple Data
If you have a multiprogramming environment with multiple programs attempting to execute concurrently, the MIMD model does allow each of those programs to execute their own code stream simultaneously. This type of parallel system is called a multiprocessor system.


front: When does multiprocessing (running on multiple processors) not help you?
back:
Multiprocessing doesn't help a program's performance unless that program is specifically written for use on a multiprocessor system.


front: What is the cache-coherency problem?
back:
Multiple CPUs operating on the same object in memory. 
In order for these two functions to operate properly, the two CPUs must notify each other whenever they make changes to shared objects, so the other CPU can update its local, cached copy.


front: What is hyper-threading?
back:
In a typical superscalar processor it is rare for an instruction sequence to utilize all the CPU's functional units on each clock cycle. Rather than allow those functional units to go unused, the CPU can run two separate threads of execution concurrently and keep all the CPU's functional units occupied. This allows a single CPU to, effectively, do the work of 1.5 CPUs in a typical multiprocessor system.


front: What is NUMA?
back:
NUMA, which stands for Non-Uniform Memory Access, is a bit of a misnomer. The term NUMA implies that different types of memory have different access times, and so it is descriptive of the entire memory hierarchy. 
A good example of NUMA memory is the memory on a video display card. Another example is flash memory, which has significantly slower access and transfer times than standard semiconductor RAM.


front: What is virtual memory?
back:
Most modern computer systems implement a virtual memory scheme that simulates main memory using a mass storage disk drive. A virtual memory subsystem is responsible for transparently copying data between the disk and main memory as needed by programs.


front: What is DSM?
back:
Distributed shared memory (DSM), where processes running on different computer systems share data stored in a common block of memory and communicate changes to that block across the network.


front: What is the penalty for a main memory access? Why does it never take one cycle?
back:
If the program has to bring the data from main memory, 999 memory accesses later you're still paying an average cost of two clock cycles to access the data that Intel's documentation claims should happen in one cycle.


front: What is the clock timing penalty of an L2 access?
back:
Accessing data in the L2 cache is always slower than in the L1 cache, and there is always the equivalent of at least one wait state, and up to eight, when accessing data in the L2 cache.


front: How does L2 cache access help pay for itself?
back:
If you execute the mov(mem32,eax); instruction, and mem32's value is not in the L1 cache, the cache controller doesn't simply read mem32's 32 bits from the L2 cache, assuming that it's present there. Instead, the cache controller will actually read a whole block of bytes (generally 16, 32, or 64 bytes, depending on the particular processor) from the L2 cache.


front: How many bytes are there per cache line?
back:
Cache memory is usually organized in blocks of cache lines, with each line containing some number of bytes (typically a small power of two like 16, 32, or 64)


front: What is the relationship to cache lines to their alignment?
back:
Generally, if a cache line is n bytes long, that cache line will hold n bytes from main memory that fall on an n-byte boundary.


front: With 512 cache lines, how are the bits in the memory address used to map to a cache line and byte?
back:
8 KB cache with 512 16-byte cache lines and a 32-bit main-memory address. Because there are 512 cache lines, it requires 9 bits to select one of the cache lines (29 = 512). This example uses bits 4 through 12 to determine which cache line to use (assuming we number the cache lines from 0 to 511), while bits 0 through 3 of the original memory address determine the particular byte within the 16-byte cache line.


front: Why are fully associative caches generally not used?
back:
The extra circuitry to achieve full associativity is expensive and, worse, can slow down the memory subsystem.


front: Which type of caches are better for instructions, for code?
back:
In particular, the direct-mapped cache is very good for data that you access in a sequential rather than random fashion. Because the CPU typically executes instructions in a sequential fashion, instruction bytes can be stored very effectively in a direct-mapped cache. However, because programs tend to access data more randomly than code, a two-way or four-way set associative cache usually makes a better choice for data accesses than a direct-mapped cache.

Because data and machine instruction bytes usually have different access patterns, many CPU designers use separate caches for each.


front: What is temporal locality?
back:
If a memory location has not been accessed in a while, it is likely to be a long time before the CPU accesses it again.


front: What is write-through policy?
back:
The write-through policy states that any time data is written to the cache, the cache immediately turns around and writes a copy of that cache line to main memory.


front:
What is a good cache write policy to use when two different CPUs are communicating through shared memory?

back:
Because the write-through policy updates main memory with the new value as rapidly as possible, it is a better policy to use when two different CPUs are communicating through shared memory.


front: What is a write-back policy?
back:
In this mode, writes to the cache are not immediately written to main memory; instead, the cache controller updates main memory at a later time. This scheme tends to be higher performance because several writes to the same cache line within a short time period do not generate multiple writes to main memory.


front: How does a cache keep track of data that needs to be written back to main memory?
back:
the cache controller usually maintains a dirty bit within each cache line. The cache system sets this bit whenever it writes data to the cache.


front:
What does a cache controller have to do before it replaces a cache line with other data from memory?

back:
Whenever the cache controller replaces a cache line with other data from memory, it must first check the dirty bit, and if that bit is set, the controller must write that cache line to memory before going through with the cache-line replacement.


front: What has to be inherent in software in order for a cache system to be effective?
back:
In order for a cache system to be effective, software must exhibit locality of reference (either spatial or temporal). Fortunately, real-world programs tend to exhibit locality of reference, so most programs will benefit from the presence of a cache in the memory subsystem.


front: What are memory pages?
back:
You break up memory into blocks of bytes called pages. A page in main memory is comparable to a cache line in a cache subsystem, although pages are usually much larger than cache lines. For example, the 80x86 CPUs use a page size of 4,096 bytes.


front: How does the page table not take up cache space?
back:
To prevent cluttering the data or instruction cache with page-table entries, which increases the number of cache misses for data and instruction requests, the page table uses its own cache, known as the translation lookaside buffer (TLB).


front: How are the bits in a page table entry used?
back:
Each entry in the page table contains 32 bits, even though the system really only needs 20 bits to remap each virtual address to a physical address. Intel, on the 80x86, uses some of the remaining 12 bits to provide some memory-protection information.


front: How can your software change bits in the page table?
back:
Note that your applications do not have access to the page table (reading and writing the page table is the operating system's responsibility), and therefore they cannot modify these bits. However, operating systems like Windows may provide some functions you can call if you want to change certain bits in the page table (for example, Windows will allow you to set a page to read-only if you want to do so)


front: What is a working set?
back:
At any given time, a program will only access a small percentage of the pages in main memory that contain data and instruction bytes and this set of pages is known as the working set.


front: What happens when you attempt to access a page in memory?
back:
If you attempt to access a page of memory, and the page-table bit tells the memory management unit (MMU) that the page is not present in main memory, the CPU interrupts the program and passes control to the operating system.


front: How can you think of main memory?
back:
You can think of main memory as a fully associative write-back cache with 4,096-byte cache lines, which caches the data that is stored on the disk drive.


front: What are typical page sizes?
back:
Page sizes tend to vary from about 1 KB to 64 KB, depending on the CPU. For CPUs that support an address space larger than 4 GB, some CPUs use an inverted page table or a three-level page table.


front: What is the disadvantage of a program that does not exhibit locality of reference?
back:
Insufficient memory at a given level in the memory hierarchy to properly contain the program working sets of cache lines or pages.


front: What does the operating system do with the lowest addresses in memory allocated for a process?
back:
The operating system reserves the lowest memory addresses. Generally, your application cannot access data (or execute instructions) at the lowest addresses in memory. One reason the OS reserves this space is to help detect NULL pointer references.


front: What are static objects?
back:
Static objects are those that have an attribute bound to them prior to the execution of the application. Constants are good examples of static objects; they have the same value bound to them throughout the execution of the application.


front: What is the lifetime of a static object?
back:
The lifetime of a static object extends from the point at which the program first begins execution to the point when the application terminates.


front: What is the scope of an identifier?
back:
The scope of an identifier is that section of the program where the identifier's name is bound to the object. As names exist only during compilation, scope is definitely a static attribute in compiled languages. (In interpretive languages, where the interpreter maintains the identifier names during program execution, scope can be a non-static attribute.)


front: What do you find in an executable file?
back:
In most executable files, you'll find a single section that combines the code, read-only data, and constant data sections.


front: What is the BSS section?
back:
The BSS section is where compilers typically put static objects that don't have an explicit value associated with them. BSS stands for block started by a symbol, which is an old assembly language term describing a pseudo-opcode one would use to allocate storage for an uninitialized static array. In modern OSes like Windows and Linux, the OS allows the compiler/linker to put all uninitialized variables into a BSS section that simply contains information that tells the OS how many bytes to set aside for the section.


front: What it the stack?
back:
The stack is a data structure that expands and contracts in response to procedure invocations and the return to calling routines, among other things. At run time, the system places all automatic variables (non-static local variables), subroutine parameters, temporary values, and other objects in the stack section of memory in a special data structure we call the activation record (which is aptly named because the system creates an activation record when a subroutine first begins execution, and it deallocates the activation record when the subroutine returns to its caller).


front: What is the different between a hardware stack and a software stack?
back:
If a CPU provides an explicit stack-pointer register (ESP), we say that the CPU supports a hardware stack; if only a general-purpose register is available, then we say that the CPU uses a software-implemented stack.


front: What are anonymous variables?
back:
We'll usually refer to objects in heap memory as anonymous variables because we refer to them by their memory address (via pointers) rather than by a name.


front: What is a simple heap manager implementation?
back:
Easy implementation of a heap manager that supports garbage collection. This simple system maintains a (linked) list of free memory blocks. Each free memory block in the list will require two double-word values: one double-word value specifies the size of the free block, and the other double word contains a link to the next free block in the list (that is, a pointer)


front: What are first-fit and best-fit?
back:
The first-fit algorithm does have a couple of advantages over the best-fit algorithm, though. The most obvious advantage is that the first-fit algorithm is usually faster. The best-fit algorithm has to scan through every block in the free block list in order to find the smallest block large enough to satisfy the allocation request


front: What is an advantage of first-fit in heap management?
back:
Another advantage to the first-fit algorithm is that it tends to suffer less from a degenerate condition known as external fragmentation. Fragmentation occurs after a long sequence of allocation and deallocation requests. Remember, when the heap manager satisfies a memory allocation request, it usually creates two blocks of memory — one in-use block for the request and one free block that contains the remaining bytes in the original block after the request is filled (assuming the heap manager did not find an exact fit). After operating for a while, the best-fit algorithm may wind up producing lots of smaller, leftover blocks of memory that are too small to satisfy an average memory request (because the best-fit algorithm also produces the smallest leftover blocks as a result of its behavior).


front: How does heap memory allocation optimize for speed?
back:
If an application calls the operating system for every memory request it makes, the performance of the application will probably suffer if the application makes several memory allocation and deallocation calls. OS API calls are very slow, because they generally involve switching between kernel mode and user mode on the CPU (which is not fast).

On the very first memory allocation, the malloc routine will request a large block of memory from the operating system, and then the application's malloc and free routines will manage this block of memory themselves.


front: What is allocation granularity?
back:
Although most heap managers will allow you to allocate storage in blocks as small as one byte, most memory managers will actually allocate some minimum number of bytes greater than one. This minimum amount is the allocation granularity the memory manager supports.


front: What is the usual allocation alignment?
back:
For performance reasons, many heap managers begin each allocation on a typical cache-line boundary, usually 16, 32, or 64 bytes.


front: How is internal fragmentation somewhat avoided?
back:
The granularity size is quite small for most memory managers (typically 16 bytes or fewer), so after thousands and thousands of memory allocations you'll only lose a couple dozen or so kilobytes to internal fragmentation.


front: What is a problem with allocation on the heap of many small objects?
back:
If you allocate lots of small objects, the memory consumed by internal fragmentation and control information (meta information stored with each heap block) may represent a significant portion of your heap area.


front: How are I/O operations similar to memory read and write operations?
back:
I/O operations behave much like memory read and write operations, except that there are usually more wait states associated with I/O operations.


front: What must be active for a latch to operate?
back:
Both the address decode line (En) and the write control line (W) must be active for the latch to operate.


front: What does the CPU read from a read/write port?
back:
Whenever the CPU reads data from a read/write port, it reads the data that was last written to the port allowing a programmer to retrieve that value.


front: What is a dual I/O port?
back:
A dual I/O port is also a read/write port, but when you read a dual I/O port, you read data from an external input device rather than the last data written to the output side of the port's address.


front: What is a dual I/O port composed of?
back:
Note that a dual I/O port is actually created using two ports — a read-only port and a write-only port — that share the same port address.


front: What is a bidirectional port?
back:
A bidirectional port allows the CPU to both read and write data to an external device. To function properly, a bidirectional port must pass various control lines, such as read and write enable, to the peripheral device so that the device can change the direction of data transfer based on the CPU's read/write request. In effect, a bidirectional port is an extension of the CPU's bus through a bidirectional latch or buffer.


front: What is memory-mapped I/O?
back:
Memory-mapped I/O uses ordinary locations within the CPU's memory address space to communicate with peripheral devices.


front: What is I/O-mapped input/output?
back:
I/O-mapped input/output uses an address space separate from memory, and it uses special machine instructions to transfer data between that special I/O address space and the outside world.


front: What is DMA?
back:
DMA is a special form of memory-mapped I/O where the peripheral device reads and writes data located in memory without CPU intervention


front: How does a memory-mapped peripheral device work?
back:
A memory-mapped peripheral device is connected to the CPU's address and data lines exactly like regular memory, so whenever the CPU writes to or reads from the address associated with the peripheral device, the CPU transfers data to or from the device.


front: How do some video cards handle their memory?
back:
Some video cards have 32 MB of on-board memory that they map into the memory address space and this means that the 32 MB address range consumed by the card is not available to the system for use as regular RAM memory.


front: How can a programmer get access to I/O address space?
back:
As almost all high-level languages provide the ability to access memory, but most do not allow access to the I/O space, having the PCI bus remap the I/O address space into the memory address space provides I/O access to those high-level languages.


front: What is direct memory access and its advantage in timely processing?
back:
For very high-speed I/O devices the CPU may be too slow to process this data one byte (or one word or double word) at a time. Such devices generally have an interface to the CPU's bus so they can directly read and write memory, which is known as direct memory access because the peripheral device accesses memory directly, without using the CPU as an intermediary.


front: What is a medium-speed device?
back:
Devices that transfer data at approximately the same rate as, or up to two orders of magnitude slower than, the CPU.


front: What are high-speed devices?
back:
Devices that transfer data faster than the CPU is capable of handling using programmed I/O. High-speed devices must use DMA because programmed I/O is too slow.


front: What I/O mechanisms do medium and low-speed devices use?
back:
Medium- and low-speed devices may use any of the three I/O mechanisms for data transfer (though low-speed devices rarely use DMA because of the cost of the extra hardware involved).


front: How do I/O devices communicate with the interrupt controller?
back:
Most peripheral buses provide a common set of interrupt control signals that let I/O devices communicate directly with the system's interrupt controller, which is also a peripheral device.


front: What must a CPU do if it wishes to access a peripheral on the PCI bus?
back:
Whenever the CPU wishes to access a peripheral on the PCI bus, it must negotiate with other peripheral devices for the right to use the bus. This negotiation can take several clock cycles before the PCI controller grants the CPU access to the bus.


front: What can be used to make I/O ports accessible from user applications?
back:
It is possible to use the Linux ioperm system call to make certain I/O ports accessible from user applications.


front: How does the OS save reads when writing full blocks to disk?
back:
If your block size is 4,096 bytes, but you just write 2,000 bytes to some block and then seek to some other position in the file outside that block, the OS will actually have to read the entire 4,096-byte block from the disk, merge in the 2000 bytes, and then finally write the entire 4,096 bytes back to the disk. This happens because the OS must read and write entire blocks; it cannot transfer partial blocks between the disk and memory. Contrast this with a write operation that writes a full 4,096 bytes — in this case, the OS wouldn't have to read the data from the disk first; it would only have to write the block.


front: What are memory-mapped files?
back:
Memory-mapped files use the OS's virtual memory capabilities to map memory addresses in the application space directly to blocks on the disk. Because modern OSes have highly optimized virtual memory subsystems, piggy-backing file I/O on top of the virtual memory subsystem can produce very efficient file access.


front: What is a drawback of memory-mapped files?
back:
Almost every OS does memory-mapped file access differently, and there is little chance that memory-mapped file I/O code will be portable between OSes.


front: What is LSD radix sort?
back:
Least Significant Digit first radix sort. It doesn't have to be a digit, just a symbol in an alphabet. Sorts stably from right to left.


front: What is MSD radix sort?
back:
Most Significant Digit first radix sort. It doesn't have to be a digit, just a symbol in an alphabet. Sorts stably from left to right, recursing when the symbols in a block equal each other.


back: O(|string| * |len of longest pattern|)
front:
What is the time complexity of using a trie to store patterns, then testing that trie against a string, looking for matches?


front: What type of input does MSD radix sort do best on?
back:
when the data to be sorted seems random

when the data is very similar, it can come to linear time work, which is slow for MSD


front: When making a trie node, what is the typical implementation of the children?
back:
An array of size R (the radix or alphabet size) with a pointer to continuing paths. Paths that do not continue have null stored in the array indexes for those paths.


back: i <= n - m
front:
When searching for a match in string with length N and pattern length M, where should we stop to ensure we don't look for matches at the end that would be too short?


front: What is Kleene's theorem?
back:
It states that any regular language (regular expression) is accepted by an FA and conversely that any language accepted by an FA is regular.


front: What is the difference between a DFA and an NFA?
back:
DFA can have only one applicable transition per character.
NFA can have several applicable transitions.


front: What is the behavior of a greedy strategy?
back:
A greedy strategy usually progresses in a top-down order, making one greedy choice after another and reducing each given problem instance to a smaller one.


front: What are 2 properties of an interview problem with a potential dynamic programming solution?
back:
If an interview problem has optimal substructure and overlapping subproblems, it might be solved by dynamic programming.


front: What is backtracking?
back:
Backtracking is a refinement of the brute-force approach, which systematically searches for a solution to a problem among all available options. It is suitable for scenarios where there is a set of options available at each step, and we must choose one from these. After a choice is made, there is a new set of options for the next step. This procedure is repeated over and over until we reach a final state.


front: How does a backtracking algorithm behave?
back:
The backtracking algorithm traverses this tree recursively, from the root down and in depth-first order. When it reaches a leaf that corresponds to a non-acceptable state, it backtracks to continue the search for another leaf by revoking the most recent choice and tries out the next option. If it runs out of options, it revokes again and tries another choice at that node. If it ends up at the root with no options left, there are no acceptable states to be found.


front: How do Python's encode and decode methods work?
back:
To convert Unicode characters to binary data, you must use the encode method. To convert binary data to Unicode characters, you must use the decode method.


front: How can you interpret a string in Python as an integer with a base other than 10?
back:
The integer constructor, int(), by default, the string must use base 10. If conversion from a different base is desired, that
base can be indicated as a second, optional, parameter. For example, the expression int(7f, 16) evaluates to the integer 127.


front: What is the ordering relationship in a sequence in Python?
back:
The list, tuple, and str classes are sequence types in Python, representing a collection of values in which the order is significant.


front: How can you copy a list in Python?
back:
Because an existing list is itself iterable, the syntax backup = list(data) can be used to construct a new list instance referencing the same contents as the original.


front: How can you initialize a Python dictionary with values already declared elsewhere?
back:
The constructor accepts a sequence of key-value pairs as a parameter, as in dict(pairs) with:
pairs = [('ga', 'Irish'), ('de', 'German')].


front: How can you tell that two Python objects are the same object?
back:
The expression a is b evaluates to True, precisely when identifiers a and b are aliases for the same object.


front: What is the division rule in Python?
back:
Python guarantees that q*d + r will equal n.
d = denominator
q = quotient
r = remainder
n = numerator


front: How can you test that two dictionaries contain the same set of key-value pairs?
back:
Dictionaries support the notion of equivalence, with d1 == d2 if the two dictionaries contain the same set of key-value pairs.


back: beta += [4, 5]
front:
In Python, how can you extend a list with two more elements? (adding to the original list, not making a new one)


back: beta = beta + [6, 7]
front:
In Python, how do you reassign a list beta to a new list [1, 2, 3, 4, 5, 6, 7]? (making a new object instead of adding to the original)


front: How does an expression like 1 <= x + y <= 10 save computation?
back:
The expression 1 <= x + y <= 10 is evaluated as the compound (1 <= x + y) and (x + y <= 10), but without computing the intermediate value x + y twice.


front: What happens when you call a Python function?
back:
Each time a function is called, Python creates a dedicated activation record that stores information relevant to the current call. This activation record includes what is known as a namespace to manage all identifiers that have local scope within the current call.


front: How does Python support polymorphism, method overloading?
back:
Python provides means for functions to support more than one possible calling signature using default parameter values. Such a function is said to be polymorphic.


front: What is the rule for default parameters in Python?
back:
It is illegal to define a function with a signature such as bar(a, b=15, c) with b having a default value, yet not the subsequent c; if a default parameter value is present for one parameter, it must be present for all further parameters.


back: KeyError
front:
What kind of exception is raised in an attempt to access a nonexistent element from a set or dictionary?


back: IndexError
front:
What kind of exception is raised on a sequence when syntax such as data[k] is used with an integer k that is not a valid index for the given sequence?


front: When should you use try/except?
back:
The try-except clause is best used when there is reason to believe that the exceptional case is relatively unlikely, or when it is prohibitively expensive to proactively evaluate a condition to avoid the exception.


front: Can you use return and yield together?
back:
It is illegal to combine yield and return statements in the same implementation, other than a zero-argument return statement to cause a generator to end its execution.


front: What are first-class objects?
back:
First-class objects are instances of a type that can be assigned to an identifier, passed as a parameter, or returned by a function.


front: What is the convention for non-public data members and member functions in Python?
back:
By convention, names of members of a class (both data members and member functions) that start with a single underscore character (e.g., _secret) are assumed to be nonpublic and should not be relied upon.


back: __len__ and __getitem__
front:
Python provides an automatic iterator implementation for any class that implements what two methods?


front: What is the template method pattern?
back:
The template method pattern is when an abstract base class provides concrete behaviors that rely upon calls to other abstract behaviors.

The template method pattern describes a generic computation mechanism that can be specialized for a particular application by redefining certain steps. To allow customization, the primary algorithm calls auxiliary functions known as hooks at designated steps of the process.


front: What is an important caveat of __slots__ in an inheritance hierarchy?
back:
When inheritance is used, if the base class declares __slots__, a subclass must also declare __slots__ to avoid creation of instance dictionaries. The declaration in the subclass should only include names of supplemental methods that are newly introduced.


front: What is dynamic dispatch?
back:
Python uses what is known as dynamic dispatch (or dynamic binding) to determine, at run-time, which implementation of a function to call based upon the type of the object upon which it is invoked.


front: What is static dispatching?
back:
Making a compile-time decision as to which version of a function to call, based upon the declared type of a variable.


front: What is the definition of Big-O (upper bound)?
back:
We say that f(n) is O(g(n)) if there is a real constant c > 0 and an integer constant n0 ≥ 1 such that f(n) ≤ cg(n), for n ≥ n0.


front: Describe the adapter design pattern.
back:
The adapter design pattern applies to any context where we effectively want to modify an existing class so that its methods match those of a related, but different, class or interface.


front: What data structure can be used to reverse a sequence?
back:
As a consequence of the LIFO protocol, a stack can be used as a general tool to reverse a data sequence.


back: f = (f + 1) % N
front:
When using a circular array queue, when we dequeue an element and want to "advance" the front index, what is the arithmetic for computing the front of the queue f?


back: array linked
front:
Operations with equivalent asymptotic bounds typically run a constant factor more efficiently with an _____-based structure versus a _____ structure.


front: How many nodes are in a tree of height d?
back:
In a binary tree, level 0 has at most one node (the root), level 1 has at most two nodes (the children of the root), level 2 has at most four nodes, and so on. In general, level d has at most 2^d nodes.


front: What is a drawback of an array representation of trees?
back:
Some update operations for trees cannot be efficiently supported. For example, deleting a node and promoting its child takes O(n) time because it is not just the child that moves locations within the array, but all descendants of that child.


front:
What are the time complexities of locating the last position of a heap, as required for add and remove min, in an array representation or tree representation?

back:
Locating the last position of a heap, as required for add and remove min, can be performed in O(1) time for an array-based representation, or O(log n) time for a linked-tree representation.


back: O(n + k log n) n: heapify k: accesses log n: cost per access
front:
What is the time complexity to produce a list of the k largest values from an iterable, using a heap?


back: 33, 37, 39, and 41
front:
What are some good numbers to hash with character strings that are English words, to create a good hash?


front: What is the standard mechanism for computing hash codes in Python?
back:
A built-in function with signature hash(x) that returns an integer value that serves as the hash code for object x.


back: compression function
front:
What is the second action performed as part of an overall hash function called that fits the hash into the storage space?


front: What is the caveat for choosing a prime for N for your hash table size?
back:
Choosing N to be a prime number is not always enough, for if there is a repeated pattern of hash codes of the form p*N + q for several different p's, then there will still be collisions.


front: What is the rule of thumb when coming up with the right load factor?
back:
The ratio λ = n/N, called the load factor of the hash table, should be bounded by a small constant, preferably below 1. As long as λ is O(1), the core operations on the hash table run in O(1) expected time.


front: What does open addressing require?
back:
Open addressing requires that the load factor is always at most 1 and that items are stored directly in the cells of the bucket array itself.


front: What is quadratic probing?
back:
Another open addressing strategy that iteratively tries the buckets A[(h(k) + f (i)) mod N], for i = 0,1,2, . . ., where f(i) = i^2, until finding an empty bucket.


front: What does quadratic probing suffer from?
back:
It creates its own kind of clustering, called secondary clustering, where the set of filled array cells still has a non-uniform pattern, even if we assume that the original hash codes are distributed uniformly.


front: What is the double hashing strategy?
back:
In this approach, we choose a secondary hash function, h', and if h maps some key k to a bucket A[h(k)] that is already occupied, then we iteratively try the buckets A[(h(k) + f(i)) mod N] next, for i = 1,2,3, . . where f(i) = i * h'(k).
In this scheme, the secondary hash function is not allowed to evaluate to zero; a common choice is h(k) = q−(k mod q), for some prime number q < N. Also, N should be a prime.


front: What is a sorted table implementation of a hash table best suited for?
back:
Sorted tables are primarily used in situations where we expect many searches but relatively few updates.


front: The O(log n) time bounds for skip lists extend to what operations over a sorted list?
back:
The skip list has the same logarithmic time bounds for searching as is achieved by the binary search algorithm, yet it extends that performance to update methods when inserting or deleting items. Nevertheless, the bounds are expected for the skip list, while binary search has a worst-case bound with a sorted table.


back: O(n + h)
front:
What is the worst-case running time for performing the __getitem__, __setitem__, and __delitem__ map operations in a skip list S with n entries and height h?


front: What is a multimap?
back:
A multimap is similar to a traditional map, in that it associates values with keys; however, in a multimap the same key can be mapped to multiple values. For example, the index of this book maps a given term to one or more locations at which the term occurs elsewhere in the book.


front: What is a way to implement a multiset?
back:
A way to implement a multiset is by using a map in which the map key is a (distinct) element of the multiset, and the associated value is a count of the number of occurrences of that element within the multiset.


front: What is a predecessor?
back:
Return the position containing the greatest key that is less than that of position p (i.e., the position that would be visited immediately before p in an inorder traversal)


front: What is a successor?
back:
The position containing the least key that is greater than that of position p (i.e., the position that would be visited immediately after p in an inorder traversal)


front: What is an example of the factory method design pattern?
back:
We provide a subclass the means to control the type of node that is created within methods of the parent class, by inheriting and augmenting.


front: What is the special property of AVL trees?
back:
Height-Balance Property: For every position p of T, the heights of the children of p differ by at most 1.


front: What is special about the self-balancing property of a splay tree?
back:
A splay tree does not strictly enforce a logarithmic upper bound on the height of the tree. In fact, there are no additional height, balance, or other auxiliary data associated with the nodes of this tree.


front: Why is splaying vital to the self-balancing property of a splay tree?
back:
Splaying allows us to guarantee a logarithmic amortized running time, for insertions, deletions, and searches.


front: How does a red-black tree relate to a (2,4) tree?
back:
Given a red-black tree, we can construct a corresponding (2,4) tree by merging every red node w into its parent, storing the entry from w at its parent, and with the children of w becoming ordered children of the parent.


front: What is the time complexity advantage of a red-black tree?
back:
The primary advantage of a red-black tree is that an insertion or deletion requires only a constant number of restructuring operations.


front: What is bottom-up merge sort?
back:
The main idea is to perform merge-sort bottom-up, performing the merges level by level going up the merge-sort tree. Given an input array of elements, we begin by merging every successive pair of elements into sorted runs of length two. We merge these runs into runs of length four, merge these new runs into runs of length eight, and so on, until the array is sorted.


front: What is the optimal relationship of range N of keys to sequence size n in bucket sort?
back:
Bucket-sort is efficient when the range N of values for the keys is small compared to the sequence size n, say N = O(n) or N = O(n log n). Still, its performance deteriorates as N grows compared to n.


front: What is the rule of thumb in sorting on 2 or more columns across a stable sort?
back:
By first stably sorting by the second component and then again by the first component, we guarantee that if two entries are equal in the second sort (by the first component), then their relative order in the starting sequence (which is sorted by the second component) is preserved.


front: What is the time complexity of radix sort across d keys of size N?
back:
Let S be a sequence of n key-value pairs, each of which has a key (k1,k2, . . . ,kd), where ki is an integer in the range [0,N − 1] for some integer N ≥ 2. We can sort S lexicographically in time O(d(n + N)) using radix-sort.


back: O(n + m)
front:
What is the running time of insertion-sort  on n items where m is the number of inversions (the number of pairs of elements out of order)?


front:
Even though it's O(n^2)-time worst-case performance makes quicksort susceptible in real-time applications, what can we expect of its performance?

back:
We expect its performance to be O(n log n)-time, and experimental studies have shown that it outperforms both heap-sort and merge-sort on many tests.


front: What is the shining using case of merge sort?
back:
Merge-sort is an excellent algorithm for situations where the input is stratified across various levels of the computer's memory hierarchy (e.g., cache, main memory, external memory). In these contexts, the way that merge-sort processes runs of data in long merge streams makes the best use of all the data brought as a block into a level of memory, thereby reducing the total number of memory transfers.


front: What is Timsort and why is it a great sorting algorithm?
back:
Tim-sort (designed by Tim Peters), which is essentially a bottom-up merge-sort that takes advantage of some initial runs in the data while using insertion-sort to build additional runs.


front: What is prune-and-search or decrease-and-conquer?
back:
We solve a given problem that is defined on a collection of n objects by pruning away a fraction of the n objects and recursively solving the smaller problem.


front: When generating a Huffman code, what is an important restriction on the encoding for each element?
back:
In order to avoid ambiguities, we insist that no code-word in our encoding be a prefix of another code-word in our encoding. Such a code is called a prefix code, and it simplifies the decoding of Y to retrieve X.


back: O(n + d log d)
front:
What is the time complexity when Huffman’s algorithm constructs an optimal prefix code for a string of length n with d distinct characters?


front: What is the greedy-choice property?
back:
This is the property that a global optimal condition can be reached by a series of locally optimal choices (that is, choices that are each the current best from among the possibilities available at the time), starting from a well-defined starting condition.


front: What is the use case when preprocessing a text is worth it?
back:
Where a series of queries is performed on a fixed text, so that the initial cost of preprocessing the text is compensated by a speedup in each subsequent query.


front: What is the advantage of a compressed trie over a standard trie?
back:
The number of nodes of the compressed trie is proportional to the number of strings and not to their total length.


front: How does a suffix trie save space over a list of all suffixes?
back:
Storing all the suffixes of X explicitly would take O(n^2) space. Even so, the suffix trie represents these strings implicitly in O(n) space.


front: What is a strongly connected graph?
back:
A directed graph G is strongly connected if for any two vertices u and v of G, u reaches v and v reaches u.


front: What is an adjacency map?
back:
An adjacency map is very similar to an adjacency list, but the secondary container of all edges incident to a vertex is organized as a map, rather than as a list, with the adjacent vertex serving as a key. This allows for access to a specific edge (u, v) in O(1) expected time.


front: What can we claim of the performance of adjacency map?
back:
We find that it essentially achieves optimal running times for all methods, making it an excellent all-purpose choice as a graph representation.


front: How can an adjacency matrix be made space efficient?
back:
If edges do not have auxiliary data, a Boolean adjacency matrix can use one bit per edge slot, such that A[i, j] = True if and only if associated (u, v) is an edge.


front: What are some uses of DFS for testing of properties of graphs?
back:
- whether there is a path from one vertex to another (transitive closure)
- whether or not a graph is connected


front: What is a tree edge and what is a non-tree edge in DFS?
back:
Whenever an edge e = (u,v) is used to discover a new vertex v during the DFS algorithm, that edge is known as a discovery edge or tree edge, as oriented from u to v. All other edges that are considered during the execution of DFS are known as non-tree edges, which take us to a previously visited vertex.


front: What is special about discovery edges in DFS?
back:
Since we only follow a discovery edge when we go to an unvisited vertex, we will never form a cycle with such edges. Therefore, the discovery edges form a connected subgraph without cycles, hence a tree. Moreover, this is a spanning tree because, as we have just seen, the depth-first search visits each vertex in the connected component of s.


back: O(ns + ms)
front:
What is the runtime of DFS starting at vertex s, where ns is the number of vertices reachable from s, and ms is the number of incident edges to those vertices?


front:
What is the running time of BFS if ns is the number of vertices reachable from vertex s, and ms ≤ m is the number of incident edges to those vertices?

back:
Similar to the one of DFS, with the algorithm running in O(n + m) time, or more specifically, in O(ns + ms) time.


front: What is the transitive closure of a directed graph?
back:
It's a directed graph where the vertices are the same as G, but vertices are connected directly when a path exists between those vertices in G.


front:
What is the runtime of generating the transitive closure of a directed graph and what method can be used?

back:
Could use Floyd-Warshall, but it is O(n^3), and you may not need the minimum path weight.
Repeated calls to DFS results in better asymptotic performance when the graph is sparse and represented using an adjacency list or adjacency map. In that case, a single DFS runs in O(n + m) time, and so the transitive closure can be computed in O(n^2 + nm) time, which is preferable to O(n^3).


front: What is the time and space complexity of topological sort?
back:
Let G be a directed graph with n vertices and m edges, using an adjacency list representation. The topological sorting algorithm runs in O(n + m) time using O(n) auxiliary space, and either computes a topological ordering of G or fails to include some vertices, which indicates that G has a directed cycle.


front: What is the running time of Kruskal's algorithm?
back:
O(m log n)

For a connected graph, m ≥ n − 1, and therefore, the bound of O(m log n) time for ordering the edges dominates the time for managing the clusters (which is log*n).


front: What does log∗ n mean?
back:
log∗ n is the number of times that one can iteratively take the logarithm (base 2) of a number before getting a number smaller than 2.


front: How does in-place DFS work and how does it save precious space when performing garbage collection?
back:
The main idea for performing DFS in-place is to simulate the recursion stack using the edges of the graph (which in the case of garbage collection correspond to object references). When we traverse an edge from a visited vertex v to a new vertex w, we change the edge (v, w) stored in v's adjacency list to point back to v's parent in the DFS tree. When we return back to v (simulating the return from the "recursive" call at w), we can then switch the edge we modified to point back to w, assuming we have some way to identify which edge we need to change back.


front: What are the best to worst caching strategies?
back:
The LRU strategy has been shown to be usually superior to the FIFO strategy, which is usually better than the random strategy.


front: What is an (a, b) tree?
back:
An (a, b) tree is a multiway search tree such that each node has between a and b children and stores between a − 1 and b − 1 entries. The algorithms for searching, inserting, and removing entries in an (a, b) tree are straightforward generalizations of the corresponding ones for (2, 4) trees.


front: How do we select the a and b of an (a, b) tree?
back:
To minimize disk accesses, we select the parameters a and b so that each tree node occupies a single disk block (so that f (b) = 1 if we wish to simply count block transfers).


front: What is a B-tree in relation to an (a, b) tree?
back:
A B-tree of order d is an (a, b) tree with a = ceiling(d/2) and b = d.

We specifically choose d = (M/B) − 1 so that we can afford to keep one block from each input sequence in main memory at any given time, and to have one additional block to use as a buffer for the merged sequence.


front: A B-tree with n entries has what I/O complexity and uses how many blocks?
back:
- O(log(base B) n) for search or update operation
- uses O(n/B) blocks, where B is the size of a block


back: Up to M/B blocks.
front:
if main memory has size M and each block has size B how many blocks can we store within main memory at any given time?


front:
What is the I/O complexity, given an array-based sequence S of n elements stored compactly in external memory, of sorting S?

back:
- O((n/B)log(n/B)/log(M/B)) block transfers where M is the size of the internal memory and B is the size of a block.
- O(n log n) internal computations


