This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1Which of the following functions has the highest growth?
Option D
The growth of functions is compared using time complexity. As input size n increases, different functions grow at different rates. The general order of growth from lower to higher is O(n log n)<O(n2)<O(n3)<O(2n).
Exponential functions like 2n grow much faster than polynomial functions such as n2 or n3.
For example, if n is 10:
As n increases further, the gap becomes even larger.
Q: 2Arrange the following complexity of the algorithm in ascending order.
O(2ⁿ), O(n), O(log₂n), O(n log₂n), O(n²)
Option A
Algorithm Complexity represents how the running time or space requirement of an algorithm increases as the input size n increases.
In ascending order, complexities are arranged from fastest growth efficiency to slowest efficiency (smallest growth to largest growth).
Therefore, the correct ascending order is O(log2n)<O(n)<O(nlog2n)<O(n2)<O(2n).
The term log2n means “How many times can we divide n by 2 until we reach 1?”. It is called logarithm with base 2. Mathematically, log2n=x. Means 2x=n.
| Value of n | Meaning of log₂n | Result |
|---|---|---|
| log₂2 | 2¹ = 2 | 1 |
| log₂4 | 2² = 4 | 2 |
| log₂8 | 2³ = 8 | 3 |
| log₂16 | 2⁴ = 16 | 4 |
| log₂32 | 2⁵ = 32 | 5 |
| log₂64 | 2⁶ = 64 | 6 |
| log₂128 | 2⁷ = 128 | 7 |
| log₂256 | 2⁸ = 256 | 8 |
| log₂512 | 2⁹ = 512 | 9 |
| log₂1024 | 2¹⁰ = 1024 | 10 |
Common Time Complexities:
| Complexity | Meaning | Mathematical Example | Common Example |
|---|---|---|---|
| O(1) | Constant Time | If n = 1000, require 1 step. | Accessing Array Element. |
| O(log₂n) | Logarithmic Time | If n = 16, require 4 steps. | Binary Search |
| O(n) | Linear Time | If n = 10, require 10 steps. | Linear Search |
| O(n log₂n) | Linearithmic Time | If n = 8, require 8*3 = 24 steps. | Merge Sort |
| O(n²) | Quadratic Time | If n = 10, require 10*10 = 100 steps. | Bubble Sort |
| O(n³) | Cubic Time | If n = 5, require 5*5* 5 = 125 steps. | Matrix Multiplication |
| O(2ⁿ) | Exponential Time | If n = 5, require 2⁵ = 32 steps. | Recursive Fibonacci |
| O(n!) | Factorial Time | If n = 5, require 5! = 120 steps. | Traveling Salesman Problem |
Q: 3What is the time complexity of an infix to postfix conversion algorithm?
Option B
The time complexity of converting an infix expression to a postfix expression is O(N). This is because the algorithm scans the infix expression one character at a time from left to right.
For each character (operands, operators, parentheses), the algorithm may push it onto a stack or pop it from the stack. Each element is pushed and popped at most once, so the total number of operations grows directly with the length of the expression.
Since no nested loops or repeated scans are used, the overall work done is proportional to N, where N is the number of characters in the expression. Therefore, the time complexity is O(N).
| ALGORITHM | TIME COMPLEXITY |
|---|---|
| Infix to Postfix Conversion. | O(N) |
| Infix to Prefix Conversion. | O(N) |
| Postfix Expression Evaluation. | O(N) |
| Prefix Expression Evaluation. | O(N) |
| Balanced Parentheses Checking. | O(N) |
| String Reversal Using Stack. | O(N) |
Q: 4Which of the following options correctly matches the asymptotic notation.
Option D
Asymptotic Notation is used to measure how the running time of an algorithm grows as the input size increases. It does not count exact time, but shows the behavior of the algorithm for large inputs.
Big-O Notation (O):
Big-O tells us the worst-case performance of an algorithm. It means the maximum time an algorithm may take in the worst situation. Big-O answers the question: “What is the slowest this algorithm can be?”
Omega Notation (Ω):
Omega represents the best-case performance of an algorithm. It shows the minimum time required when the input is in the most favorable condition. Omega answers the question: “What is the fastest this algorithm can be?”
Theta Notation (Θ):
Theta gives the average or exact growth rate of an algorithm. It is used when the best case and worst case grow at the same rate. Theta answers the question : “On average, how does this algorithm perform?”
E.g.:
If searching for an element in a list:
In short:
Q: 5For defining the best time complexity, let f(n) = log(n) and g(n) = √n, ……..
Option B
Q: 6What would be the time complexity of a code that contains n nested loops running from 1 to m.
Option D
When a code contains n nested loops, and each loop runs from 1 to m, the inner loop executes m times for every iteration of the outer loop.
For two nested loops running from 1 to m time complexity will be m2 . Similarly, for n nested loops running from 1 to m time complexity will be mn.
| LOOP SCENARIO | DESCRIPTION | TIME COMPLEXITY |
|---|---|---|
| Loop with constant work. | Loop runs fixed number of times. | O(1) |
| Single loop. | One loop running from 1 to n. | O(n) |
| Two sequential loops. | One loop after another, both 1 to n. | O(n+n) ≈ O(n) |
| Two nested loops. | Outer loop 1 to n, inner loop 1 to n. | O(n2) |
| Three nested loops. | Three loops each 1 to n. | O(n3) |
| The k nested loops. | The k loops each 1 to n. | O(nk) |
| Two nested loops with different limits. | Outer 1 to n, inner 1 to m. | O(n*m) |
| The n nested loops, each 1 to m. | The n loops all running 1 to m. | O(mn) |
You have reached the end of this topic. Continue learning with the next topic below.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.