Different Ministry
Post: Assistant Maintenance Engineer
Subject: CSE, Exam Taker: BPSC
1(a)What is heap? Explain how heap property is maintained in binary tree. 3+5 = 8
What is Heap?
A Heap is a special type of Complete Binary Tree that satisfies the Heap Property. In a heap, the tree must be completely filled at all levels except possibly the last level, which is filled from left to right. There are two types of heap:
- Max-Heap: The value of each parent node is greater than or equal to its child nodes.
- Min-Heap: The value of each parent node is less than or equal to its child nodes.
How Heap Property is Maintained in Binary Tree
The heap property is maintained mainly during Insertion and Deletion operations using two procedures:
1. Heapify-Up (Bubble-Up): When a new element is inserted, it is first placed at the last position to maintain the Complete Binary Tree property. Then it is compared with its parent. If the heap property is violated, the element is swapped with its parent. This process continues until the heap property is restored.
2. Heapify-Down (Bubble-Down): When the root element is removed (in deletion), the last element replaces the root. Then it is compared with its children. If the heap property is violated, it is swapped with the appropriate child (larger child in Max-Heap, smaller child in Min-Heap). This process continues until the heap property is satisfied.
Thus, by using Heapify-Up and Heapify-Down operations, the heap property is maintained in a binary heap.
Heap কী?
Heap হলো একটি বিশেষ ধরনের Complete Binary Tree যা Heap Property অনুসরণ করে। এখানে সব লেভেল পূর্ণ থাকে, শুধু শেষ লেভেলটি বাম দিক থেকে পূরণ হয়। Heap দুই প্রকার:
- Max-Heap: প্রতিটি parent node-এর মান তার child node-এর সমান বা বড় হয়।
- Min-Heap: প্রতিটি parent node-এর মান তার child node-এর সমান বা ছোট হয়।
Binary Tree-তে Heap Property কীভাবে বজায় রাখা হয়
Heap Property মূলত Insertion এবং Deletion অপারেশনের সময় বজায় রাখা হয়, দুটি পদ্ধতির মাধ্যমে:
১. Heapify-Up (Bubble-Up): নতুন element প্রথমে শেষ স্থানে বসানো হয়। তারপর parent-এর সাথে তুলনা করা হয়। যদি Heap Property ভঙ্গ হয়, তবে element ও parent-এর মধ্যে swap করা হয়। এই প্রক্রিয়া চলতে থাকে যতক্ষণ না Heap Property ঠিক থাকে।
২. Heapify-Down (Bubble-Down): Root delete করলে শেষ element root-এ আনা হয়। তারপর child node-এর সাথে তুলনা করা হয়। যদি Heap Property ভঙ্গ হয়, তবে উপযুক্ত child-এর সাথে swap করা হয়। এই প্রক্রিয়া চলতে থাকে যতক্ষণ না Heap Property বজায় থাকে।
এইভাবে Heapify-Up এবং Heapify-Down প্রক্রিয়ার মাধ্যমে Heap Property বজায় রাখা হয়।
1(b)How can a graph be represented? Explain Prim's algorithm with an example. 4+4 = 8
Graph Representation
A graph can be represented mainly in two ways:
1) Adjacency Matrix:
It is a 2D array of size V × V (where V = number of vertices). If there is an edge between vertex i and j, the value is 1 (or weight for weighted graph); otherwise 0.
Example:
For a graph with vertices A, B, C and edges A–B, B–C:
A B C A [ 0 1 0 ] B [ 1 0 1 ] C [ 0 1 0 ]
2) Adjacency List:
Each vertex stores a list of its adjacent vertices. It requires less memory and is suitable for sparse graphs.
Example:
A → B B → A, C C → B
Prim’s Algorithm Prim’s Algorithm is a greedy algorithm used to find the Minimum Spanning Tree (MST) of a weighted, connected, undirected graph. The MST connects all vertices with minimum total edge weight and without forming cycles.
Steps of Prim’s Algorithm:
1) Start from any vertex.
2) Select the minimum weight edge that connects a visited vertex to an unvisited vertex.
3) Add the selected edge to the MST.
4) Repeat until all vertices are included.
Example:

