k Flashcards

1
Q

Is the following a property that holds for all non-decreasing positive functions f and g? (True=Yes/ False=No)

If f(n) = O(n2) for c=1 and n0=0 and

g(n) = Theta(n2) for n0=0 and c1 =1 and c2=1

then f(n) = O(g(n)).

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Rank the following functions by increasing order of growth:

log( n! ), 10000n2, log(n3), 2n, n2log(n)

A

log(n3), log( n! ), 10000n2, n2log(n), 2n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Let W(n) and A(n) denote respectively, the worst case and average case running time of an algorithm executed on an input of size n. which of the following is ALWAYS TRUE?

  • A(n) = O(W(n))
  • A(n) = \Theta(W(n))
  • None of the options
  • A(n) = \Omega(W(n))
A

A(n) = O(W(n))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which of the following can be used to compare two algorithms?

  • implementations of the two algorithms
    computers on which programs which implement the two algorithms are run
  • growth rates of the two algorithms
  • number of input parameters required for two algorithms
A

growth rates of the two algorithms

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If you are given different versions of the same algorithm with the following complexity classes, which one would you select?

  • linear
  • quadratic
  • Logarithmic
  • polynomial
A

logarithmic

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When we say algorithm A is asymptotically more efficient than B, what does that imply?

  • B will always be a better choice for small inputs
  • A will always be a better choice for large inputs
  • B will always be a better choice for all inputs
  • A will always be a better choice for small inputs
A

A will always be a better choice for large inputs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Consider the following algorithm

1 Bubble-sort(a)
2 for i = a.length() to 1
3 for j = 1 to i-1
4 if a[j]>a[j+1]
5 swap(a[j],a[j+1]);
6 end if
What is its basic operation (write the line number of code which would define the execution time of the code)?

A

4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the basic operation (that which is executed maximum number of times) in the following code?

reverse(a):
for i = 1 to len(a)-1
x = a[i]
for j = i downto 1
a[j] = a[j-1]
a[0] = x

A

a[j] = a[j-1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the correct loop invariant for the below code:

for i in range(len(A)): # in pseudo-code for i=0,...,len(A)-1

    answer += A[i]    

return answer
  • at the start of iteration i of the loop, the variable answer should contain the sum of the numbers from the subarray A[0:i-1]
  • the loop iterates from i ranging from 0 to length of the array
  • the loop stops when i reaches the last element of the array
  • the result of this code will be the sum of all the elements in the array
A

At the start of iteration i of the loop, the variable answer should contain the sum of the numbers from the subarray A[0:i-1].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Select correct inequality for the asymptotic order of growth of the below function. That is, as the value of n becomes very large what is the relation between the two functions.

n ___ n^2

A

<

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which of the following correctly defines what a ‘recurrence relation’ is?
- An equation (or inequality) that represents nth iteration of a sequence in terms of n. That includes an initial condition.
- T(n) = 2(1+n), T(0) = 2
- T(n)=T(n-1)+2n, T(0)=1
- An equation (or inequality) that relates the nth element of a sequence to its predecessors (recursive case). This includes an initial condition (base case).

A

An equation (or inequality) that relates the nth element of a sequence to its predecessors (recursive case). This includes an initial condition (base case).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Given the following algorithm

foo(n)
if n <= 1
return 1
else
x = foo(n-1)
for i = 1 to n
x = x + i
return x
Determine the asymptotic running time. Assume that addition can be done in constant time.

A

Theta(n^2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Solve the following recurrence by giving the tightest bound possible.

    T(n) = 4T(n/4) + 4n
A

Theta(nlogn)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the solution of T(n) = 2T(n/2) + n^2
using the Master theorem?

A

Theta(n^2), case 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the solution of T(n) = 2T(n/2) + n/logn
using the Master theorem?

A

master method does not apply

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

We can use Divide and Conquer technique to solve a problem in which of the following scenarios?

  • None of the options
  • The complexity is exponential to solve the entire problem
  • The subproblems are overlapping so we don’t have to solve them over and over again
  • We can break the problem into several subproblems that are similar to the original problems but smaller in size
A

We can break the problem into several subproblems that are similar to the original problems but smaller in size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What would be the time complexity of the following algorithm?

int sum = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < n; k++) {
if (i == j == k) {
for (l = 0; l < nnn; l++) {
sum = i + j + k + l;
}
}
}
}
}

