Showing posts with label worst case. Show all posts
Showing posts with label worst case. Show all posts

Saturday, October 10, 2020

Analysis and Design of Algorithms Multiple choice questions

 1.Recursion is similar to which of the following?
 Switch Case
 Loop
 If-else
 if elif else

2.In recursion, the condition for which the function will stop calling itself is ____________
 Best case
 Worst case
 Base case
 There is no such condition

3.Consider the following code snippet:
void my_recursive_function()
{
my_recursive_function();
}
int main()
{
my_recursive_function();
return 0;
}

What will happen when the above snippet is executed?
 The code will be executed successfully and no output will be generated
 The code will be executed successfully and random output will be generated
 The code will show a compile-time error
 The code will run for some time and stop when the stack overflows

4.What is the output of the following code?
void my_recursive_function(int n)
{
if(n == 0)
return;
printf("%d ",n);
my_recursive_function(n-1);
}
int main()
{
my_recursive_function(10);
return 0;
}
 10
 1
 10 9 8 ... 1 0
 10 9 8 ... 1

5.What is the base case for the following code?
void my_recursive_function(int n)
{
if(n == 0)
return;
printf("%d ",n);
my_recursive_function(n-1);
}
int main()
{
my_recursive_function(10);
return 0;
}

 Return
 printf(“%d “, n)
 if(n == 0)
 My_recursive_function(n-1)

6.How many times is the recursive function called, when the following code is executed?
void my_recursive_function(int n)
{
if(n == 0)
return;
printf("%d ",n);
my_recursive_function(n-1);
}
int main()
{
my_recursive_function(10);
return 0;
}

 9
 10
 11
 12

7.Which of the following statements is true?
 Recursion is always better than iteration
 Recursion uses more memory compared to iteration
 Recursion uses less memory compared to iteration
 Iteration is always better and simpler than recursion

8.What will be the output of the following code?
int cnt=0;
void my_recursive_function(int n)
{
if(n == 0)
return;
cnt++;
my_recursive_function(n/10);
}
int main()
{
my_recursive_function(123456789);
printf("%d",cnt);
return 0;
}

 123456789
 10
 0
 9

9.
void my_recursive_function(int n)
{
if(n == 0)
{
printf("False");
return;
}
if(n == 1)
{
printf("True");
return;
}
if(n%2==0)
my_recursive_function(n/2);
else
{
printf("False");
return;
}
}
int main()
{
my_recursive_function(100);
return 0;
}

 True
 False

10.What is the output of the following code?
int cnt = 0;
void my_recursive_function(char *s, int i)
{
if(s[i] == '\0')
return;
if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
cnt++;
my_recursive_function(s,i+1);
}
int main()
{
my_recursive_function("thisisrecursion",0);
printf("%d",cnt);
return 0;
}

 6
 9
 5
 10

11.What is the output of the following code?
void my_recursive_function(int *arr, int val, int idx, int len)
{
if(idx == len)
{
printf("-1");
return ;
}
if(arr[idx] == val)
{
printf("%d",idx);
return;
}
my_recursive_function(arr,val,idx+1,len);
}
int main()
{
int array[10] = {7, 6, 4, 3, 2, 1, 9, 5, 0, 8};
int value = 2;
int len = 10;
my_recursive_function(array, value, 0, len);
return 0;
}

 3
 4
 5
 6

12.______is the first step in solving the problem
 Understanding the Problem
 Identify the Problem
 Evaluate the Solution
 None of these

13.______is the last step in solving the problem
 Understanding the Problem
 Identify the Problem
 Evaluate the Solution
 None of these

14.Following is true for understanding of a problem
 Knowing the knowledgebase
 Understanding the subject on which the problem is based
 Communication with the client
 All of the above

15.The six-step solution for the problem can be applied to
I. Problems with Algorithmic Solution
II. Problems with Heuristic Solution
 Only I
 Only II
 Both I and II
 Neither I nor II

16.______ solution requires reasoning built on knowledge and experience
 Algorithmic Solution
 Heuristic Solution
 Random Solution
 None of these

17.The correctness and appropriateness of ___________solution can be checked very easily.
 Algorithmic solution
 Heuristic solution
 Random solution
 None of these

18.The branch of computer that deals with heuristic types of problem is called _________________.
 System software
 Real time software
 Artificial intelligence
 None of these

19.Artificial Intelligence makes use of following prominently
 Database
 Data Warehouse
 Knowledge base
 None of these

20.Naming convention for variable is followed in company because ____________.
 It enhances readability
 It allows to work without conflicts
 It enhances the efficiency
 All of the above

Tuesday, June 02, 2020

Time complexities and asymptotic notation

Searching

Algorithm

Data Structure

Time Complexity

Space Complexity

Average

Worst

Worst