Graph প্রধানত দুইভাবে উপস্থাপন করা যায়:
১) Adjacency Matrix:এটি একটি 2D array (V × V), যেখানে V হলো vertex সংখ্যা। যদি i ও j vertex-এর মধ্যে edge থাকে তবে মান 1 (বা weighted graph হলে weight), না থাকলে 0।
উদাহরণ:Vertex A, B, C এবং edge A–B, B–C হলে:
A B C A [ 0 1 0 ] B [ 1 0 1 ] C [ 0 1 0 ]
২) Adjacency List:
প্রতিটি vertex-এর সাথে সংযুক্ত vertex-এর তালিকা সংরক্ষণ করা হয়। এটি কম memory ব্যবহার করে এবং sparse graph-এর জন্য উপযোগী।
উদাহরণ:
A → B B → A, C C → B
Prim’s Algorithm Prim’s Algorithm হলো একটি greedy algorithm যা weighted, connected, undirected graph-এর Minimum Spanning Tree (MST) নির্ণয় করতে ব্যবহৃত হয়। MST সব vertex-কে minimum মোট weight দিয়ে সংযুক্ত করে এবং কোনো cycle তৈরি করে না।
Prim’s Algorithm-এর ধাপসমূহ:
1) যেকোনো একটি vertex থেকে শুরু করতে হবে।
2) এমন minimum weight edge নির্বাচন করতে হবে যা visited vertex থেকে unvisited vertex-এ যায়।
3) সেই edge MST-তে যুক্ত করতে হবে।
4) সব vertex অন্তর্ভুক্ত না হওয়া পর্যন্ত প্রক্রিয়া চালাতে হবে।
উদাহরণ:

