Q.1
14 MarksWhat is the best-case, average-case, and worst-case time complexity analysis?
Time Complexity Analysis
- Best-Case Time Complexity ( notation):
- The minimum amount of time or operations an algorithm requires to execute for an input of size , under the most favorable circumstances.
- Example: Linear search on an array where the search key is at the first index ().
- Worst-Case Time Complexity ( notation):
- The maximum amount of time or operations an algorithm can ever take for an input of size , under the least favorable (adversarial) input.
- Provides a guaranteed upper bound on performance.
- Example: Linear search when the element is at the last position or not present ().
- Average-Case Time Complexity ( notation):
- The expected number of operations an algorithm executes across all possible valid input permutations of size , assuming a uniform probability distribution.
- Example: Linear search where the element is found around index on average ().
Explain row-major order and column-major order representation of 2-D array.ss
Also asked in Winter 2024 Q1c
Row-Major vs Column-Major Representation of 2D Arrays
In computer memory, multi-dimensional arrays are mapped to a linear 1D address space using two standard conventions:
Given 2D Array A[2][2]:
[ A[0][0] A[0][1] ]
[ A[1][0] A[1][1] ]
Row-Major: | A[0][0] | A[0][1] | A[1][0] | A[1][1] | (Consecutive rows)
Column-Major: | A[0][0] | A[1][0] | A[0][1] | A[1][1] | (Consecutive columns)Address Calculation Formulas:
Let have Base Address , element size bytes, row indices , and column indices :
- Row-Major Order (C, C++, Java, Python):
- Elements are stored row-by-row consecutively in memory.
- Column-Major Order (Fortran, MATLAB, R):
- Elements are stored column-by-column consecutively in memory.
21, 51, 12, 45, 17, 71, 19, 47, 78. Write Pre-order, In-order, and Post-order traversal of constructed BST.
1. Construction of Binary Search Tree (BST)
Given Data Sequence: 21, 51, 12, 45, 17, 71, 19, 47, 78
Insertion Steps (Left if smaller, Right if larger):
- Insert 21: Root node .
- Insert 51: right child of .
- Insert 12: left child of .
- Insert 45: , left child of .
- Insert 17: , right child of .
- Insert 71: , right child of .
- Insert 19: , , right child of .
- Insert 47: , , right child of .
- Insert 78: , , right child of .
2. Constructed BST Structure
21
/ \
12 51
\ / \
17 45 71
\ \ \
19 47 783. Tree Traversals
- In-order Traversal (Left Root Right):
(Property: Always yields strictly ascending sorted order for any valid BST!)
- Pre-order Traversal (Root Left Right):
- Post-order Traversal (Left Right Root):
- In-order:
12, 17, 19, 21, 45, 47, 51, 71, 78 - Pre-order:
21, 12, 17, 19, 51, 45, 47, 71, 78 - Post-order:
19, 17, 12, 47, 45, 78, 71, 51, 21

