Deck 1 Flashcards

(51 cards)

1
Q

What is data structure?

A

A systematic way of organizing and accessing data

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

What is an algorithm?

A

A step-by-step procedure for performing some task in finite time

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

How do you perform a linear search?

A

Given a value x, compare it to each element of L until an element L[i] of value x is found
or
x is compared against all elements in L and all of them are different from x

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

What is the pseudo-code for a linear search?

A
Algorithm LinearSearch(L, n, x)
Input: array L of size n and value x
Output:
• position i, 0 ≤ i < n, such that L[i] = x,
if x ∈ L, or • −1, if x !∈ L
i ← 0
while (i < n) and (L[i] != x) do i ← i + 1
if i = n then return −1
else return i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you perform a binary search?

A

Compare x with any name y located near the middle of the phone book. If x = y search ends.
If x precedes y in lexicographic order, continue search for x on the first half of the phone book
Otherwise, continue the search for x on the second half of phone book

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

What is the pseudo-code for binary search?

A
Binary Search
Algorithm BinarySearch(L, x, first, last)
Input: array L of size n and value x
Output:
• position i, 0 ≤ i < n, such that L[i] = x,
if x ∈ L, or • −1, if x 6∈ L
if first > last then return −1
else {
mid ← ⌊(first + last)/2⌋
if x = L[mid] then return mid
else if x < L[mid] then
return BinarySearch(L, x, first,mid−1)
else
return BinarySearch(L, x,mid+1, last)
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the complexity of an algorithm?

A

The complexity of an algorithm is the amount of computer resources it uses.

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

What type of computer resources are we interested in, in terms of the complexity of an algorithm?

A

Time and Memory

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

What is space complexity?

A

amount of memory the algorithm needs

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

What is time complexity?

A

amount of time needed by the algorithm to complete?

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

What is the best case?

A

Least amount of time needed by algorithm to solve an instance of the problem of size n

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

What is the worst case?

A

Largest amount of time needed by the algorithm to solve an instance of the problem of size n.

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

What is the average case?

A
time to solve instance 1 of size n +
time to solve instance 2 of size n +
...
time to solve last instance of size n
number of instances of size n
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What mathematical notation is used to express time complexities?

A

Asymptotic notations

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

How is f(n) is O(g(n)) read as?

A

f(n) is the big-Oh of g(n) if there is a real constant c > 0 and an integer constant n0 >= 1 such that f(n) = n0

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

What are the basic operations that an algorithm performs?

A

Assignment, comparison, return, boolean, and arithmetic operations

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

Why do we focus on the worst case running time?

A

Easier to analyze, average case difficult to determine, crucial to applications such as games, finance and robotics

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

What method can you use to get an accurate measure of running time?

A

System.currentTimeMillis()

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

What is Pseudo-code?

A

High-level description of an algorithm
More structured than English prose
less detailed than a program
Hides program design issues

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

What are the two parts of a recursive algorithm

A

A base case

A recurisve part

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

What are abstract data types? How parts do they have?

A

They are user defined data types. There are 2 parts:

  1. A name or type, specifying a set of data
  2. Descriptions of all the operations that do things with that type (e.g. find, insert, remove)
22
Q

Why are ADTs the preferred way of designing and implementing data structures?

A

Because of it’s ability to hide information and re-usability

23
Q

Explain Information hiding in ADTs

A

User data structure should not need to know details of its implementation.
We should be able to change implementation without affecting applications that use it

24
Q

Explain why re-usability in ADTs is important

A

If data structure is useful for one application, it is probably useful for another.

25
How do you declare class of new exceptions?
Make the class a subclass of Exception.
26
Write an exception example class:
``` class ExampleException extends Exception{ public ExampleException(String message){ super(message); } } ```
27
What is a dictionary ADT?
a dictionary ADT models a searchable collection of key-element entries
28
How can we implement a dictionary?
We implement a dictionary using an unsorted list.
29
What is the time complexity in a recursive case?
The algorithm performs a constant number c of operations plus the operations performed in the recursive calls
30
What is a hashtable?
A hash table consists of hash function h and an array (table) of size N. Goal of hash table is to store item (k, o) at index i = h(k)
31
What is a hash function and what is it's purpose
It is a formula that is supposed to disperse the keys in an apparently random way
32
When do collisions occur?
Collisions occur when different elements are mapped to the same cell
33
What is separate chaining?
Separate chaining is when each cell in the table points to a linked list of entries
34
What is linear probing?
Linear probing is when the colliding item is placed in a different cell of the table. Each table cell inspected is a probe.
35
How to handle insertions and deletions in linear probing?
We introduce a special object called Available which replaces deleted elements
36
What is double hashing?
Double hashing uses a secondary hash function d(k) and handles collisions by placing an item in the first available cell of the series.
37
What is the running time in worst case for searches insertions and removals on a hashtable?
O(n)
38
What is the expected running time of all dictionary ADT operations in a hash table?
O(1)
39
What are some applications of hash tables?
small databases, compilers, and browser caches
40
What is the average running time of separate chaining?
1 + a
41
What is the average running time of linear probing?
1/2 + 1/(2(1 - a)^2)
42
What is the average running time of double hashing?
1/(1-a)
43
What is a tree?
A tree is an abstract model of hierarchical structure consisting of nodes with a parent-child relation
44
What is a traversal?
Visits nodes of a tree in a systematic manner
45
What is a preorder traversal?
A preorder traversal is when a node is visited before its descendants
46
What is a post order traversal?
A node is visited after its descendents
47
What is a binary tree
a tree with two children. Children of a node are an ordered pair
48
What is an inorder traversal
A traversal where a node is visited after its left subtree and before its right subtree
49
What is a binary search tree
A binary search tree is a binary tree storing keys at its internal nodes.
50
What is the tree structure of a binary search tree?
left node is smaller than parent node which is smaller than right node
51
How does one search for a key k in a binary search tree?
Start at the root, if key being searched is smaller than root, (recursively) search left node, otherwise search right node,