2 - Abstract Data Types, Dynamic Arrays, Amortized Analysis Flashcards

1
Q

What is abstraction? Give three examples of abstraction from real life.

A

Abstraction is the process of trying to identify the most important qualities of an object or model. (e.g. table of contents, menu, class schedule)

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

What is information hiding? How is it related to abstraction?

A

Information hiding is purposely not including some information as a way of controlling complexity.

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

How is encapsulation related to abstraction? Explain how a function can be viewed as a type of encapsulation. What information is being hidden or abstracted away?

A

Encapsulation is placing items into a unit, or capsule. The outside is the task being performed, and the inside is how that task is performed.

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

What makes an ADT description abstract? How is it different from a function signature or an interface?

A

The description just provides a metaphor for how it behaves.

An interface, on the other hand, lists out exactly the functions that a file may have.

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

Come up with another example from everyday life that illustrates the behavior of each of the six classic abstractions (bag, stack, queue, deque, priority queue, map).

A

Bag: Adding a value, asking if a value is there, and removing a value. (like box of candy)

Set: In addition to bag operations, no element may appear more than once. (like a VIP party)

Stack: LIFO, remembers the order (like an ordered bus).

Queue: FIFO, remembers the order (like a line)

Deque: Insert/remove from either end, but not the middle (like a bus with two doors).

Priority queue: maintains value in order of importance. (like triage)

Map: maintains pairs of elements (each unique key is matched to a corresponding value).

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

What collection is important, and why? Is order important? Is time of insertion important?

  1. names of students enrollled
  2. files being held until can be printed on printer
  3. URLs for recently visited web pages in browser
  4. names of patients waiting for operating room in ER
  5. names and associated employee records in company database
A
  1. Set - don’t care about order, but can only enroll once
  2. Queue - first in, first out
  3. Stack - want the most recent to be on top
  4. Priority queue - make sure the most urgent is taken care of first
  5. Map - must have unique key corresponding to that employee
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In what ways is a set similar to a bag? In what ways are they different?

A

Both are simply unordered lists that allow you to see whether something is there.

They are different in that a bag can contain the SAME thing, while a set must contain unique things.

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

In what ways is a priority queue similar to a queue? In what ways are they different?

A

Both place some priority on time as a negative factor.

However, for a queue, relative time is the only factor. For a priority queue, there may be other factors.

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

What is a dynamic array?

A

A dynamic array is one that automatically resizes as elements are added on to ensure that the array does not overflow.

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

What does the term capacity refer to in a dynamic array? What does the term size refer to?

A

The capacity is how much memory is currently allocated, but the size is how much of that memory is actually storing information.

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

Can you describe the set of legitimate subscript positions in a dynamic array? Is this different from the set of legal positions in an ordinary array?

A

In a dynamic array, the legitimate subscript positions are only ones that are within the size or one after the array.

In a normal array, you can’t just add onto the end of the array.

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

How does the add function in a dynamic array respond if the user enters more values than the current capacity of the array?

A

The add function doubles the size of the array.

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

Suppose the user enters 17 numbers into a dynamic array that was initialized with a capacity of 5? What will be the final length of the data array? How many times will it have been expanded?

A

Once you add onto a 5-sized array, it’ll double into a 10. Once you add onto a 10-sized array, it’ll double into a 20-sized array.

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

What is the algorithmic execution time for the _dyArrayDoubleCapcity, if n represents the size of the final data array?

A

First, a new array is created (1). Next, the old values are copied into the new array (n/2). Next, the free statement releases old memory (1). Finally, the pointer is changed to reference the array. (1).

Overall, it’s just n.

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

Based on your answer to the previous question, what is the worst case algorithmic complexity of the function dyArrayAdd?

A

Since there’s only one for loop, the copying of the array, it would be O(n).

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

What are the defining characteristics of the stack abstraction?

A
  1. Push(new Entry) - places new element on the top
  2. Pop () - removes the topmost item
  3. Top() - returns, but does not remove, the topmost item
  4. isEmpty() - determines whether the stack is empty
17
Q

Explain the meaning of the term LIFO. Explain why FILO might have been equally appropriate.

A

LIFO means last in, first out. This just refers to the top of stack being the last “in”, but the first to leave.

It’s the same as FILO, or first in, last out, which refers to the bottom of the stack instead.

18
Q

Give some examples of stacks found in real life.

A

Back and forward buttons in a web browser, buffered character input (backspace).

19
Q

Explain how the use of an activation record stack simplifies the allocation of memory for program variables. Explain how an activation record stack makes it possible to perform memory allocation for recursive problems.

A

For example, if you have a recursive function, you need an activation record every time the function p is created so you know what is being passed and when finally it pops back, so that you can recover each space.

20
Q

Explain the following postfix polish expression:

a. 2 3 + 5 9 - *
b. 2 3 5 9 + - *

A

a. (2+3) pushed back onto stack, then (5-9) pushed onto stack, so * pops both off and 5 * -4 = -20.
b. (5+9)=14 pushed back onto stack, then (3-14) = -11 pushed back onto stack, then 2*-11 = -22.

21
Q

How is the memory representation of a linked list different from that of a Dynamic Array?

A

The dynamic array uses a large block of memory (much less common, but much more work to be performed).

The linked list allocates a new link EVERY time, but it’s only a single element.

22
Q

What information is stored in a link?

A

Each link points to the next node, which contains a data value and a link to the following node.

23
Q

How do you access the first element in a linked list? How would you get to the second element?

A

The first element in a linked list is just the head of the list. You would following the link in the head to get to the second element in the list.

24
Q

In the last link, what value is stored in the next field?

A

Nothing - it’s a null pointer.

25
Q

What is the Big-Oh complexity of pushing a value on the front of a linked list stack? Of popping?

A

It would just be O(1), since you just need to point the new head’s pointer to the old head’s address. Popping would also be O(1), since you just need to increment the head pointer to the next and free the previously first node.

26
Q

What would eventually happen if the pop function did not free the first link in the list?

A

There would be a memory leak.

27
Q

Suppose you wanted to test your linked list class. What would be some boundary value test cases? Develop a test harness program and execute your code with these test cases.

A

You should add 5 nodes, then try to delete the head node and the back node?

28
Q

What features characterize the Bag data type?

A
  1. Add(newElement) - place value into the bag
  2. Remove(element) - remove the value
  3. Contains(element) - return true if element is in collection
  4. Size() - return number of values in collection
  5. Iterator() - returns an iterator used to loop over collection
29
Q

How is a set different from a bag?

A

Two important differences between set and bag:

  1. elements in a set MUST be unique (cannot add if already there).
  2. set adds a number of operations that combine two sets (set union, intersection, set difference, subset of the other).

Some implementations allow elements to be repeated (called a multiset).

30
Q

When a dynamic array is used as a bag, how is the add operation different from operations you have already implemented in the dynamic array stack?

A

In a dynamic array, the add operation has the potential to need expansion of the top of the stack.

31
Q

Using a dynamic array as the implementation technique, what is the Big-O algorithmic execution time for each of the bag operations? Is this different if you use a linked list as the implementation technique?

A

For a dynamic array, the time complexity is O(1) for all operations, although the add operation is O(1) on average (b/c you’ll be doubling log n times, copying O(n) overall).

If a linked list is used, then the only major difference is returning the size, which would require traversing the entire linked list.