Coding university Flashcards
What is Hamming Code?
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.
Using bitwise operations, how would you test that a number is a power of 2
bool isPowerOfTwo = (x & (x - 1);
What does ELF stand for?
Executable and Linkable Format. It’s a common standard file format for executables, object code, shared libraries, and core dumps.
example of a latency device
CPU core
example of a throughput device
GPU core
What is the Hamming Distance?
A number used to denote the number of differences between two binary strings of the same length.
What are the 5 steps of the compiling process?
Lexical Analysis
Parsing
Semantic Analysis
Optimization
Code Generation
What is parsing?
Combining tokens and groups of tokens into a tree structure (a parse tree).
What is lexical analysis?
The process of dividing program text into words or tokens.
What is code generation?
Producing a translation from a high-level program to assembly code. (Linker and Archiver taker over from here to produce machine code)
How would you turn OFF the 3rd bit from the end in a bitstring?
x &= ~(1 «_space;2)
Write a function that calculates the Hamming distance.
def hamming_distance(x, y):
difference = x ^ y count = 0 while difference != 0: count += 1 difference &= difference - 1 return count
Write a function to calculate the Hamming weight of an integer. (Kernighan method)
int countSetBits(int n) {
int count = 0;
while (n) {
n = n & (n - 1); \++count;
}
return count;
}
Write a function that calculates the Hamming weight in constant time. Divide and Conquer strategy
int countSetBits(unsigned int n) {
n = n - ((n»_space; 1) & 0x55555555);
n = (n & 0x33333333) + ((n»_space; 2) & 0x33333333);
n = (n + (n»_space; 4)) & 0x0F0F0F0F;
n = n + (n»_space; 8);
n = n + (n»_space; 16);
return n & 0x0000003F;
}
Write a function that tells you if a number is even, using bitwise operation
def is_even(x):
return x & 1 == 0
Write a function to add 2 integers using bitwise operations.
def add(a, b):
while a: c = b & a b ^= a c <<= 1 a = c return b
Write a function to get the sign of an integer
def get_sign(x):
return -(x < 0)
Write a function to calculate the absolute value of a 32-bit integer
def myabs(x):
high_bit_mask = x >> 31 return (x ^ high_bit_mask) - high_bit_mask
What is a red-black tree?
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
What is a splay tree?
A self-adjusting binary search tree where recently accessed elements are moved to the root so they are quick to access again.
What is a treap?
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
What is typical cache line size?
64 bytes. 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
What is latency?
Latency is the delay from input into a system to desired outcome. The time interval between between a stimulus and response.
What is a y-fast trie?
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.