0% found this document useful (0 votes)
5 views

Test 1 sol

The document provides a test solution on data structures and algorithms, including multiple choice questions about stacks, binary search trees, and sorting algorithms. It also covers short answer questions defining linked lists, explaining recursion, and comparing BFS and DFS. Additionally, it details the implementation of a stack using an array with example code in C++.

Uploaded by

ranahamzasab4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Test 1 sol

The document provides a test solution on data structures and algorithms, including multiple choice questions about stacks, binary search trees, and sorting algorithms. It also covers short answer questions defining linked lists, explaining recursion, and comparing BFS and DFS. Additionally, it details the implementation of a stack using an array with example code in C++.

Uploaded by

ranahamzasab4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Test # 1

Key Solution

Data Structure and Algorithms

Section I: Multiple Choice Questions (5 × 1 = 5)

1. Which data structure uses LIFO order?


Answer: b) Stack
2. What is the time complexity of searching in a binary search tree (average case)?
Answer: b) O(log n)
3. Which sorting algorithm is stable?
Answer: a) Merge Sort
4. What is the maximum number of children a binary tree node can have?
Answer: b) 2
5. Which data structure is best suited for breadth-first search?
Answer: b) Queue

Section II: Short Answer Questions (5 × 2 = 10)

1. Define a linked list and its types.


A linked list is a dynamic data structure consisting of nodes, where each node contains
data and a pointer to the next node.
Types of Linked Lists:
o Singly Linked List: Each node points to the next node; the last node points to
NULL.
o Doubly Linked List: Each node has pointers to both the next and previous nodes.
o Circular Linked List: The last node points back to the head, forming a loop.

2. What is the difference between an array and a linked list?

Feature Array Linked List


Size Fixed size (static allocation). Dynamic size (grows/shrinks).
Memory Contiguous memory blocks. Non-contiguous memory blocks.
Costly (requires shifting Efficient (just pointer
Insertion/Deletion
elements). adjustments).
3. Explain the purpose of the 'stack' data structure.
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle.
The last element added to the stack is the first one to be removed.
Uses:
o Expression evaluation (e.g., postfix).
o Backtracking (e.g., browser history).
o Function call management in recursion.

4. What is recursion? Give an example.


Recursion is a process where a function calls itself to solve a smaller instance of a
problem.
Example (C++): Factorial of a Number

int factorial(int n) {
if (n == 0) return 1; // Base case
return n * factorial(n - 1); // Recursive call
}

5. What is the difference between BFS and DFS?


BFS (Breadth-First Search):
 Explores all nodes level by level.
 Uses a queue for implementation.
 Suitable for finding the shortest path in an unweighted graph.

DFS (Depth-First Search):

 Explores nodes as far as possible along a branch before backtracking.


 Uses a stack (or recursion).
 Suitable for exploring all possible paths.

Section III: Extensive Question (1 × 5 = 5)

Q#3: Explain how to implement a stack using an array with examples.

Stack Implementation Using an Array

A stack can be implemented using an array by maintaining a variable top to track the topmost
element in the stack.

Operations:

1. Push: Add an element to the top of the stack.


2. Pop: Remove the top element.
3. Peek/Top: Return the top element without removing it.
4. isEmpty: Check if the stack is empty.

Example (C++)

#include <iostream>
using namespace std;

#define MAX 100 // Maximum size of the stack

class Stack {
private:
int arr[MAX];
int top;

public:
Stack() { top = -1; } // Constructor initializes stack as empty

// Push an element onto the stack


void push(int x) {
if (top == MAX - 1) {
cout << "Stack Overflow\n";
return;
}
arr[++top] = x;
}

// Pop the top element from the stack


int pop() {
if (top == -1) {
cout << "Stack Underflow\n";
return -1;
}
return arr[top--];
}

// Peek the top element


int peek() {
if (top == -1) {
cout << "Stack is Empty\n";
return -1;
}
return arr[top];
}

// Check if the stack is empty


bool isEmpty() {
return top == -1;
}
};

int main() {
Stack stack;

stack.push(10);
stack.push(20);
stack.push(30);

cout << "Top element: " << stack.peek() << endl; // 30

cout << "Popped element: " << stack.pop() << endl; // 30


cout << "Top element after pop: " << stack.peek() << endl; // 20

return 0;
}

Output:

Top element: 30
Popped element: 30
Top element after pop: 20

Advantages of Array-based Stack:

 Easy to implement.
 Fast access due to contiguous memory.

Limitations:

 Fixed size (static allocation).


 Stack Overflow if the array size limit is reached.

You might also like