Descriptive Question
Descriptive Question
Unit - I
1. Illustrate Sieve of Eratosthenes with help of program?
2. Given a positive integer ‘n'( 1 <= n <= 10^5 ). Write a CPP code to find the largest prime factor of
given number.
3. Given an integer n, write a function that returns count of trailing zeroes in n! ?
Input: n = 20
Output: 4
Explanation: Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes.
4. Given an integer array nums, return the greatest common divisor of the smallest number and largest
number in nums. The greatest common divisor of two numbers is the largest positive integer that
evenly divides both numbers.
Example:-
Input: 5
[ 2,5,6,9,10 ]
Output: 2
5. What is the "modular multiplicative inverse" and elaborate it with example ?
6. Write down about the Extended Euclidean algorithm and demonstrate it with a suitable example.
7. Write down the Pseudo Code to find out the Greatest Common Divisor based on Euclidian
algorithm , with suitable example.
8. What is Modular arithmetic, how it will be applied for problem solving ?
9. A and B are playing a game. Players are given an integer n. On their turn, the player can add 1 to the
current integer or subtract 1. The players take turns; A starts. If after A 's move the integer is divisible
by 3, then he wins. If 10moves have passed and Vanya has not won, then B wins. Write a program
that, based on the integer n, determines who will win if both players play optimally.
10. You are given a 0-indexed integer array nums. A pair of indices i, j where 0 <= i < j < nums.length
is called beautiful if the first digit of nums[i] and the last digit of nums[j] are coprime. Return the
total number of beautiful pairs in nums.
Input:- n = 3, nums = [11,21,12]
Output:- 2 Unit - II
1. Write an algorithm that will perform an insertion node at beginning and deletion of node at end in a
single linked list.
2. Explain about Doubly liked list and design a function to insert and delete at beginning and end of the
liked list?
3. Describe an algorithm to reverse a singly linked list. Implement the function in code and explain how it
works with an example.
4. Explain the concept of a circular linked list and discuss how it differs from a regular singly linked list.
Implement a circular linked list with the following functions:- insert a node at the beginning, end, and
after a specific node and delete a node with a specific value.
5. Describe an efficient algorithm to find the middle element of a singly linked list without knowing its
length beforehand and find out the time complexity.
6. Write a C++ code to find the maximum and minimum value in a single linked list.
7. Given a singly linked list, the task is to remove every k th node of the linked list. Assume that k is
always less than or equal to the length of the Linked List.
8. Given a singly linked list. The task is to check if the given linked list is palindrome or not.
Input: head: 1->2->1->1->2->1
Output: true
Explanation: The given linked list is 1->2->1->1->2->1 , which is a palindrome and Hence, the output is
true.
9. Given a singly linked list, the task is to convert it into a circular linked list.
Examples:
Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL
Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6
10. Given a singly linked list, the task is to find the middle of the linked list. If the number of nodes are
even, then there would be two middle nodes, so return the second middle node.
Example: Input: linked list: 1->2->3->4->5
Output: 3
Explanation: There are 5 nodes in the linked list and there is one middle node whose value is 3.
Input: linked list = 10 -> 20 -> 30 -> 40 -> 50 -> 60
Output: 40
Explanation: There are 6 nodes in the linked list, so we have two middle nodes: 30 and 40, but
we will return the second middle node which is 40.
Unit - III
1. Write a C++ program to sort the elements using Selection Sort and find out the time and space
complexity.
2. Write an algorithm for Insertion Sort and explain it wit example, and find out the time and space
complexity.
3. Describe how the partitioning step works in Quick Sort and why it is crucial to the algorithm's
efficiency.
4. Explain the mechanism of the Bubble Sort algorithm. Why is it called "Bubble" Sort?
5. Explain how Merge Sort handles merging two sorted subarrays. Why is this step essential to the
algorithm's structure and efficiency?
6. Explain how Counting Sort maintains stability and why this property is important in sorting algorithms.
7. How does Linear Search handle unsorted data, and why is it often the default choice for searching in
unsorted lists?
8. What are the prerequisites for using Binary Search, and why can't it be applied to unsorted datasets?
9. A peak element is an element that is strictly greater than its neighbors. Given a 0-indexed integer
array nums, find a peak element, and return its index. If the array contains multiple peaks, return the
index to any of the peaks. You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is
always considered to be strictly greater than a neighbor that is outside the array. You must write an
algorithm that runs in O(log n) time.
Example:
Input: nums = [1,2,3,1] Output: 2
Explanation: 3 is a peak element and your function should return the index number 2.
10. Given two strings s and t, return true if t is an anagram of s, and false otherwise.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Unit - IV
1. Explain stack, its applications and implement all functionalities of stack using linked list ?
2. Explain queue, its applications and implement all functionalities of queue using linked list ?
3. Explain Queue, its applications and implement all functionalities of queue?
4. Explain stack, its applications and implement all functionalities of stack?
5. Given an expression string exp, write a program to examine whether the pairs and the orders of “{“,
“}”, “(“, “)”, “[“, “]” are correct in the given expression.
Example:
Input: exp = “[()]{}{[()()]()}”
Output: Balanced
6. Given a string, reverse it using stack.
Input: str = “Quiz”
Output: ziuQ
Input: str = “abc”
Output: cba
7. The stock span problem is a financial problem where we have a series of N daily price quotes for a
stock and we need to calculate the span of the stock’s price for all N days. The span Si of the stock’s
price on a given day i is defined as the maximum number of consecutive days just before the given day,
for which the price of the stock on the current day is less than or equal to its price on the given day.
8. The next greater element of some element x in an array is the first greater element that is to the
right of x in the same array. You are given two distinct 0-indexed integer arrays nums1 and nums2,
where nums1 is a subset of nums2. For each 0 <= i < nums1.length, find the index j such that nums1[i] ==
nums2[j] and determine the next greater element of nums2[j] in nums2. If there is no next greater
element, then the answer for this query is -1. Return an array ans of length nums1.length such
that ans[i] is the next greater element as described above.
Example:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2] Output: [-1,3,-1]
Explanation: The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
9. Given a string s, remove duplicate letters so that every letter appears once and only once. You must
make sure your result is the smallest in lexicographical order among all possible results.
Example 1:
Input: s = "bcabc" Output: "abc"
Example 2:
Input: s = "cbacdcbc" Output: "acdb"
10. Given string num representing a non-negative integer num, and an integer k, return the smallest
possible integer after removing k digits from num.
Example :
Input: num = "1432219", k = 3 Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Unit - V
1. What is Binary Search tree? Implement pre-order ,in-order, post-order tree traversing for binary
tree using recursion?
2. Explain about Depth first search algorithm and breadth first search with example?
3. Explain tree and different types of trees?
4. What is graph and explain different types of graphs with different graph representation techniques?
5. Compute DFS ,BFS for the given below Graph? Consider E as starting vertex.
6. Given an array nums sorted in non-decreasing order, return the maximum between the number of
positive integers and the number of negative integers.
In other words, if the number of positive integers in nums is pos and the number of negative integers
is neg, then return the maximum of pos and neg.
Note that 0 is neither positive nor negative.
Example :
Input: nums = [-2,-1,-1,1,2,3] Output: 3
Explanation: There are 3 positive integers and 3 negative integers. The maximum count among them is 3.
7. There is an integer array nums sorted in ascending order (with distinct values). Prior to being passed to
your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the
resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For
example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
Given the array nums after the possible rotation and an integer target, return the index of target if it is
in nums, or -1 if it is not in nums. You must write an algorithm with O(log n) runtime complexity.
Example:
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
8. You are a product manager and currently leading a team to develop a new product. Unfortunately, the
latest version of your product fails the quality check. Since each version is developed based on the
previous version, all the versions after a bad version are also bad. Suppose you have n versions [1, 2, ...,
n] and you want to find out the first bad one, which causes all the following ones to be bad. You are given
an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find
the first bad version. You should minimize the number of calls to the API.
Example:
Input: n = 5, bad = 4
Output: 4
Explanation:
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.
9. Given a sorted array of distinct integers and a target value, return the index if the target is found. If not,
return the index where it would be if it were inserted in order. You must write an algorithm with O(log
n) runtime complexity.
Example:
Input: nums = [1,3,5,6], target = 5
Output: 2
10. You are given an integer mountain array arr of length n where the values increase to a peak
element and then decrease. Return the index of the peak element. Your task is to solve it in O(log(n)) time
complexity.
Example 1:
Input: arr = [0,1,0]
Output: 1
Example 2:
Input: arr = [0,2,1,0]
Output: 1
Unit - VI
1. Explain top down and bottom up dynamic programming?
2. Explain Dynamic programming and when we apply dynamic programming approach?
3. How can the 0/1 Knapsack Problem be solved using recursive methods, and what are the challenges of
this approach compared to dynamic programming?
4. Explain how dynamic programming is used to solve the Coin Change Problem efficiently, and describe
the time complexity of this approach.
5. Describe the recursive approach to solving the Longest Common Subsequence problem. What are the
limitations of this approach, and why does dynamic programming provide a more efficient solution?
6. You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the
cost, you can either climb one or two steps. You can either start from the step with index 0, or the step
with index 1. Return the minimum cost to reach the top of the floor.
Example:
Input: cost = [10,15,20] Output: 15
Explanation: You will start at index 1. - Pay 15 and climb two steps to reach the top. The total cost is 15.
7. You are a professional robber planning to rob houses along a street. Each house has a certain amount of
money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have
security systems connected and it will automatically contact the police if two adjacent houses were
broken into on the same night. Given an integer array nums representing the amount of money of each
house, return the maximum amount of money you can rob tonight without alerting the police.
Example :
Input: nums = [1,2,3,1] Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
8. Given an integer array nums, return the length of the longest strictly increasing subsequence.
Example :
Input: nums = [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
9. You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to
maximize your profit by choosing a single day to buy one stock and choosing a different day in the
future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot
achieve any profit, return 0.
Example:
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
10. The alternating sum of a 0-indexed array is defined as the sum of the elements
at even indices minus the sum of the elements at odd indices. For example, the alternating sum
of [4,2,5,3] is (4 + 5) - (2 + 3) = 4. Given an array nums, return the maximum alternating sum of any
subsequence of nums (after reindexing the elements of the subsequence). A subsequence of an array is a
new array generated from the original array by deleting some elements (possibly none) without changing
the remaining elements' relative order. For example, [2,7,4] is a subsequence of [4,2,3,7,2,1,4] (the
underlined elements), while [2,4,2] is not.
Example :
Input: nums = [4,2,5,3]
Output: 7
Explanation: It is optimal to choose the subsequence [4,2,5] with alternating sum (4 + 5) - 2 = 7.