Asymptotic Notation · Algorithms
40 questions · asked in 21 of 27 years (2000–2026)
MCQ 37 · MSQ 2 · NAT 11-mark 27 · 2-mark 13
2000Questions per year2026
Questions 11–15 of 40
Consider the following C function
int fun (int n) {
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1 ; j < n ; j+=i) {
printf ("%d %d ", i, j);
}
}
} The time complexity of fun in terms of notation isAnswer: (C)
The inner loop goes up in steps of i, so for each value of i it runs about n/i times. Adding this over all i gives .
Answer:
Answer:
Consider a carry lookahead adder for adding two n-bit integers,built using gates of fan-in at most two. The time to perform addition using this adder is
Answer: (B)
A carry lookahead adder finds every carry from the generate and propagate signals. The gates have fan-in 2, so joining n signals takes a tree whose depth is .
Answer:
Answer:
The given diagram shows the flowchart for a recursive function A(n). Assume that all statements, except for the recursive calls, have O(1) time complexity. If the worst case time complexity of this function is , then the least possible value (accurate upto two decimal positions) of is .
Answer: 2.2 to 2.4
In the flowchart, the worst case path makes these calls: A(n/2) at the start, then the right branch with two calls, then one more call going down, then one more call on the left branch. This gives 5 calls to A(n/2).
So the recurrence and its result are:
Answer: 2.32
So the recurrence and its result are:
Answer: 2.32
In an adjacency list representation of an undirected simple graph G = (V,E), each edge (u,v) has two adjacency list entries: [v] in the adjacency list of u, and [u] in the adjacency list of v. These are called twins of each other. A twin pointer is a pointer from an adjacency list entry to its twin. If |E|=m and |V|=n, and the memory size is not a constraint, what is the time complexity of the most efficient algorithm to set the twin pointer in each entry in each adjacency list?
Answer: (B)
Memory is free, so use lists and arrays freely.
Pass 1. For every vertex and every entry in the list of , add a record "(u, pointer to this entry)" to a bucket list . This visits each of the entries once, so it takes . After it, has one record for each neighbour of , where is the entry stored in the list of .
Pass 2. For each vertex , first scan its own list and set to the entry in it. Then, for each record in , set the twins . The twin of is the entry in the list of . Every that is read was just written for this (the graph is simple), so needs no clearing. The work for is , so pass 2 takes .
Any algorithm must at least visit all entries (and the lists), so is needed. The total is .
Answer: (B)
Pass 1. For every vertex and every entry in the list of , add a record "(u, pointer to this entry)" to a bucket list . This visits each of the entries once, so it takes . After it, has one record for each neighbour of , where is the entry stored in the list of .
Pass 2. For each vertex , first scan its own list and set to the entry in it. Then, for each record in , set the twins . The twin of is the entry in the list of . Every that is read was just written for this (the graph is simple), so needs no clearing. The work for is , so pass 2 takes .
Any algorithm must at least visit all entries (and the lists), so is needed. The total is .
Answer: (B)
Consider the following C function.
int fun1(int n){
int i,j,k,p,q=0;
for (i=1; i < n; ++i) {
p=0;
for (j=n; j > 1; j=j/2)
++p;
for (k=1; k < p; k=k*2)
++q;
}
return q;
} Which one of the following most closely approximates the return value of the function fun1?Answer: (D)
In each of the n passes of the outer loop:
- the j loop runs and sets p to
- the k loop doubles k until it reaches p, so q grows by
So in total, q is
Answer:
- the j loop runs and sets p to
- the k loop doubles k until it reaches p, so q grows by
So in total, q is
Answer:
Keep practising