Coding Interview Prep (Source: Github)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

size of char in bytes

1 byte

2^10

1024

size of wchar_t in bytes

2 bytes

size of short in bytes

2 bytes, and 2 bytes commonly

2^8

256

2^9

512

size of double in bytes

8 bytes

size of long long in bytes

8 bytes

What is an AVL tree?

A BST where the height of every node and that of its sibling differ by at most 1.

example of a throughput device

GPU core

Can merge sort be done in-place?

No. It requires O(n) space. There is an in-place version?

What relationship of the keys do you lose with a hash table?

The ordering of the keys.

2^7

128

2^11

2048

2^5

32

size of long in bits

32 (at least, 32 commonly), 64 on LP64

size of float in bits

32 bits

size of int in bits

32 bits commonly, at least 16 bits

2^15

32768

size of float in bytes

4 bytes

2^32

4.294 Billion

2^6

64

4 * 16

64

size of double in bits

64 bits

size of long long in bits

64 bits

2^16

65536

2^3

8

size of bool in bits

8 bits

size of char in bits

8 bits

2^13

8192

What is a Binary Search Tree?

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.

Write merge sort in C (check answer carefully)

void merge(int numbers[], int low, int mid, int high) { // temp array for holding sorted items int b[high - low - 1]; int i = low; int j = mid + 1; int k = 0; // merge items from list in order while (i <= mid && j <= high) { if (numbers[i] <= numbers[j]) { b[k++] = numbers[i++]; } else { b[k++] = numbers[j++]; } } // copy the remaining items to tmp array while (i <= mid) b[k++] = numbers[i++]; while (j <= high) b[k++] = numbers[j++]; --k; while (k >= 0) { numbers[low + k] = b[k]; --k; } } void merge_sort(int numbers[], int low, int high) { if (low < high) { int mid = (low + high) / 2; merge_sort(numbers, low, mid); merge_sort(numbers, mid + 1, high); merge(numbers, low, mid, high); } }

Write a function that reverses a linked list, with this argument: pointer to pointer to the head node.

void reverse(node_t **head) { node_t *prev = NULL; node_t *current = *head; node_t *next = *head; while (current) { next = current->next; current->next = prev; prev = current; current = next; } *head = prev; }

What is a compressed trie?

It's a trie where the non-branching paths are compacted into a single edge.

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 parsing?

Combining tokens and groups of tokens into a tree structure (a parse tree).

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.

What is lexical analysis?

The process of dividing program text into words or tokens.

How is a deque usually implemented?

Using a Circular Array or Doubly Linked List.

How are queues usually implemented?

Using a Circular Array or Singly Linked List.

Can heap sort be done in-place?

Yes.

How would you swap 2 integers using only bitwise operations?

a ^= b b ^= a a ^= b

Output a file with line numbers.

cat -n somefile

Print columns 2, 3, and 6 from the date command.

date | awk '{print $2, $3, $6}'

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

How many levels in a complete binary tree of size n?

floor(1 + log(base2)(n))

Sed command to take a file separated by spaces, turn spaces into newlines, and then sort it alphabetically.

sed 's/ /\n/g' words.txt | sort

C or Python: Sort an array of numbers using heap sort.

void heap_sort(int* numbers, int count) { int temp; for (int i = count - 1; i > 0; --i) { temp = numbers[i]; numbers[i] = numbers[0]; numbers[0] = temp; percolate_down(numbers, i, 0); } } void heapify(int* numbers, int count) { for (int i = count / 2 - 1; i >= 0; --i) { percolate_down(numbers, count, i); } } void percolate_down(int* numbers, int count, int index) { while (index * 2 + 1 < count) { int swap_index = index; int left_child_index = index * 2 + 1; int right_child_index = index * 2 + 2; bool has_left_child = left_child_index < count; bool has_right_child = right_child_index < count; if (has_left_child && has_right_child) { if (numbers[left_child_index] > numbers[right_child_index]) { swap_index = left_child_index; } else { swap_index = right_child_index; } } else if (has_left_child) { swap_index = left_child_index; } else if (has_right_child) { swap_index = right_child_index; } else { break; } if (numbers[swap_index] > numbers[index]) { int temp = numbers[index]; numbers[index] = numbers[swap_index]; numbers[swap_index] = temp; index = swap_index; } else { break; } } }

Using an iterative approach, insert a value into a BST: insert(node*, int)

void treeInsert(bst_node* node, int key) { bst_node* new_node; new_node = malloc(sizeof(*new_node)); assert(new_node); new_node->key = key; new_node->left = 0; new_node->right = 0; while (1) { if (node->key > key) { if (node->left) { node = node->left; } else { node->left = new_node; return; } } else { if (node->right) { node = node->right; } else { node->right = new_node; return; } } } }

How would you turn OFF the 3rd bit from the end in a bitstring?

x &= ~(1 << 2);

Write a function to get the sign of an integer.

def get_sign(x): return -(x < 0)

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 that tells you if a number is even, using bitwise operation.

def is_even(x): return x & 1 == 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

How would you turn ON the 3rd bit from the end in a bitstring?

x |= (1 << 2)

Using recursion, insert a value into a tree: root = insert(node*, int)

"bst_node* insert(bst_node* node, const int value) { if (node == 0) { bst_node* new_node = malloc(sizeof(bst_node)); if (new_node == NULL) { printf(""Unable to allocate memory.""); exit(0); } new_node->value = value; new_node->left = 0; new_node->right = 0; node = new_node; return node; } if (value < node->value) { node->left = insert(node->left, value); } else if (value > node->value) { node->right = insert(node->right, value); } return node; }"

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

example of a latency device

CPU core

Describe the universal hashing function for an integer. What arguments would it need? What would it look like?

/* key = the Key a = random number from 1 to p-1 b = random number from 0 to p-1 p = a prime number >=m m = the size of the array */ int hash(int key, int a, int b, int p, int m) { return ((a * x + b) % p) % m; }

2 ^ 4

16

What is the square root of 256?

16

size of wchar_t in bits

16 bits

size of short in bits

16 bits (at least), and 16 commonly

2^14

16384

size of int in bytes

4 bytes commonly, at least 2 bytes

size of long in bytes

4 bytes, (at least 4, and commonly 4), 8 on LP64

2^12

4096

What is typical cache line size?

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

What is the Hamming Distance?

A number used to denote the number of differences between two binary strings of the same length.

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 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.

How can build heap be done in linear time?

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

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.

What is an x-fast trie?

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.

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.

What are the 5 steps of the compiling process?

Lexical Analysis Parsing Semantic Analysis Optimization Code Generation

Is heap sort stable?

No.

Is quicksort stable?

No.

Is selection sort stable?

No.

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)

