Week 7 Flashcards

1
Q

What is a data structure?

A

A data structure is a way of organizing, managing, and storing data to enable efficient access and modification.

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

How are data structures classified? Data structures are classified into:

A

Primitive (e.g., int, char)
Non-primitive (e.g., arrays, linked lists)
Linear (e.g., arrays, stacks, queues)
Non-linear (e.g., trees, graphs)

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

What is the difference between linear and non-linear data structures?

A

Linear data structures store data in a sequential manner (e.g., arrays, lists).
Non-linear data structures organize data hierarchically or with complex relationships (e.g., trees, graphs).

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

What is the Java Collections Framework?

A

A set of classes and interfaces in Java that provide data structures and algorithms for storing and manipulating groups of objects efficiently.

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

What is the root interface of the Java Collections Framework?

A

The Collection interface is the root interface for working with groups of objects.

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

Name two subinterfaces of the Collection interface.

A

List
Set

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

How does the Collection interface differ from the Map interface?

A

Collection stores individual elements.
Map stores key-value pairs.

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

What are generics in Java?

A

Generics allow for type-safe data structures and methods by letting you specify the type of objects a class or method can work with.

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

Why are generics useful in Java?

A

They provide type safety, reduce casting, and enable code reusability.

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

How do you define a generic class?

A

class GenericClass<T> {
private T value;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}</T>

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

What is type erasure in generics?

A

Type erasure is the process by which the generic type information is removed at runtime, and the generic type is replaced by Object or bound types during compilation.

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

What is the Set interface in Java?

A

The Set interface is a collection that cannot contain duplicate elements.

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

What is the difference between Set and List?

A

Set does not allow duplicates, and there is no guarantee of order.
List allows duplicates and maintains insertion order.

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

Name two classes that implement the Set interface.

A

HashSet
TreeSet

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

How does Set ensure uniqueness of elements?

A

By using either hashCode() and equals() (in HashSet) or by comparing elements using their natural ordering or a comparator (in TreeSet).

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

What is a HashSet in Java?

A

A HashSet is an implementation of the Set interface that uses a hash table for storing elements, ensuring constant-time performance for basic operations.

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

How does HashSet determine the uniqueness of elements?

A

It uses the hashCode() and equals() methods of objects to ensure uniqueness.

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

What is a TreeSet in Java?

A

A TreeSet is a sorted Set implementation that stores elements in a binary search tree (usually a Red-Black tree).

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

How is a TreeSet different from a HashSet?

A

A TreeSet maintains the elements in sorted order, whereas HashSet does not guarantee any order.

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

Which sorting order does a TreeSet maintain?

A

The natural ordering of elements, or a custom ordering defined by a Comparator.

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

What is the Queue interface in Java?

A

A Queue is a collection used to hold elements prior to processing, typically following FIFO (First-In-First-Out) order.

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

How does a Queue differ from a Stack?

A

Queue: Follows FIFO (First-In-First-Out).
Stack: Follows LIFO (Last-In-First-Out).

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

What are two classes that implement the Queue interface?

A

LinkedList
PriorityQueue

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

What is a PriorityQueue in Java?

A

A PriorityQueue is a type of queue where elements are ordered based on their priority rather than the insertion order.

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

How does a PriorityQueue determine priority?

A

By using the natural ordering of elements or a comparator provided at the queue’s creation.

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

Does a PriorityQueue maintain the insertion order?

A

No, it orders elements based on their priority.

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

What is the Map interface in Java?

A

The Map interface represents a collection of key-value pairs where each key maps to one value.

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

How does a Map differ from a Set?

A

Map holds key-value pairs.
Set holds individual elements without duplicates.

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

What are two commonly used classes that implement the Map interface?

A

HashMap
TreeMap

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

What is a HashMap in Java?

A

A HashMap is an implementation of the Map interface that stores key-value pairs in a hash table, providing constant-time performance for basic operations.

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

What is the difference between HashMap and Hashtable?

A

HashMap is not synchronized, while Hashtable is synchronized.
HashMap allows null keys and values, while Hashtable does not.

26
Q

Is HashMap synchronized by default?

A

No, HashMap is not synchronized by default.

27
Q

How does Hashtable ensure thread safety?

A

All methods in Hashtable are synchronized, meaning that only one thread can modify it at a time.

28
Q

What is an Iterator in Java?

A

An Iterator is an object that allows you to traverse through a collection sequentially.

29
Q

How does the Iterator interface differ from the ListIterator?

A

Iterator can only traverse in one direction (forward).
ListIterator can traverse both forward and backward and supports additional operations like adding elements.

30
Q

What are the three primary methods of the Iterator interface?

A

hasNext()
next()
remove()

31
Q

What is an ArrayList in Java?

A

An ArrayList is a resizable array implementation of the List interface.

32
Q

What is the difference between ArrayList and LinkedList?

A

ArrayList is backed by a dynamic array.
LinkedList is backed by a doubly linked list.

33
Q

Which data structure is more efficient for frequent inserts, ArrayList or LinkedList?

A

