Data Structures Flashcards

1
Q

What is direct addressing?

A

It is an alternative way to solve collision by having distinct array positions for every possible key. Direct addressing is possible only when we can afford to allocate an array that has one position for every possible key.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When I create a linked list, what should I store?

A

Typically, a pointer to its head is stored. Sometimes also a pointer to the tail.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Deque: definition, possible implementations, insert policy, remove policy, contiguity in memory, time complexity

A
  • Definition: a queue with two ends
  • Possible implementations: dynamic array, linked list
  • Insert policy: both ends
  • Remove policy: both ends
  • Contiguity in memory: yes
  • Time complexity: implementation dependent
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the search complexity in direct addressing?

A

O(1), since every key has a unique array position, searching takes a constant time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is collision in a hash table?

A

Multiple keys might have the same hash code, or two different hash codes can map to the same index. This is because we operate two mappings from higher to lower dimensional spaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a common way of mapping the hash code into an index?

A

Using the modulo function

idx = hash_code % array_length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can I add an element in a min-heap?

A

To insert an element in a min-heap, we start from the bottom and compare the new node added with its parent. If it is smaller, we swap parent and child, and continue until we find the right spot for the node we want to add.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is chaining?

A

Chaining is a technique to solve the collision problem in a hash table which is based on the use of a linked list and an array. Each element of the array is a linked list, which allows multiple elements to be mapped into the same index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a leaf in a tree?

A

A node without children is called leaf.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is primary clustering? How can I solve it?

A

Primary Clustering is the tendency for a collision resolution scheme such as linear probing to create long runs of filled slots near the hash position of keys.

I can solve it with quadratic probing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a binary tree?

A

A tree is a binary tree if all its nodes have up to two children.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is rehashing? When is it necessary?

A

When the load factor increases to more than a desired value (usually 0.75) we can perform a rehashing: the array is increased in size (double), the elements are rehashed and the load factor is therefore decreased.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the search complexity in direct addressing?

A

O(1), Since every key has a unique array position, searching takes a constant time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Draw a complete, a full and a perfect binary tree

A

DRAW…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between ordered and unordered set/map?

A

The elements in the std::map/std::set are ordered by the key, and the std::unordered_map/std::unordered_set are not ordered based on their key, but are placed into “buckets” based on a hash value computed for their key.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a perfect binary tree?

A

A complete and full binary tree.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How can I solve collisions in a hash table?

A
  • Chaining

- Open addressing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What’s the runtime complexity of removing in a linked list?

A

O(1) for the head, O(N) for the middle, O(N) for the last element (even if we have the tail pointer, because we have to go through the entire list to find the new last element)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is a singly-linked list?

A

A singly-linked list is a data structure composed of pairs {key, next}

  • Key: is the value stored by the element (e.g., an int, a float, or whatever)
  • Next: is a pointer to the next element in the list.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Is there a difference between hash code and index?

A

It is important to highlight that the hash code is NOT the index in the array. Indeed, we can potentially have much more hash codes than indices in the array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is a min-heap?

A

In a min-heap, each node is smaller than its children. It follows that the root is the smallest element of the tree.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How does a ring buffer work?

A

When an element is written, the pointer to the next element to write is increased. If the maximum capacity of the buffer is reached, then the pointer goes to the beginning of the data structure and keeps writing from there.
When an element is read, the pointer to the next element to read it increased. If the end of the buffer is reached, the pointer is moved to the begin of the buffer. Clearly, the read pointer cannot be ahead of the write pointer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is a complete binary tree?

A

A binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. In other words, it is totally filled, other than the rightmost elements of the last level.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

In simple chaining, what data structure is appropriate?

A

Linked-lists. Doubly linked lists are better because deletion becomes easier.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Binary heap: runtime complexity for search, insert, remove

A
  • Search: to look for a generic element is O(N), to look for the smallest is O(1)
  • Insert: average O(1), since the elements are added at the bottom and “bubbled-up”
  • Remove: O(log N)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is a doubly-linked list?

A

A doubly-linked list extends a singly-linked list by adding another pointer to the previous element. Therefore, each element in the list is a triplet {key, previous, next}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Map: definition, what’s composed of, insert policy, remove policy, contiguity in memory, time complexity

A
  • Definition: Sets are a type of associative containers storing pairs {key,value}. Each element is identified by its key. Keys are unique (i.e., cannot add the same key twice).
  • Possible implementations: binary search tree
  • Ordering: if ordered, then elements are sorted by value. If unordered, elements are organized in buckets based on their hash value.
  • Contiguity in memory: contiguous if unordered, otherwise do not know
  • Time complexity: logarithmic
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

How can I traverse a binary tree?

A
  • In-order: we visit the left branch, then current node, then right branch;
  • Pre-order: we visit the current node first, then left, then right;
  • Post-order: we visit the left branch, then right, then current node.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What’s the most common way to implement a hash table?

A