Depth First Search (DFS)

Graph of |V| vertices and |E| edges

-

O(|E| + |V|)

O(|V|)

Breadth First Search (BFS)

Graph of |V| vertices and |E| edges

-

O(|E| + |V|)

O(|V|)

Binary search

Sorted array of n elements

O(log(n))

O(log(n))

O(1)

Linear (Brute Force)

Array

O(n)

O(n)

O(1)

Shortest path by Dijkstra,
using a Min-heap as priority queue

Graph with |V| vertices and |E| edges

O((|V| + |E|) log |V|)

O((|V| + |E|) log |V|)

O(|V|)

Shortest path by Dijkstra,
using an unsorted array as priority queue

Graph with |V| vertices and |E| edges

O(|V|^2)

O(|V|^2)

O(|V|)

Shortest path by Bellman-Ford

Graph with |V| vertices and |E| edges

O(|V||E|)

O(|V||E|)

O(|V|)

 

Sorting

Algorithm

Data Structure

Time Complexity

Worst Case Auxiliary Space Complexity

Best

Average

Worst

Worst

Quicksort

Array

O(n log(n))

O(n log(n))

O(n^2)

O(n)

Mergesort

Array

O(n log(n))

O(n log(n))

O(n log(n))

O(n)

Heapsort

Array

O(n log(n))

O(n log(n))

O(n log(n))

O(1)

Bubble Sort

Array

O(n)

O(n^2)

O(n^2)

O(1)

Insertion Sort

Array

O(n)

O(n^2)

O(n^2)

O(1)

Select Sort

Array

O(n^2)

O(n^2)

O(n^2)

O(1)

Bucket Sort

Array

O(n+k)

O(n+k)

O(n^2)

O(nk)

Radix Sort

Array

O(nk)

O(nk)

O(nk)

O(n+k)

 

Heaps

Heaps

Heapify

Find Max

Extract Max

Increase Key

Insert

Delete

Merge

Linked List (sorted)

-

O(1)

O(1)

O(n)

O(n)

O(1)

O(m+n)

Linked List (unsorted)

-

O(n)

O(n)

O(1)

O(1)

O(1)

O(1)

Binary Heap

O(n)

O(1)

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(m+n)

Binomial Heap

-

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(log(n))

Fibonacci Heap

-

O(1)

O(log(n))*

O(1)*

O(1)

O(log(n))*

O(1)

 

Graphs

Node / Edge Management

Storage

Add Vertex

Add Edge

Remove Vertex

Remove Edge

Query

Adjacency list

O(|V|+|E|)

O(1)

O(1)

O(|V| + |E|)

O(|E|)

O(|V|)

Incidence list

O(|V|+|E|)

O(1)

O(1)

O(|E|)

O(|E|)

O(|E|)

Adjacency matrix

O(|V|^2)

O(|V|^2)

O(1)

O(|V|^2)

O(1)

O(1)

Incidence matrix

O(|V| |E|)

O(|V| |E|)

O(|V| |E|)

O(|V| |E|)

O(|V| |E|)

O(|E|)

 

Data Structures

Data Structure

Time Complexity

Space Complexity

Average

Worst

Worst

Indexing

Search

Insertion

Deletion

Indexing

Search

Insertion

Deletion

Basic Array

O(1)

O(n)

-

-

O(1)

O(n)

-

-

O(n)

Dynamic Array

O(1)

O(n)

O(n)

O(n)

O(1)

O(n)

O(n)

O(n)

O(n)

Singly-Linked List

O(n)

O(n)

O(1)

O(1)

O(n)

O(n)

O(1)

O(1)

O(n)

Doubly-Linked List

O(n)

O(n)

O(1)

O(1)

O(n)

O(n)

O(1)

O(1)

O(n)

Skip List

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(n)

O(n)

O(n)

O(n)

O(n log(n))

Hash Table

-

O(1)

O(1)

O(1)

-

O(n)

O(n)

O(n)

O(n)

Binary Search Tree

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(n)

O(n)

O(n)

O(n)

O(n)

Cartesian Tree

-

O(log(n))

O(log(n))

O(log(n))

-

O(n)

O(n)

O(n)

O(n)

B-Tree

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(n)

Red-Black Tree

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(n)

Splay Tree

-

O(log(n))

O(log(n))

O(log(n))

-

O(log(n))

O(log(n))

O(log(n))

O(n)

AVL Tree

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(log(n))

O(n)

 

Notation for asymptotic growth

letter

bound

growth

(theta) Θ

upper and lower, tight[1]

equal[2]

(big-oh) O

upper, tightness unknown

less than or equal[3]

(small-oh) o

upper, not tight

less than

(big omega) Ω

lower, tightness unknown

greater than or equal

(small omega) ω

lower, not tight

greater than


Search Aptipedia