CSC 305 PRACTICE QUESTIONS SIMPLIFIED BY WLS
CSC 305 PRACTICE QUESTIONS SIMPLIFIED BY WLS
● 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++).
● 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)).
● 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.
● 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.
● 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.
● 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).
● 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
● 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.
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.
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.
● 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.
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.