A common way is the use of an array of linked lists and a hash function. Given a key, we can compute its hash code, which corresponds to an index of the array. nce we know to which element of the array the key is mapped, we have access to the linked list stored in such an element and we can do the operation that we want (for example, store the pair {key,value}).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is the difference between a binary heap (e.g., a min-heap) and a binary search tree?

A

A binary heap only guarantees order among a parent and its children. For example, it guarantees that each node is smaller (min-heap) than its childeren. However no overall order is guaranteed.

In a binary search tree, all the elements in the left subtree of a node are smaller than any element in the right subtree. Therefore, a binary search tree has stronger order property than a binary heap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is a hash table?

A

A hash table is a data structure which efficiently maps keys to values. It is very efficient for lookup.

32
Q

What is the main advantage of a hash table?

A

It is very efficient for lookup.

33
Q

Stack: definition, possible implementations, insert policy, remove policy, contiguity in memory, time complexity

A
  • Definition: abstract data structure with policy of insertion and removal from top.
  • Possible implementations: dynamic array, linked list
  • Insert policy: top
  • Remove policy: top
  • Contiguity in memory: depends on implementation, yes with array stack, no with linked list stack
  • Time complexity: implementation dependent
34
Q

What can be a key in a hash table?

A

The key can be whatever object, as long as it is possible to compute an hash code out of it. Typically, strings are used for convenience, but can be any type and also custom classes. It has to be stored in the linked list together with the value because we need to be able to find what we are looking for in the linked list, which is possible having the key.

35
Q

How can I add elements in a binary search tree?

A

I compare with root, decide whether I should go left or right (left if smaller than root, right if larger), repeat this with children until find a spot.

36
Q

What important property should the hash function have?

A

A good hash function should uniformly distribute the keys among the different elements of the array.

37
Q

Set: definition, possible implementations, ordering, contiguity in memory, time complexity

A
  • Definition: Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it.
  • Possible implementations: binary search tree
  • Ordering: if ordered, then elements are sorted by value. If unordered, elements are organized in buckets based on their hash value.
  • Contiguity in memory: contiguous if unordered, otherwise do not know
  • Time complexity: logarithmic
38
Q

What is a tree?

A

A tree is a data structure composed of nodes. Each node can have zero or multiple children.
There is always a root node, which can have zero or multiple children.
Each children can have zero or multiple nodes.

39
Q

Why should you always try to have a balanced tree?

A

Because otherwise we can loose the efficiency of trees.

40
Q

Queue: definition, possible implementations, insert policy, remove policy, contiguity in memory, time complexity

A
  • Definition: a data structure with FIFO policy.
  • Possible implementations: dynamic array, linked list, two stacks
  • Insert policy: end
  • Remove policy: front
  • Contiguity in memory: no
  • Time complexity: implementation dependent
41
Q

Which methods are available to solve the collisions problem?

A

Open chaining and direct addressing.

42
Q

What is simple uniform hashing?

A

Every element has equal probability of hashing into any of the slots

43
Q

How can extract from a min-heap?

A

Extracting the minimum in a min-heap is simple, since it is always the root. However, once we extract it, we need to pay attention to how we restore the properties of the tree.

44
Q

What is a binary search tree?

A

A binary search tree is a binary tree which respects the following property:

all left descents <= n < all right descents

In other words, given a node x, it is a binary search tree if:

  • All the left subtrees contain elements lower than x;
  • All the right subtrees contain elements larger than x.
  • The left and right subtrees of x are binary search trees.
45
Q

How can I search elements in a binary search tree?

A

I can exploit the ordering property of the binary search tree to know in which direction I should go each time I visit a node, until I find it (or reach a situation where the element I look for is certainly not there).

46
Q

What is the search complexity in a binary search tree?

A

Searching in a binary search tree has complexity O(h), where h is the height of the tree, since in the worst case we have to reach the leaf (which are at distance h from the root).

47
Q

What’s the main advantage of doubly-linked lists against singly-linked?

A

We can go through the list in both directions. This makes the pop in the back constant time O(1).

48
Q

Dynamic array: definition, what’s composed of, insert policy, remove policy, contiguity in memory, time complexity

A
  • Definition: an array whose size can be changed. To change it, needs to be reallocated and the elements are copied over.
  • What’s composed of: potentially anything.
  • Insert policy: anywhere. Insert at begin and middle has complexity O(n), at the end can be O(1) or O(n)
  • Remove policy: anywhere. Remove at begin and middle has complexity O(n), at the end can be O(1) or O(n)
  • Contiguity in memory: contiguous
49
Q

What’s the runtime complexity of adding in a linked list?

A

O(1) for the head, O(N) for the middle, O(1) or O(N) for the last element

50
Q

What is a binary heap?

A

A binary heap is a complete binary tree where each node is smaller/larger than its children.

51
Q

Can multiple keys be mapped into the same index by the hash function of a hash table?

A

