Basic Data Structures Flashcards

1
Q

Given an array whose:
- address is 1000,
- element size is 8
- first index is 0
What is the address of the element at index 6?
a)1040
b)1005
c)1048
d)1006
e)40
f)48

A

Correct: 1000 + 8(6-0) = 1048

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

Assume you have a three-dimensional array laid out in column-major order with the first element at indices (1, 1, 1). What are the indices of the next element in memory?
a)(2,1,1)
b)(1,1,2)
c)(1,2,1)

A

(2,1,1)
Correct! In column-major ordering, the first index changes most rapidly.

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

You have an empty list, and then do the following operations:
PushBack(a)
PushFront(b)
PushBack(d)
PushFront(c)
PopBack()
What is the contents of the list now?

a) c,b,a
b) c,b,a,d
c) a,b,d

A

Correct! Here are the list contents after each operation;
PushBack(a) -> a
PushFront(b) -> b, a
PushBack(d) -> b, a, d
PushFront(c) -> c, b, a, d
PopBack() -> c, b, a

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

If we do a PopBack on a list containing the keys (in order) a, b, c, and d, we’ll execute the following part of the code:

p <- head
while p.next.next != nil:
p <- p.next

The loop will start with p pointing to the node containing key a. How many times will the body of the while loop be executed?

A

In the first test of the condition, p.next.next will be a pointer to the node containing the key c. After the first time the body of the loop is executed, p will point to the node containing the key b. In the second test of the condition, p.next.next will be a pointer to the node containing the key d. After the second time the body of the loop is executed, p will point to the node containing the key c. In the third test of the condition, p.next.next will be nil, so the body of the while loop won’t be executed any more. p will now point to the next-to-last node.
Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.

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

Given the unbalanced string “()([]”, what character is on the top of the stack when the for loop is finished?

A

The stack will contain a single value: ‘(‘, The procedure will push the initial ‘(‘, and then pop it off to match the ‘)’. It’ll then push the next ‘(‘, and the ‘[’, and then pop off the ‘[’ to match the ‘]’. What remains will the the ‘(‘.

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

What would happen if we used List.PushFront to implement Enqueue and List.TopBack and List.PopBack to implement Dequeue? Check all that apply.

a) The queue would work correctly

b) The queue wouldn’t work right

c) The queue would work correctly if the list were singly-linked with a tailed pointer

d) The queue would work correctly if the list were doubly-linked with a tail pointer

e) The queue would work correctly, buy Dequeue would be O(n) time if the list were singly-linked with a tail pointer

f) The queue would work correctly, buy Dequeue would be O(n) time if the list were doubly-linked with a tail pointer

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

Is Hugh a child of Fred?
Fred
Kate Sally Jim
Sam Hugh

A

NO. Although Hugh is a descendant of Fred, he is not a direct child. We could say that he is a grandchild.

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

If we have an expression tree and we want to evaluate that tree in order to compute the value of the expression, we want to evaluate the children before evaluating the arithmetic operator at an internal node. What traversal method does this mean we should use?
Sample expression tree representing (5-3)+(6*2).
+
- *
5 3 6 2
a) Post-order
b) Pre-order
c) In-order
d) Breadth-first

A

In a post-order traversal, we evaluate all children fully before evaluating a node itself.

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

Which of the basic data structures is the most suitable if you need to access its elements by their positions in O(1) time (this is called random access)?
a) Array
b) List
c) Queue
d) Stack

A

Array

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

Which of the basic data structures is the most suitable if you want to be able to insert elements in the middle in O(1)

a) Array
b) List
c) Queue
d) Stack

A

List. Inserting an element after an existing element in a list is O(1) , even if it is in the middle of the list.

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

Which of the basic data structures is the most suitable if you only need to insert the elements in the back and to extract elements from the front?
a) Array
c) Queue
d) Stack

A

Queue

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

Which of the basic data structures is the most suitable if you only need to implement recursion in a programming language? When you make a recursive call, you need to save the function you are currently in and its parameters values in some data structure, so that when you go out of the recursion you can restore the state. When you go out of the recursive call, you will always need to extract the last element that was put in the data structure.

a) Stack
b) Queue

A

You put the function and its parameters values on the stack when you make recursive call, and you remove the top element of the stack when you go out of the recursive call. Stack is LIFO - last in first out, so you will always extract the last element that was put on the stack.

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

Which of the basic data structures is the most suitable if you need to store the directory structure on your hard drive?

a) Array
b) List
c) Queue
d) Stack
e) Tree

A

Tree. The directory structure is a tree, so it is good to store it as a tree data structure.

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

Which is the complexity of PushFront(Key)? why?

A

O(1)

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

Which is the complexity of TopFront()? why?

A

O(1)

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

Which is the complexity of PopFront()? why?

A