What is a van Emde Boas tree?

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.

Can insertion sort be done in-place?

Yes.

Can quicksort be done in-place?

Yes.

Can selection sort be done in-place?

Yes.

Is insertion sort stable?

Yes.

Is merge sort stable?

Yes.

Using bitwise operations, how would you test that a number is a power of 2?

bool isPowerOfTwo = (x & (x - 1);

Write a method is_binary_search_tree that returns true if a given tree is a BST (use helper function).

bool is_binary_search_tree(bst_node* node) { if (node == NULL) return true; return is_between(node, INT_MIN, INT_MAX); } bool is_between(bst_node* node, int min, int max) { if (node == NULL) return true; // ensure subtrees are not hiding a value lower or higher than the subtree // allows return node->value > min && node->value < max && is_between(node->left, min, node->value) && is_between(node->right, node->value, max); }

Delete a given value from a BST rooted at given node. Returns a pointer to node.

bst_node* delete_value(bst_node* node, int value) { if (node == NULL) return node; if (value < node->value) { node->left = delete_value(node->left, value); } else if (value > node->value) { node->right = delete_value(node->right, value); } else { // found value if (node->left == NULL && node->right == NULL) { free(node); node = NULL; } else if (node->left == NULL) { bst_node* temp = node; node = node->right; free(temp); } else if (node->right == NULL) { bst_node* temp = node; node = node->left; free(temp); } else { // 2 children - get min node of right subtree int right_min = get_min(node->right); node->value = right_min; node->right = delete_value(node->right, right_min); } } return node; }

Using an recursive approach, write a function: find_node(bst_node* node, int value) that returns the node with the given target value in a BST.

bst_node* find_node(bst_node* node, int value) { if (node == NULL) return false; if (value < node->value) { return find_node(node->left, value); } else if (value > node->value) { return find_node(node->right, value); } else { return node; } }

Using an iterative approach, write a function find_node(bst_node* root, int target) that returns the node with the given target value in a BST.

bst_node* find_node(bst_node* root, int target) { while (root != NULL && root->key != target) { if (root->key > target) { root = root->left; } else { root = root->right; } } return root; }

Take a file delimited by : and make it tab-delimited.

cat /etc/passwd | sed 's/:/\t/g'

Write a MergeSort class in Python (check answer carefully)

class MergeSort(object): def __init__(self, numbers): self.values = numbers self.count = len(numbers) def sort(self): self.merge_sort(0, self.count - 1) return self.values def merge_sort(self, low, high): if low < high: mid = (low + high) // 2 self.merge_sort(low, mid) self.merge_sort(mid + 1, high) self.merge(low, mid, high) def merge(self, low, mid, high): b = [] i = low j = mid + 1 while i <= mid and j <= high: if self.values[i] <= self.values[j]: b.append(self.values[i]) i += 1 else: b.append(self.values[j]) j += 1 while i <= mid: b.append(self.values[i]) i += 1 while j <= high: b.append(self.values[j]) j += 1 for index, val in enumerate(b): self.values[low + index] = val

Write a binary search function that works iteratively, taking a target int, array of ints, and size of the array, returning the index of the found item, or -1.

int binary_search(int target, int numbers[], int size) { int low = 0; int high = size - 1; int mid = 0; while (low <= high) { mid = (high + low) / 2; if (target > numbers[mid]) { low = mid + 1; } else if (target < numbers[mid]) { high = mid - 1; } else { return mid; } } return -1; }

Write a binary search function that works recursively, returning the index of the found item, or -1.

int binary_search_recur(int target, int numbers[], int low, int high) { if (low > high) { return -1; } int mid = (high + low) / 2; if (target > numbers[mid]) { return binary_search_recur(target, numbers, mid + 1, high); } else if (target < numbers[mid]) { return binary_search_recur(target, numbers, low, mid - 1); } else { return mid; } }

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 >> 1) & 0x55555555); n = (n & 0x33333333) + ((n >> 2) & 0x33333333); n = (n + (n >> 4)) & 0x0F0F0F0F; n = n + (n >> 8); n = n + (n >> 16); return n & 0x0000003F; }

Function that returns the height (in nodes) of a BST: int get_height(bst_node* node)

int get_height(bst_node* node) { if (node == NULL) { return 0; } return 1 + max_num(get_height(node->left), get_height(node->right)); }

Get the successor of a value in a BST rooted by given node. Returns int.

int get_successor(bst_node* node, int value) { if (node == NULL) return -1; bst_node* target = node; while (target->value != value) { if (value < target->value) { target = target->left; } else if (value > target->value) { target = target->right; } } // arrived at target node if (target->right != NULL) { // get min value of right subtree return get_min(target->right); } else { // get lowest ancestor that is a left child in the path to target value bst_node* successor = NULL; bst_node* ancestor = node; while (ancestor != NULL) { if (value < ancestor->value) { successor = ancestor; ancestor = ancestor->left; } else { ancestor = ancestor->right; } } return successor->value; } }

In C or Python, Write a universal hashing function for a string, taking as arguments a string and the capacity of the hashtable.

int hash(const char* key, const int m) { int hash = 0; for (int i = 0; i < key[i] != '\0'; ++i) { hash = hash * 31 + key[i]; } return abs(hash % m); }


Set pelajaran terkait

Psychology 1000 sem. 1 exam - Reilly Huie

View Set

Section 9-2 The Krebs Cycle and Electron Transport

View Set

Swallowing Disroders in Adult Populations

View Set

chapter 5, Exam 2 Learn Smart, Chapter 5, Real Estate Ch. 5, Real Estate, Real Estate chapter 5, FIN 3070 - Final Exam, Chapter 5, RE Ch. 5, The Effects of Time and Risk on Value, ch 5, Real Estate Chapter 14 - Effects of Times and Risk on Value, RE...

View Set

Google Analytics for Beginngers (2020)

View Set

Chapter 3: Audit Planning, Tests, Materiality

View Set

32.1 World History - Hitler's Lightning War

View Set