Yes, potentially infinite keys can be mapped into the same hash code and into the same index.

52
Q

What is a hash function?

A

The hash function takes an input (for example a string), produces an integer and remaps that integer into an index of the array.

53
Q

Why are binary search trees good for searching elements?

A

Binary search trees respect a given ordering property, namely all the elements in the left sub-tree of a node are smaller than the node, and all the elements in the right sub-tree are larger than the node. Therefore, we have a pretty good clue of which direction to follow to search for a number: if it is larger than the current node, we move right, if it is smaller, we move left.

54
Q

Can there be cycles in a tree?

A

No

55
Q

What is a ring buffer?

A

A ring buffer (or circular buffer) is a data structure which has fixed size and is connected end-to-end.

56
Q

How can I restore the min-heap after extracting?

A

We can do as follows:

  • Take the bottommost, rightmost (last) element of the tree and place it as root;
  • Compare it with its two children and swap it with the smallest one;
  • Repeat this procedure until the min-heap property is restored.
57
Q

What’s the runtime of a hash table?

A

If the number of collisions is very high, the worst case runtime is O(N), where N is the number of keys. However, lookup can be kept O(1) with a good implementation able to keep the number of collisions to the minimum.

58
Q

List: definition, possible implementations, insert policy, remove policy, contiguity in memory, time complexity

A
  • Definition: Lists are sequence containers that allow non-contiguous memory allocation.
  • Possible implementations: dynamic array, linked list
  • Insert policy: both ends
  • Remove policy: both ends
  • Contiguity in memory: depends on implementation, yes with array lists, no with linked lists
  • Time complexity: implementation dependent
59
Q

When is a ring buffer considered empty?

A

When the write and read pointers point to the same element.

60
Q

When is it appropriate to use direct addressing?

A

When the universe U of keys is reasonably small. Since each key is associated with a slot in the array, it is better to use direct addressing when the universe of keys is small as the array size grows with the increase in number of keys.

61
Q

What is the search time complexity in a binary search tree?

A

O(h), where h is the height of the tree.

62
Q

What probing techniques are available?

A
  • Linear probing: we linearly search through the array until we find a free slot;
  • We look every n position (e.g., every 2, every 3, etc…()
  • Quadratic probing: we search through the array at distances from the initial slot that increase quadratically (e.g., we first look 1 slot ahead, then 2, then 4, then 8, then 16 and so on). This allows us to solve primary clustering problem, which happens when using linear probing several keys are added contiguously. This could result in a non-uniform distribution of the keys, causing problems.
63
Q

Binary search tree: runtime complexity for search, insert, remove

A
  • Search: to look for a generic element is O(log N), to look for the smallest is O(1)
  • Insert: O(h), since we start from top and worst case we reach the height of the tree
  • Remove: O(log N)
64
Q

What’s the runtime complexity of accessing a linked list?

A

O(1) for the head, O(N) for the middle, O(1) or O(N) for the last element

65
Q

What is probing?

A

Probing is a technique to search for an available slot for a given key in direct addressing.

66
Q

How many pointers to I have in a ring buffer?

A

Two: write and read.

67
Q

What is a full binary tree?

A

A binary tree in which all the nodes have either zero or two children, but none has one child.

68
Q

Provide some examples of hash functions

A
  • Strings: we compute the ASCII code for character, sum them up, and compute the modulo with the size of the array;
  • Integers: we split the integers into chunks of 2 numbers, sum them up, and divide them by some constant (e.g., again the array size).
69
Q

When is a ring buffer considered full?

A

When the write pointer is one element behind the write array (e.g., the next write will overwrite the element to be read if this is not read before);

70
Q

What is the policy of a ring buffer?

A

It has a FIFO policy (therefore, it is similar to a queue from this perspective).

71
Q

What is the add time complexity in a binary search tree?

A

O(h), where h is the height of the tree.

72
Q

What is the difference between set and map

A

Set is used to store only keys, while map is used to store key value pairs.

73
Q

In simple uniform hashing, what is the search complexity?

A

O(1), because there are two cases, once when the search is successful and when it is unsuccessful, but in both the cases, the complexity is O(1+alpha) where 1 is to compute the hash function and alpha is the load factor.

74
Q

How can I remove elements in a binary search tree?

A

Search for the element going through the tree, and then remove it.

75
Q

Functions defined on a linked-list

A

The following functions are typically provided:
- push_front(), top_front(),
pop_front(): to add, visualize and remove the front element;
- push_back(), top_back(), pop_back(): to add, visualize and remove the last element;
- find(key): to find a given key;
- remove(key): to remove a key;
- empty(): to know whether the list is empty or not;
- add_before(), add_after(): to add a key before/after a given element in the list.

76
Q

What is the load factor?

A

Load factor is the average number of elements stored in a chain, and is given by the ratio of number of elements stored to the number of slots in the array.

77
Q

What is the remove time complexity in a binary search tree?

A

O(h), where h is the height of the tree.