O(1)

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

Which is the complexity of PushBack(Key)? why? and with the tail?

A

O(n) (no tail)
O(1) (with tail)

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

Which is the complexity of TopBack()? why?

A

O(n) (no tail)
O(1) (with tail)

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

Which is the complexity of PopBack()? why?

A

O(n)

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

Which is the complexity of Boolean Find(Key)? why?

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

Which is the complexity of Erase(Key)? why?

A

O(n)

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

Which is the complexity of Boolean Empty()? why?

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

Which is the complexity of AddBefore(Node,Key)? why?

A

O(n)

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

Which is the complexity of AddAfter(Node,Key)? why?

A

O(1)

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

Define PushFront

A

add to front

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

Define TopFront

A

Return front item

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

Define PopFront

A

remove front item

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

Define PushBack(Key)

A

add to back

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

Define TopBack()

A

return back item

30
Q

Define PopBack()

A

remove back item

31
Q

Define Find(Key)

A

is key in list?

32
Q

Define Erase(Key)

A

remove key from list

33
Q

Define Empty()

A

empty list?

34
Q

Define AddBefore(Node, Key)

A

adds key before node

35
Q

Define AddAfte(Node, Key)

A

adds key after node

36
Q

Which is the complexity of Adding at the beginning, middle and end in an array? why?

A

Add:
O(n) Beginning
O(n) Middle
O(1) End

37
Q

Which is the complexity of Remove at the beginning, middle and end in an array? why?

A

Remove:
O(n) Beginning
O(n) Middle
O(1) End

38
Q

Which is the complexity of PushFront(Key) in a Doubly-Linked list? why?

A

O(1)

39
Q

Which is the complexity of TopFront() in a Doubly-Linked list? why?

A

O(1)

40
Q

Which is the complexity of PopFront() in a Doubly-Linked list? why?

A

O(1)

41
Q

Which is the complexity of PushBack(Key) in a Doubly-Linked list? why? and with a tail?

A

O(n) (no tail)
O(1) (with tail)

42
Q

Which is the complexity of TopBack() in a Doubly-Linked list? why?

A

O(n) (no tail)
O(1) (with tail)

43
Q

Which is the complexity of PopBack() in a Doubly-Linked list? why?

A

O(1) and that is the benefit of a doubly-linked list over a singly-linked list

44
Q

Which is the complexity of Find(Key) in a Doubly-Linked list? why?

A

O(n)

45
Q

Which is the complexity of Erase(Key) in a Doubly-Linked list? why?

A

O(n)

46
Q

Which is the complexity of Empty(Key) in a Doubly-Linked list? why?

A

O(1)

47
Q

Which is the complexity of AddBefore(Node, Key) in a Doubly-Linked list? why?

A

O(1)

48
Q

Which is the complexity of AddAfter(Node, Key) in a Doubly-Linked list? why?

A

O(1)

49
Q

Define Stack

A

Abstract data type with three properties:
1-Push(Key)
2-Key top()
3-Key Pop()

50
Q

What does Push(key) do?

A

adds key to collection

51
Q

What does Key Top() do?

A

returns most recently-added key

52
Q

What does Key Pop() do?

A

removes and returns most recently-added key

53
Q

Stacks are FIFO?

A

no, is LIFO (Last In First Out)

54
Q

Complexity of Push, Pop, Top, Empty

A

O(1)

55
Q

What is the benefit of implementing a Stack with a linked-list instead of with an array?

A

There’s no wasted space, you can keep adding elements to the list. The array has a fixed amount of available memory space

56
Q

Main benefits of doubly linked-list

A

1-Constant time to insert/remove from the front
2-doubly-linked list with tail O(1) to insert/remove from the back
3- O(n) time arbitrary element
4-List of elements needs not be contiguous

57
Q

Define Queue

A

Abstract data type with the following properties:
1-Enqueue(Key)
2-Key Dequeue()
3-Boolean Empty()

58
Q

Defines Enqueue(Key)

A

Adds key to collection

59
Q

Defines Dequeue()

A

removes and returns least recently-added key

60
Q

Defines Boolean Empty()

A

are there any elements?

61
Q

Queue is FIFO?

A

First-In, First-Out

62
Q

Can a queue be implemented without a tail pointer?

A
63
Q

Complexity of Enqueue, Dequeue, Empty

A

O(1)

64
Q

Define Root of a tree

A

top node in the tree

65
Q

Define Child of a tree

A

has a line down directly from a parent

66
Q

Define Ancestor of a tree

A

Parent, or parent of parent, etc

67
Q

Define Descendant of a tree

A

child, or child of child, etc

68
Q

Define Sibling of a tree

A

sharing the same parent

69
Q

Define Leaf of a tree

A

no with no children

70
Q

Define Interior node of a tree

A

non-leaf