comp 2503 midterm Flashcards

1
Q

What is computational complexity?

A

The amount of resources (time and space) required by an algorithm to solve a problem as a function of input size

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

What is Big-O notation used for?

A

To describe the upper bound or worst-case growth rate of an algorithm’s resource usage

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

What is the difference between LIFO and FIFO?

A

LIFO (Last In First Out) is used by stacks where the last element added is the first removed. FIFO (First In First Out) is used by queues where the first element added is the first removed

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

What are the main operations of a Stack?

A

push (add to top), pop (remove from top), peek (view top without removing), isEmpty

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

What are the main operations of a Queue?

A

enqueue (add to rear), dequeue (remove from front), peek (view front without removing), isEmpty

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

What makes Binary Search more efficient than Linear Search?

A

Binary Search divides the search space in half each time (O(log n)), while Linear Search checks each element sequentially (O(n))

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

What is the purpose of generics in Java?

A

To enable classes to work with different data types while maintaining type safety at compile time

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

What is the Comparable interface used for?

A

To define a natural ordering for a class by implementing the compareTo method

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

What is the Comparator interface used for?

A

To define multiple different ways to compare objects of a class without modifying the class itself

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

What are the key differences between arrays and linked lists?

A

Arrays have fixed size and continuous memory with direct access, while linked lists have dynamic size, scattered memory, and sequential access

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

What is the time complexity of adding to the front of an array vs. linked list?

A

Array: O(n) due to shifting elements, Linked List: O(1) by changing the head pointer

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

What is the time complexity of accessing an element by index in array vs. linked list?

A

Array: O(1) direct access, Linked List: O(n) must traverse from start

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

What are traversal pointers in linked lists used for?

A

To move through the list one node at a time, often used to search, modify, or perform operations on nodes

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

What is the difference between Singly and Doubly Linked Lists?

A

Singly Linked Lists have nodes with one pointer to the next node, Doubly Linked Lists have nodes with pointers to both next and previous nodes

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

Why use a Doubly Linked List over a Singly Linked List?

A

Doubly Linked Lists allow backward traversal and easier deletion of nodes, but use more memory per node

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

What is the advantage of implementing a Stack with a linked list vs. array?

A

Linked list implementation can grow dynamically without needing to resize, while array implementation may need to resize

17
Q

What problem can be solved using two stacks?

A

Evaluating arithmetic expressions, implementing undo/redo functionality, or validating matching parentheses

18
Q

What is the key difference in memory usage between arrays and linked lists?

A

Arrays use contiguous memory blocks and have memory overhead for resizing, linked lists use scattered memory with overhead for node pointers

19
Q

How does Java Collections sort method work with custom objects?

A

Objects must either implement Comparable interface or a separate Comparator must be provided

20
Q

What is standard input/output in Java?

A

System.in for input stream (keyboard by default) and System.out for output stream (console by default)

21
Q

In what scenarios would you choose an array over a linked list?

A

When you need constant-time access to elements, fixed size is acceptable, and frequent insertion/deletion at arbitrary positions isn’t needed

22
Q

What is a moving reference in linked list traversal?

A

A temporary pointer that moves through the list to access or modify nodes without losing the reference to the head of the list

23
Q

How does a circular array implementation of a queue work?

A

Uses modulo arithmetic to wrap around array indices, making efficient use of space by reusing empty positions at the start

24
Q

What are the advantages of implementing a queue using a linked list?

A

No size limitation, efficient enqueue and dequeue operations (O(1)), no need to shift elements

25
Q

How do you handle edge cases in linked list operations?

A

Check for null pointers, empty lists, single-element lists, and maintain proper links when adding/removing at start/end of list