LinkedList is more efficient for frequent inserts, especially in the middle of the list.

34
Q

What is the Stack class in Java?

A

A Stack is a subclass of Vector that implements the LIFO (Last-In-First-Out) data structure.

34
Q

What is the difference between Stack and Queue?

A

Stack: LIFO (Last-In-First-Out).
Queue: FIFO (First-In-First-Out).

35
Q

How is a Vector different from an ArrayList?

A

Vector is synchronized, while ArrayList is not.
Vector grows by doubling its size, while ArrayList grows by half of its size.

36
Q

What is the Comparator interface used for in Java?

A

The Comparator interface is used to define custom sorting rules for objects.

36
Q

How does the Comparator interface differ from the Comparable interface?

A

Comparable is used to define the natural order of objects.
Comparator allows you to define external, custom sorting orders.

36
Q

How do you implement a custom sorting order using Comparator?

A

You implement the compare() method in the Comparator interface:

class CustomComparator implements Comparator<Person> {
public int compare(Person p1, Person p2) {
return p1.getName().compareTo(p2.getName());
}
}</Person>

36
Q

What is the Comparable interface in Java?

A

An interface that imposes a natural ordering on the objects of a class.

37
Q

What method must be implemented when a class implements Comparable?

A

The compareTo() method.

38
Q

How does Comparable affect sorting?

A

Classes that implement Comparable can be sorted automatically by collections that support ordering (e.g., TreeSet or Collections.sort()).

39
Q

What is the Factory Design Pattern?

A

A creational design pattern that provides an interface for creating objects, but allows subclasses to alter the type of objects that will be created.

40
Q

How does the Factory Design Pattern promote loose coupling?

A

By delegating the responsibility of object creation to factory classes, which allows the client code to depend on interfaces rather than specific implementations.

41
Q

What is the Singleton Design Pattern?

A

A design pattern that ensures that a class has only one instance and provides a global point of access to that instance.

42
Q

What are the benefits of using the Singleton Pattern?

A

Controlled access to the sole instance.
Reduction of memory usage.
Prevents the creation of unnecessary objects.

43
Q

How do you implement the Singleton Pattern in Java?

A

public class Singleton {
private static Singleton instance = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return instance;
}
}

44
Q

What is an algorithm?

A

A step-by-step procedure or formula for solving a problem.

45
Q

What are the characteristics of a good algorithm?

A

Correctness
Efficiency (in terms of time and space)
Simplicity
Definiteness

45
Q

What is recursion in the context of algorithms?

A

Recursion is when a function calls itself to solve a problem.

46
Q

Give an example of a recursive algorithm. The calculation of the factorial of a number:

A

int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}

47
Q

What is a greedy algorithm?

A

An algorithm that makes the locally optimal choice at each step with the hope of finding the global optimum.

48
Q

Give an example of a problem that can be solved using a greedy algorithm.

A

The coin change problem, where the goal is to make change for a given amount using the fewest number of coins.

49
Q

What is the divide and conquer algorithm approach?

A

It is an approach where a problem is divided into smaller subproblems, solved recursively, and then combined to form the solution to the original problem.

50
Q

How does merge sort implement the divide and conquer approach?

A

Merge sort divides the array into two halves, recursively sorts each half, and then merges the sorted halves together.

51
Q

What is a backtracking algorithm?

A

A technique for solving problems by trying out different solutions and undoing (“backtracking”) if a solution does not work, then moving on to the next possibility.

52
Q

Provide an example where backtracking is used.

A

Solving the N-Queens problem, where the goal is to place N queens on an NxN chessboard so that no two queens attack each other.

53
Q

What is time complexity in algorithms?

A

Time complexity is a measure of how the runtime of an algorithm grows as the size of the input increases.

54
Q

What is the difference between Big O, Big Omega, and Big Theta notations?

A

Big O: Upper bound of the time complexity (worst case).
Big Omega: Lower bound (best case).
Big Theta: Exact bound (average case).

55
Q

What is linear search?

A

A search algorithm that checks each element of a list sequentially to find a target value.

56
Q

What is the time complexity of linear search?

A

The time complexity is O(n), where n is the number of elements in the list.

57
Q

What is binary search?

A

A search algorithm that finds the position of a target value within a sorted array by repeatedly dividing the search interval in half.

58
Q

What is the prerequisite for using binary search?

A

The array must be sorted.

59
Q

What is the time complexity of binary search?

A

The time complexity is O(log n).

60
Q

What is bubble sort?

A

A simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.

61
Q

What is the worst-case time complexity of bubble sort?

A

The worst-case time complexity is O(n²)

62
Q

What is merge sort?

A

A sorting algorithm that divides the array into two halves, recursively sorts each half, and then merges the sorted halves.

63
Q

What is the time complexity of merge sort?

A

The time complexity is O(n log n).

64
Q

How does merge sort differ from quick sort?

A

Merge sort: Divides the array into equal halves and merges sorted subarrays; stable but requires extra space.

Quick sort: Partitions the array around a pivot; can be faster in practice but is unstable and has worse worst-case time complexity.