A

Theta(n^4)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What would be the time complexity of the following algorithm?

reverse(a):
for i = 1 to len(a)-1
x = a[i]
for j = i downto 1
a[j] = a[j-1]
a[0] = x

A

O(n^2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Which of the following equations correctly represent the factorial function.

  • f(n) = (n-1) f(n-1)
  • f(n-1) = n f(n) - f(n) = n f(n)
  • f(n) = nf(n)
  • f(n) = n f(n-1)
A

f(n) = n f(n-1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Which of the following recurrence relations is correct representation of the towers of Hanoi problem that was discussed in the exploration?

  • F(n) = 2F(n-1) + 1
  • F(n) = F(n-1) + 2
  • F(n) = 2F(n-1)
  • F(n) = nF(n-1) + 1
A

F(n) = 2F(n-1) + 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What are the major required aspects in a problem in order to apply Dynamic Programming Technique?

  • Base case to stop recurrence
  • Be able to solve using top down and bottom up approach
  • Optimal Substructure and Overlapping subproblems
  • Whether a problem can be divided or not
A

Optimal Substructure and Overlapping subproblems

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

In which of the following approaches we start with the base case and proceed to solve the bigger subproblems?

  • both
  • none of the options
  • bottom-up approach
  • top-down approach
A

bottom-up approach

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

In dynamic programming, the technique of storing the previously calculated values is called

  • bottom-up approach
  • top-down approach
  • cache
  • memoization
A

memoization

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

The difference between Divide and Conquer Approach and Dynamic Programming is

  • The way we divide the sub-problems
  • The base case
  • Whether the sub-problems overlap or not
  • Use of recurrence formula
A

Whether the sub-problems overlap or not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

A binary search algorithm searches for a target value within a sorted array. Binary search compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until the target value is found or until a search can no longer be performed. This problem can be solved using which of the techniques?

  • Any of the two techniques
  • Dynamic Programming
  • None of the options
  • Divide and Conquer
A

Divide and Conquer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

In the Longest Common Subsequence problem assume we are comparing two strings of lengths m and n. In the bottom-up approach the solution we build a 2-Dimensional array called Cache[m][n]. The final solution was obtained by accessing which element of the cache?

  • The last but one element in the cache[m][n]
  • Any element in the Cache[m][n]
  • The first element in the cache[m][n]
  • The element in the bottom right corner of the cache[m][n]
A

The element in the bottom right corner of the cache[m][n]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

In _______ approach we start with the base case and build the solution starting from base case. In
________ approach we start solving the the bigger problem proceed towards the base case.

A

bottom-up approach
top-down approach

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Given two integer arrays to represent weights and profits of ’N’ items, find a subset of these items that will give us maximum profit such that their cumulative weight is not more than a given number ‘C’. Best technique to solve this problem is?

  • Dynamic Programming
  • Brute Force
  • Backtracking
  • Divide and Conquer
A

Dynamic Programming

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

To find the optimal solution for 0-1 knapsack, what would be dimensions of the extra array that we would need? The knapsack has a capacity of W, and there are total of n items. Assume we are using the approach that was discussed in the exploration.

  • Array[W][n]
  • Array[W]
  • Array[W+1][n+1]
  • Array[n+1]
A

Array[W+1][n+1]

30
Q

(T/F):We are given an array of numbers and we are asked to find an optimal solution to maximize the sum of numbers (i.e continuous subsequence that has maximum sum). if the order of the input numbers were altered or if we use a different algorithm, we will always end up with the same combination of numbers as answer.

A

False

31
Q

Backtracking is used to solve which of the problems:

  • Optimal solution problems
  • Any numerical problems
  • To find all possible solutions
  • Problems that have sub-problems similar to divide and conquer
A

To find all possible solutions

32
Q

What is the correct recurrence formula for the unbound knapsack problem that was discussed in the exploration?

Consider the weight of the items w[1..n], value of the items v[1..n]

  • F(x,v) = max{ F[x-wi] + vi }
  • F(x) = max{ F[x-wi] + vi }
  • F(x,i) = max{ vi + F[x-wi , i-1] , F[x , i -1] }
  • F(x) = max{ F[x-vi] + wi }
A
33
Q

In the o-1 knapsack recurrence formula f(x,i) = max{ vi + f[x-wi , i-1] , f[x , i -1] }

The first part vi + f[x-wi , i-1] represents : _____ the ith item to the knapsack

The second part f[x , i -1] represents: _____ the ith item to the knapsack

A

adding the ith item to the knapsack

not adding the ith item to the knapsack

34
Q

What makes the solution for the ‘Activity Selection Problem’ that we implemented in the exploration, a greedy approach?

  • It has optimal substructure
  • It satisfies greedy property
  • It is similar to Dynamic Programming algorithm
  • We make a best available choice in each iteration and we never look back
A

We make a best available choice in each iteration and we never look back

35
Q

Pick the statements which are True.

  • Greedy algorithms would always return an optimal solution
  • Dynamic programming technique would always return an optimal solution
  • Greedy algorithms are efficient compared to dynamic programming algorithms
  • A greedy algorithm is hard to design sometimes as it is difficult to find the best greedy approach
A
  • Dynamic programming technique would always return an optimal solution
  • Greedy algorithms are efficient compared to dynamic programming algorithms
  • A greedy algorithm is hard to design sometimes as it is difficult to find the best greedy approach
36
Q

(T/F): All possible greedy algorithms, at each step, choose what they know is going to lead to an optimal/near optimal solution

A

True

37
Q

(T/F):Can 0/1 knapsack problem be solved using the Greedy algorithm technique to obtain an optimum solution to fill the knapsack?

0/1 knapsack problem (This is the problem that we saw in the previous modules) When have n items and their values given. We are provided with a knapsack of capacity X. We have only one copy of each item. We need to maximize the value of our knapsack with items that we pick.

A

False

38
Q

(T/F): A graph can be represented as a tuple containing two sets. For example: A= ({…},{…})

A

True

39
Q

The asymptotic runtime of the solution for the combination sum problem that was discussed in the exploration is ____________.

  • Linear
  • N Factorial
  • Logarithmic
  • Exponential
A

Exponential

40
Q

Fill in the below pseudocode for activity selection problem using the greedy approach. The function returns the count of the maximum number of activities that can be selected.

activitySelection(activities):

sortBasedonEndTime(activities) # uses quick sort to sort the activities

for activity in activities:
if currendEndTime <= activity.startTime:
____________________
___________________

return result

Time complexity for the pseudocode will be
__________

A

result = result + 1

currentEndTime = activity.endTime

O(n^2)

41
Q

(T/F): Dijkstra is used to solve for a shortest path in a weighted graph with non-negative weights

A

True

42
Q

For an undirected graph G, what will be the sum of degrees of all vertices. (degree of a vertex is the number of edges connected to it.)
V: number of vertices, E: number of edges.

  • 2|E|
  • |V|
  • |E|
  • |V|+|E|
A

2|E|

43
Q

Which of the following data structures can be used to implement the Dijkstra algorithm most efficiently?

  • Min priority queue
  • Max priority queue
  • Circular queue
  • Stack
A

Min priority queue

44
Q

Given two vertices s and t in a connected graph G, which of the two traversals, BFS and DFS can be used to find if there is a path from s to t?

  • Both BFS and DFS
  • Only BFS
  • Only DFS
  • Neither BFS nor DFS
A

Both BFS and DFS

45
Q

Following is a pseudocode for graph traversal approach:

traverse (G, s) //Where G is the graph and s is the source node
let Q be queue.
Q.enqueue( s ) //Inserting s in queue until all its neighbour vertices are marked.

  mark s as visited.
  while ( Q is not empty)
       //Removing that vertex from queue,whose neighbour will be visited now
       v  =  Q.dequeue( )

      //processing all the neighbours of v  
      for all neighbours w of v in Graph G
           if w is not visited 
                    Q.enqueue( w )             //Stores w in Q to further visit its neighbour
                    mark w as visited.
A

Breadth First Search

46
Q

Following is a pseudocode for a graph traversal technique:

traversal-recursive(G, s):
    mark s as visited
    for all neighbours w of s in Graph G:
        if w is not visited:
            traversal-recursive(G, w)
A

Depth First search

47
Q

(T/F): When performing the topological sort we always find a unique solution

A

False

48
Q

(T/F): A spanning tree of a graph should contain all the edges of the graph

A

False

49
Q

(T/F): A graph can have many spanning trees

A

True

50
Q

Consider the graph M with 3 vertices. Its adjacency matrix is shown below.

M = [[0, 1, 1], [1, 0, 1], [1, 1, 0]]

This graph has ____ number of spanning trees.

  • 2
  • 1
  • 4
  • 3
A

3

51
Q

The following pseudocode is for which of the algorithms?

def mystery(G):
s <- pick a source vertex from V

for v in V:
    dist[v] = infinity
    prev[v] = Empty

#initalize source
dist[v] = 0
prev[v] = s

#update neighbouring nodes of s
for node in s.neighbours
    dist[v] = w(s,node)
    prev[v] = s

while(len(visited)<len(V)):
    CurrentNode = unvisited vertex v with smallest dist[v]
    MST.add((prevNode, CurrentNode))
    for node in CurrentNode.neighbours:
        dist[node] = min(w(CurrentNode, node), dist[node])
        if dist[node] updated: prev[node] = CurrentNode
    visited.add(CurrentNode)
return MST
A

Prim’s Algorithm

52
Q

Prim’s and Kruskal’s algorithms to find the MST follows which of the algorithm paradigms?

  • Dynamic Programming
  • Greedy Approach
  • Brute Force
  • Divide and Conquer
A

Greedy Approach

53
Q

Which of the following is true for Prim’s algorithm?

  • It can be implemented using a heap
  • It is a greedy algorithm
  • It never accepts cycles in the MST
A

all of them

54
Q

(T/F): Removing the maximum weighted edge from a Hamiltonian cycle will result in a Spanning tree

A

true

55
Q

We use reduction to prove that NP-Completeness of a problem X from A. As a part of reduction we must prove which of the following statements?

Assume A is a NP-Hard problem.

Statement P: A can be transformed to X in a polynomial time

Statement Q: We can obtain solution to A from X in polynomial time

  • P alone
  • Q alone
  • Neither P nor Q
  • Both P and Q
A

Both P and Q

56
Q

If the solution obtained by an approximation algorithm is : 10

The optimal solution is : 5

What will be the value of the approximation ratio?

A

2

57
Q

In the exploration to show that the independent set problem is NP-Complete we have used which of the following NP-Hard problems?

  • None of the options
  • 2SAT
  • 3SAT
  • Circuit SAT
A

3SAT

58
Q

(T/F):Given Problem A, that can be verified in polynomial time. If Hamiltonian Cycle problem
reduces to Problem A in polynomial time, we can say A is in NP-Complete.

A

True

59
Q

(T/F): Every problem in NP can be reduced to NP hard problems?

A

True

60
Q

Which of the following data structures can be used to implement the Dijkstra algorithm
most efficiently?

A

Min Priority Queue

61
Q

There are two problems, X and Y. 3SAT reduces to problem X and problem X reduces to
Problem Y. (These reductions are poly-time reductions.)
Is it true that: Y is not in NP-hard.

A

No, false

62
Q

a _______ is a simple strategy that we build mentally to form an approximate solution to a problem, it is not mathematical approach.

A

heuristic

63
Q

_________ is a connected acyclic graph that contains all the vertices of the graph

A

spanning tree

64
Q

if a graph has V vertices, how many spanning graphs does it have?

A

V^(V-2)

65
Q

How do we write pseudocode?

A

In a way which conveys the instructions and structure of the algorithm clearly enough for us to implement it in the language of our choice

66
Q

An _____ is a set of specific instructions intended to solve a problem.

A

algorithm

67
Q

algorithms are________ _______ _______ that can be implemented in a programming language

A

abstract mechanical procedures

68
Q

What is the time it takes to execute a program?

A

Running time

69
Q

What parameters does the running time of the BubbleSort algorithm ‘generally’ depend on (check all that apply)?

  • The programming language use to code
    thisalgorithm
  • The size the array
  • The speed of the computer: CPU
A

all of the above

70
Q

Suppose your algorithm performs multiplication of two numbers x, y. The function takes x , y as inputs. How would you express the running time?

  • T(xy)
  • T(y)
  • T(x , y)
  • T(x)
A

T(x , y)

71
Q

if you see that the problem can be solved by breaking the problem into smaller problems of the same form, then you can use ________ to solve it

A

recursion

72
Q
A