Data Structure And Algorithm Flashcards

1
Q

What is an array in C++?

A

A collection of elements of the same type, stored in contiguous memory locations.

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

What is the starting index of an array in C++?

A

0

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

How is the size of an array determined in C++?

A

At compile time (static size).

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

What happens if you access an out-of-bounds index in a C++ array?

A

It can cause undefined behavior.

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

How do you access an element in an array?

A

Using the syntax array_name[index].

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

What is a pointer in C++?

A

A variable that stores the memory address of another variable.

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

What symbol is used to declare and dereference a pointer?

A

*.

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

What symbol is used to get the address of a variable?

A

&.

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

Can a pointer point to any data type in C++?

A

Yes, it can point to any data type (e.g., int, char).

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

What is the purpose of the dereferencing operator (*) in pointers?

A

To access the value stored at the memory address held by the pointer.

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

What is a null pointer?

A

A pointer initialized to nullptr or NULL that doesn’t point to any valid address.

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

What is pointer arithmetic?

A

Operations like ptr++ to navigate through array elements.

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

What is recursion?

A

A technique in which a function calls itself to solve a problem.

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

Why is a base case important in recursion?

A

To avoid infinite recursion.

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

What are some common problems solved using recursive algorithms?

A

Factorial, Fibonacci, and tree traversals.

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

What is a base case in recursion?

A

The condition under which the recursion ends.

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

What is a recursive case?

A

The part of the function where the function calls itself with a reduced problem size.

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

What does the factorial function return when n is 0?

A

1, which is the base case.

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

What happens when recursion goes too deep?

A

It can exhaust the stack memory, leading to a Stack Overflow.

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

What principle does a stack follow?

A

Last In First Out (LIFO).

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

What operation adds an element to the top of the stack?

A

Push.

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

What operation removes the top element from the stack?

A

Pop.

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

What does the ‘Top’ operation do in a stack?

A

Returns the top element without removing it.

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

What is a key limitation of a stack implemented using arrays?

A

It has a fixed size.

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

What does the constructor in the Stack class initialize?

A

It initializes the top to -1.

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

What message is displayed when a stack overflow occurs?

A

“Stack Overflow”.

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

What message is displayed when a stack underflow occurs?

A

“Stack Underflow”.

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

What does the peek function return if the stack is empty?

A

-1 and displays “Stack is empty”.

29
Q

What does the ‘Top’ refer to in a stack?

A

The current index of the last element inserted in the stack.

30
Q

What happens during a stack overflow?

A

Trying to push an element when the stack is full.

31
Q

What is stack underflow?

A

Trying to pop an element when the stack is empty.

32
Q

What principle does a queue follow?

A

First In First Out (FIFO).

33
Q

What operation adds an element to the queue?

A

Enqueue.

34
Q

What operation removes an element from the queue?

A

Dequeue.

35
Q

What do ‘Front’ and ‘Rear’ indicate in a queue?

A

The current front and rear positions in the array.

36
Q

What is a limitation of the array implementation of a queue?

A

It has fixed capacity and requires checking for overflow.

37
Q

What do ‘front’ and ‘rear’ indicate in a queue?

A

They indicate the beginning and end of the queue, respectively.

38
Q

What does the function ‘isFull()’ check?

A

It checks if the queue is full by comparing ‘rear’ to 4.

39
Q

What condition does ‘isEmpty()’ check for?

A

It checks if the queue is empty by verifying if ‘front’ is -1 or greater than ‘rear’.

40
Q

What happens when ‘enqueue’ is called on a full queue?

A

It outputs ‘Queue Overflow’.

41
Q

What does the ‘dequeue()’ function do?

A

It removes and returns the front element of the queue, or outputs ‘Queue Underflow’ if the queue is empty.

42
Q

What does the ‘peek()’ function return?

A

It returns the front element of the queue without removing it, or -1 if the queue is empty.

43
Q

What is ‘Queue Overflow’?

A

It occurs when trying to enqueue an element into a full queue.

44
Q

What is ‘Queue Underflow’?

A

It occurs when trying to dequeue an element from an empty queue.

45
Q

What is a linked list?

A

A dynamic data structure where elements, called nodes, are linked using pointers.

46
Q

How is a linked list simulated using an array?

A

By managing pointers (or indices) within the array, where each node has data and a next index.

47
Q

What are the two parts of each node in an array-based linked list?

A

Data (the value stored) and Next Index (the index to the next element).

48
Q

What is the head in a linked list?

A

The first element (or node) in the list.

49
Q

How is the end of the list indicated in an array-based linked list?

A

By a -1 in the next index, serving as the equivalent of nullptr.

50
Q

What is a key property of an array-based linked list?

A

It uses an array to store both data and next pointers, but the size is fixed by the array’s size.

51
Q

What operation inserts a new element at the head of the list?

A

Insert at the beginning.

52
Q

What operation removes an element from the list?

A

Delete an element.

53
Q

What does the traverse operation do in a linked list?

A

Visits each node in the list and prints its data.

54
Q

What does the ‘freeIndex’ variable represent in the LinkedList class?

A

It points to the next free position in the array.

55
Q

What does the line ‘list[MAX_SIZE - 1].next = -1

A

’ signify?

56
Q

What happens when ‘freeIndex’ is -1 in the ‘insertAtBeginning’ function?

A

It indicates that the list is full.

57
Q

What does ‘list[newIndex].next = head

A

’ do in the ‘insertAtBeginning’ function?

58
Q

How does the ‘insertAtEnd’ function handle an empty list?

A

If the list is empty, the new node becomes the head.

59
Q

What is the purpose of the ‘while’ loop in the ‘insertAtEnd’ function?

A

To traverse to the end of the list.

60
Q

What does ‘list[newIndex].next = -1

A

’ signify in the ‘insertAtEnd’ function?

61
Q

What does the ‘deleteAtBeginning’ function check before proceeding?

A

It checks if the list is empty by verifying if ‘head’ is -1.

62
Q

What does the ‘head’ represent in a linked list?

A

The index of the first node in the list.

63
Q

What is the purpose of the ‘next index’ in a linked list?

A

It stores the index of the next node in the list.

64
Q

What does ‘free index’ point to in a linked list?

A

The next available spot in the array for inserting a new node.

65
Q

How is the end of the linked list indicated?

A

By using -1 to mark the end (no next node).

66
Q

What is linked list traversal?

A

The process of visiting each node in the list sequentially.

67
Q

How does this array-based implementation simulate a linked list?

A

By manually managing indices instead of pointers.

68
Q

What does each ‘node’ in the array hold?

A

Both the data and an index pointing to the next node.

69
Q

What happens when a node is deleted from the beginning of the linked list?

A

The head moves to the next node and the old head is added to the free list.