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

CSC 305 PRACTICE QUESTIONS SIMPLIFIED BY WLS

Algorithm practice questions

Uploaded by

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

CSC 305 PRACTICE QUESTIONS SIMPLIFIED BY WLS

Algorithm practice questions

Uploaded by

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

Section 1: Basics of Algorithms

1. Define an algorithm and explain its key properties.

●​ Answer: An algorithm is a step-by-step, well-defined


procedure for solving a problem.
●​ Key Properties:
○​ Finiteness: Must terminate after a finite number
of steps.
○​ Definiteness: Each step is clear and
unambiguous.
○​ Input & Output: Accepts zero or more inputs
and produces one or more outputs.
○​ Effectiveness: Every operation can be
performed in a finite amount of time using
available resources.

2. Differentiate between a problem and an instance of a


problem.

●​ Answer:
○​ A problem is a general description of a task or
question to be solved (e.g., "sort a list of
numbers").
○​ An instance of a problem is a specific case with
concrete data (e.g., "sort the list [3, 1, 4, 1, 5]").
3. List three ways to represent an algorithm.

●​ Answer:
○​ Flowcharts: Diagrammatic representation
showing the sequence of steps.
○​ Pseudocode: A high-level, language-agnostic
description using structured plain language.
○​ Programming Language Code: A direct
implementation in a programming language
(such as Python, Java, or C++).

Section 2: Algorithm Classification &


Design
4. What are the different ways to classify algorithms?

●​ Answer:
○​ By Implementation: Recursive vs. iterative.
○​ By Design Paradigm:
■​ Brute Force: Try all possibilities.
■​ Divide and Conquer: Split the problem
into smaller subproblems (e.g., Merge
Sort).
■​ Greedy: Choose the best option at each
step (e.g., Dijkstra’s algorithm).
■​ Dynamic Programming: Solve
overlapping subproblems and store
results.
○​ By Complexity: Classified based on time and
space complexity (e.g., O(n), O(n log n)).

5. Explain the difference between top-down and bottom-up


design approaches.

●​ Answer:
○​ Top-Down Design: Start with a high-level
overview and break the problem into smaller,
manageable parts until detailed steps are
defined.
○​ Bottom-Up Design: Begin by creating and
testing small, basic components (or modules)
and then integrate them to form a complete
solution.

Section 3: Algorithm Analysis


6. What is time complexity, and why is it important?

●​ Answer:
○​ Time Complexity: A measure of the running
time of an algorithm as a function of the input
size, usually expressed in Big-O notation (e.g.,
O(n), O(n²)).
○​ Importance: It helps predict how an algorithm
will scale and perform with large inputs, enabling
the selection of the most efficient algorithm for a
problem.

7. Define space complexity and list its components.

●​ Answer:
○​ Space Complexity: Measures the total amount
of memory an algorithm uses relative to the
input size.
○​ Components:
■​ Instruction Space: Memory required for
the code itself.
■​ Data Space: Memory for variables,
constants, and dynamically allocated
storage.
■​ Environment (Stack) Space: Memory
used by the system for function calls,
recursion, and temporary data.

Section 4: Data Structures


8. Differentiate between primitive and non-primitive data
structures.

●​ Answer:
○​ Primitive Data Structures: Basic data types
provided by a programming language (e.g., int,
char, float).
○​ Non-Primitive Data Structures: Structures built
from primitive types to organize data more
complexly (e.g., arrays, linked lists, stacks,
queues, trees, graphs).

9. Describe the operations that can be performed on data


structures.

●​ Answer:
○​ Insertion: Adding an element.
○​ Deletion: Removing an element.
○​ Traversal: Visiting each element (e.g., for
printing or processing).
○​ Searching: Locating an element.
○​ Updating: Changing the value of an element.
○​ Sorting: Arranging elements in a specified
order.

Section 5: Arrays
10. How do you compute the memory location of an
element in a 2D array using row-major order?

●​ Answer:
○​ Formula: Address=Base Address+((i×Number
of Columns)+j)×Size of Element\text{Address} =
\text{Base Address} + \left((i \times \text{Number
of Columns}) + j\right) \times \text{Size of
Element}
○​ Example: For a 3×3 array with a base address
of 1000, element size of 4 bytes, the location of
element at row 1, column 2 is:
1000+((1×3)+2)×4=1000+(5×4)=10201000 + ((1
\times 3) + 2) \times 4 = 1000 + (5 \times 4) =
1020

11. Explain the concept of a sparse array and its


advantages.

●​ Answer:
○​ A sparse array is one in which most elements
have the same default value (often zero), and
only a few elements hold significant values.
○​ Advantages:
■​ Memory Efficiency: Only non-default
values and their indices are stored.
■​ Performance: Algorithms can be
optimized by ignoring the default values.

Section 6: Stacks
12. Describe the LIFO property of a stack.

●​ Answer:
○​ LIFO (Last In, First Out): The most recently
added element is the first one to be removed.
○​ Analogy: Think of a stack of plates—the last
plate placed on top is the first one you remove.

13. Write the algorithm for the PUSH operation in a stack.

Answer (Pseudocode):​
Algorithm PUSH(stack, element):
if stack is full:
error "Stack Overflow"
else:
top = top + 1
stack[top] = element

●​
○​ Explanation: The algorithm checks for overflow,
increments the top pointer, and then inserts the
new element.

Section 7: Queues
14. What is the main difference between a stack and a
queue?

●​ Answer:
○​ Stack: Follows LIFO (Last In, First Out).
○​ Queue: Follows FIFO (First In, First Out); the
first element inserted is the first to be removed.

15. Write the algorithm for the ENQUEUE operation in a


queue.

Answer (Pseudocode):​
Algorithm ENQUEUE(queue, element):
if queue is full:
error "Queue Overflow"
else:
rear = (rear + 1) mod capacity
queue[rear] = element

●​
○​ Explanation: The algorithm checks if the queue
is full, advances the rear pointer (wrapping
around if needed), and adds the element.

Section 8: Linked Lists


16. Explain the difference between singly and doubly
linked lists.

●​ Answer:
○​ Singly Linked List: Each node contains data
and a pointer to the next node, allowing traversal
in one direction only.
○​ Doubly Linked List: Each node contains data,
a pointer to the next node, and a pointer to the
previous node, allowing traversal in both
directions.

17. Write the algorithm for inserting an element into a


linked list (inserting at the beginning).

Answer (Pseudocode):​
Algorithm INSERT_AT_BEGINNING(head, new_data):
create new_node
new_node.data = new_data
new_node.next = head
head = new_node
return head

●​
○​ Explanation: A new node is created, its next
pointer is set to the current head, then the head
is updated to the new node.

You might also like