2(a) Write an algorithm for finding the k-th smallest element in an array of n elements. Analyze the complexity of your algorithm. 4+4 = 8
Method: Using QuickSelect (Efficient Approach)
The QuickSelect algorithm is based on the partition process of QuickSort. It finds the k-th smallest element without fully sorting the array.
Pseudocode:
QUICKSELECT(A, low, high, k) 1. if low = high 2. return A[low] 3. pivotIndex ← PARTITION(A, low, high) 4. if pivotIndex = k 5. return A[pivotIndex] 6. else if k < pivotIndex 7. return QUICKSELECT(A, low, pivotIndex - 1, k) 8. else 9. return QUICKSELECT(A, pivotIndex + 1, high, k) PARTITION(A, low, high) 1. pivot ← A[high] 2. i ← low - 1 3. for j ← low to high - 1 4. if A[j] ≤ pivot 5. i ← i + 1 6. swap A[i] and A[j] 7. swap A[i + 1] and A[high] 8. return i + 1
Note: To find the k-th smallest element, call QUICKSELECT(A, 0, n-1, k-1) (assuming 0-based index).
Complexity Analysis:
- Best Case: O(n)
- Average Case: O(n)
- Worst Case: O(n²) (when pivot selection is poor)
- Space Complexity: O(1) (in-place, ignoring recursion stack)
Thus, QuickSelect is efficient for finding the k-th smallest element compared to fully sorting the array (which takes O(n log n)).
2(b) Distinguish between function overloading and operator overloading with necessary examples. 8
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}Here, the function name add() is the same, but parameters are different.Operator Overloading:Operator Overloading allows existing operators (such as +, -, *, ==) to have special meaning when applied to user-defined objects. It also supports Compile-time Polymorphism.Example (C++):class Complex {
public:
int real, imag;
Complex(int r, int i) {
real = r;
imag = i;
}
Complex operator + (Complex c) {
return Complex(real + c.real, imag + c.imag);
}
};Here, the + operator is overloaded to add two Complex objects.Differences:- Meaning: Function overloading defines multiple functions with same name; Operator overloading gives new meaning to operators.
- Applies To: Function names vs Operators.
- Purpose: Improves code readability by using same function name; Makes user-defined objects behave like built-in types.
- Example: add() function vs + operator for Complex numbers.
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}এখানে add() function একই নাম হলেও parameter ভিন্ন।Operator Overloading:Operator Overloading এর মাধ্যমে +, -, *, == ইত্যাদি operator-কে user-defined object-এর জন্য নতুন অর্থ দেওয়া যায়। এটিও Compile-time Polymorphism-এর উদাহরণ।উদাহরণ (C++):class Complex {
public:
int real, imag;
Complex(int r, int i) {
real = r;
imag = i;
}
Complex operator + (Complex c) {
return Complex(real + c.real, imag + c.imag);
}
};এখানে + operator-টি Complex object যোগ করার জন্য overloaded করা হয়েছে।পার্থক্য:- অর্থ: Function overloading একই নামের একাধিক function; Operator overloading operator-কে নতুন অর্থ দেয়।
- প্রযোজ্য ক্ষেত্র: Function name বনাম Operator।
- উদ্দেশ্য: একই function name ব্যবহার করে readability বৃদ্ধি; User-defined object-কে built-in type-এর মতো ব্যবহার করা।
- উদাহরণ: add() function বনাম Complex class-এ + operator।
3(a) When does a page fault occur? Describe the actions taken by an oprating system when a page fault occurs. 3+5 = 8
When Does a Page Fault Occur?
A Page Fault occurs in a Virtual Memory system when a process tries to access a page that is not currently present in the main memory (RAM). The page may be stored in secondary storage (disk), and the Page Table entry for that page is marked as invalid. When the CPU detects this condition, it generates a page fault interrupt.
Actions Taken by the Operating System During a Page Fault
- Trap to Operating System: The hardware generates a page fault interrupt and transfers control to the OS.
- Check Validity: The OS checks whether the memory reference is valid or illegal. If invalid, the process is terminated.
- Locate the Page on Disk: If valid, the OS finds the required page in secondary storage (swap space).
- Find Free Frame: The OS searches for a free frame in main memory. If no free frame is available, a Page Replacement Algorithm (e.g., FIFO, LRU) is used to remove an existing page.
- Load the Page: The required page is loaded from disk into the selected frame.
- Update Page Table: The page table is updated to mark the page as valid and store the frame number.
- Restart the Instruction: The interrupted instruction is restarted, and execution continues normally.
Thus, a page fault allows the system to load required pages dynamically, ensuring efficient use of memory.
কখন Page Fault ঘটে?
Page Fault ঘটে যখন কোনো process এমন একটি page access করতে চায় যা বর্তমানে main memory (RAM)-এ নেই। এটি সাধারণত Virtual Memory ব্যবস্থায় ঘটে। ঐ page-টি secondary storage (disk)-এ থাকে এবং Page Table-এ তার entry invalid হিসেবে চিহ্নিত থাকে। তখন CPU একটি page fault interrupt তৈরি করে।
Page Fault ঘটলে Operating System যে পদক্ষেপ গ্রহণ করে
- Trap to OS: Hardware একটি interrupt তৈরি করে এবং নিয়ন্ত্রণ OS-এর কাছে হস্তান্তর করে।
- Validity Check: OS পরীক্ষা করে memory reference বৈধ কিনা। অবৈধ হলে process terminate করা হয়।
- Page Locate করা: যদি বৈধ হয়, OS disk-এ প্রয়োজনীয় page খুঁজে বের করে।
- Free Frame খোঁজা: Main memory-তে একটি খালি frame খোঁজা হয়। না থাকলে Page Replacement Algorithm (যেমন FIFO, LRU) ব্যবহার করে একটি page সরানো হয়।
- Page Load করা: প্রয়োজনীয় page disk থেকে main memory-তে load করা হয়।
- Page Table Update: Page table update করে নতুন frame number সংরক্ষণ করা হয় এবং page-কে valid করা হয়।
- Instruction Restart: বাধাপ্রাপ্ত instruction পুনরায় চালু করা হয় এবং execution স্বাভাবিকভাবে চলতে থাকে।
এভাবে Page Fault-এর মাধ্যমে প্রয়োজনীয় page dynamicভাবে load করে memory দক্ষভাবে ব্যবহৃত হয়।
3(b)What is deadlock? What are the necessary conditions for a deadlock? What are the ways to prevent it? 2+4+2=8
Deadlock:
A Deadlock is a situation in an Operating System where two or more processes are permanently blocked because each process is waiting for a resource that is held by another process. As a result, none of the processes can proceed.
Necessary Conditions for Deadlock (Coffman Conditions):
Deadlock occurs only if all the following four conditions hold simultaneously:
- Mutual Exclusion: At least one resource must be held in a non-shareable mode (only one process can use it at a time).
- Hold and Wait: A process holding at least one resource is waiting to acquire additional resources held by other processes.
- No Preemption: Resources cannot be forcibly taken away from a process; they must be released voluntarily.
- Circular Wait: A circular chain of processes exists where each process is waiting for a resource held by the next process in the chain.
Ways to Prevent Deadlock:
Deadlock can be prevented by eliminating at least one of the necessary conditions:
- Eliminate Mutual Exclusion: Make resources sharable where possible (e.g., read-only files).
- Eliminate Hold and Wait: Require processes to request all required resources at once.
- Allow Preemption: If a process requests a resource that is unavailable, it must release its currently held resources.
- Prevent Circular Wait: Impose a strict ordering of resource allocation so that processes request resources in a fixed order.
Thus, by breaking any one of the four conditions, deadlock can be prevented.
৩(খ) Deadlock কী? এর প্রয়োজনীয় শর্তসমূহ এবং প্রতিরোধের উপায়
Deadlock:
Deadlock হলো এমন একটি অবস্থা যেখানে দুই বা ততোধিক process স্থায়ীভাবে আটকে যায়, কারণ প্রত্যেক process অন্য process-এর দখলে থাকা resource-এর জন্য অপেক্ষা করে। ফলে কোনো process-ই তার কাজ সম্পন্ন করতে পারে না।
Deadlock-এর প্রয়োজনীয় শর্তসমূহ (Coffman Conditions):
নিচের চারটি শর্ত একসাথে সত্য হলে Deadlock ঘটে:
- Mutual Exclusion: অন্তত একটি resource এমন হতে হবে যা এক সময়ে শুধুমাত্র একটি process ব্যবহার করতে পারে।
- Hold and Wait: একটি process একটি resource ধরে রেখে অন্য resource-এর জন্য অপেক্ষা করে।
- No Preemption: Resource জোরপূর্বক কেড়ে নেওয়া যায় না; process নিজে থেকে ছাড়তে হয়।
- Circular Wait: একটি চক্র তৈরি হয় যেখানে প্রতিটি process পরবর্তী process-এর দখলে থাকা resource-এর জন্য অপেক্ষা করে।
Deadlock প্রতিরোধের উপায়:
চারটি শর্তের যেকোনো একটি ভেঙে দিলে Deadlock প্রতিরোধ করা যায়:
- Mutual Exclusion দূর করা: সম্ভব হলে resource-কে sharable করা।
- Hold and Wait দূর করা: Process-কে একসাথে সব resource চাইতে বাধ্য করা।
- Preemption চালু করা: প্রয়োজন হলে resource ফিরিয়ে নেওয়ার ব্যবস্থা রাখা।
- Circular Wait প্রতিরোধ করা: Resource allocation-এর জন্য নির্দিষ্ট ক্রম (ordering) নির্ধারণ করা।
অতএব, চারটি শর্তের যেকোনো একটি বাতিল করলে Deadlock প্রতিরোধ করা সম্ভব।
4(a)What do you mean by pipelining? Describe briefly different kinds of hazards that decrease the performance of a pipelined processor. 3+5=8
Pipelining:
Pipelining is a technique used in CPU architecture to improve instruction throughput by overlapping the execution of multiple instructions. Instead of completing one instruction before starting the next, the processor divides instruction execution into stages (such as Fetch, Decode, Execute, Memory, Write Back) and processes different instructions simultaneously in different stages.
This increases performance and CPU utilization but may introduce certain hazards.
Hazards in Pipelined Processor:
Hazards are problems that prevent the next instruction in the pipeline from executing in its designated clock cycle. There are three main types:
- Structural Hazard: Occurs when hardware resources are insufficient to support all concurrent instructions.
Example: Two stages need the same memory unit at the same time. - Data Hazard: Occurs when an instruction depends on the result of a previous instruction that has not yet completed.
Example: Instruction 2 needs a value that Instruction 1 is still computing.
Types: RAW (Read After Write), WAR (Write After Read), WAW (Write After Write). - Control Hazard (Branch Hazard): Occurs due to branch instructions that change the flow of execution.
Example: The processor does not know the next instruction until the branch condition is evaluated.
These hazards reduce pipeline efficiency and may cause pipeline stalls or delays.
Pipelining হলো CPU architecture-এ ব্যবহৃত একটি কৌশল, যেখানে একাধিক instruction-এর execution ধাপগুলোকে ভাগ করে একসাথে প্রক্রিয়াকরণ করা হয়। যেমন: Fetch, Decode, Execute, Memory এবং Write Back ধাপগুলোতে বিভিন্ন instruction একই সময়ে কাজ করে।
এটি CPU-এর কর্মক্ষমতা বৃদ্ধি করে, তবে কিছু সমস্যা বা hazard সৃষ্টি করতে পারে।
Pipelined Processor-এ Hazard:
Hazard হলো এমন অবস্থা যা pipeline-এর স্বাভাবিক execution ব্যাহত করে। প্রধানত তিন ধরনের hazard রয়েছে:
- Structural Hazard: যখন পর্যাপ্ত hardware resource না থাকায় একাধিক instruction একই resource ব্যবহার করতে চায়।
উদাহরণ: একই সময়ে দুটি instruction memory ব্যবহার করতে চায়। - Data Hazard: যখন একটি instruction পূর্ববর্তী instruction-এর ফলাফলের উপর নির্ভরশীল।
উদাহরণ: দ্বিতীয় instruction প্রথমটির ফলাফল ব্যবহার করতে চায়, কিন্তু তা এখনো সম্পন্ন হয়নি।
প্রকার: RAW, WAR, WAW। - Control Hazard (Branch Hazard): যখন branch instruction execution flow পরিবর্তন করে এবং পরবর্তী instruction নির্ধারণে বিলম্ব ঘটে।
এই hazard-গুলোর কারণে pipeline stall বা delay সৃষ্টি হয় এবং performance কমে যায়।
