Algorithms determine whether or not a you have understood how the code works. Conceptualising algorithms prior to an interview can not only acquaint you with them, but will also give you confidence in conveying the solution to the interviewee. Follow the blog to master top algorithm placement questions and answers.
Basic Algorithm Placement Questions
Q1. Consider a truck that shifts products from source to destination. There are N products of different weights. The truck driver can make one trip in a day and can take a maximum load of X with him. Which algorithm can be used in order to maximize the number of products he can take in one trip?
Back tracking
Greedy approach
Dynamic programming
Brute force
Answer B
Explanation:
The above problem can be solved by the algorithm i.e. Container loading problem which is a type of greedy approach.
Q2.What is the space complexity of following code snippet
int sum(int A[], int n)
{
int sum = 0, i;
for(i = 0; i< n; i++)
sum = sum + A[i];
return sum;
}
n+8
2n+16
2n+8
n^2
Answer C
Explanation: 'n*2' bytes of memory to store array variable 'a[]'. 2 bytes of memory for integer parameter 'n' 4 bytes of memory for local integer variables 'sum' and 'i‘ (2 bytes each) 2 bytes of memory for return value. That means, totally it requires '2n+8' bytes of memory to complete its execution. Here, the amount of memory depends on the input value of 'n'.
Q3. What is the time complexity of the given code snippet? (Choose the most appropriate)
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
O(n^2)
O(n)
O(3n+2)
O(n^3)
Answer B
Explanation:
Statement | Frequency Count |
i=0 | 1 |
i<n | (n+1) |
i++ | n |
sum=sum+a[i] | n |
Total | 3n+2 |
Frequency Count: 3n+2
Time Complexity is O(n) (Note: Ignore the constants value)
Q4. What is the time complexity of n nested for loops present in any code?
O(n^n)
O(n)
O(nlogn)
O(n^3)
Answer A
Explanation:
For any number of loops present in nested form, the time complexity is n raise to the power of maximum no of nested loops present in a program.
Q5 Which of the following is true about Huffman Coding.
Huffman coding may become lossy in some cases
Huffman Codes may not be optimal lossless codes in some cases
In Huffman coding, no code is prefix of any other code.
All of the above
Answer C
Explanation: Huffman coding is a lossless data compression algorithm. The codes assigned to input character are prefix codes, means the codes are assigned in such a way that the code assigned to one character is not prefix of code assigned to any other character. This is how Huffman Coding makes sure that there is no ambiguity when decoding.
Q6. Suppose you are provided with a document of 1000 lines and a string pattern whose presence is to be checked in the entire document. Which algorithm strategy is reliable to provide 100 % accurate result?
Backtracking
Brute force
Huff man coding
Activity selection
Answer B
Explanation:
Brute force technique guarantees 100 % accurate results. The technique is however avoided as it is not an efficient algorithm
Q7 What is the worst case of linear search?
O(n)
O(1)
O(n^2)
O(logn)
Answer A
Explanation:
The worst case scenario can be that the element is present at the end or not present. In both situations we have to iterate through the entire array. If the size of the array in n then we will have N checks, so time complexity is n.
Q8 Which algorithm strategy can be applied in order to find the most optimal solution between two points?
Dijkstra’s algorithm
Multi stage graph
Floyd-War shall algorithm
Knapsack
Answer. B
Explanation:
Multi multi-stage graph is a dynamic algorithm which can be used as a greedy approach does not guarantee an optimal solution.
Q9 What is the recurrence expression for Strassen’s matrix multiplication?
T(n) = 8T(n/2) + n^2
T(n) = 7T(n/2) + n^2
T(n) = 8T(n/2) + n^3
T(n) = 8T(n) + n2
Answer B
Explanation:
Strassen’s reduces the number of recursion call from 8 to 7 using its own formula which results in decrease of time complexity. Q10 Which of the following algorithm implementations is similar to that of an insertion sort?
Binary heap
Quick sort
Merge sort
Radix sort
Answer: A
Explanation:
Insertion sort is similar to that of a binary heap algorithm because of the use of temporary variable to swap.
For more Algorithm questions and answers download our free handbook below.
Mobiprep handbooks are free downloadable placement preparation resources that can help you ace any company placement process. We have curated a list of the 40 most important MCQ questions with detailed explanations to ensure the best placement preparation.
Comments