What is the difference between linear search and binary search? Give an example.
Define time complexity. Why is it important in algorithm analysis?
What is space complexity? Explain with a small example.
Define recursion. Mention one problem where recursion can be used effectively.
Write a function to calculate the factorial of a number using recursion.
Write a function to calculate factorial of a number using recursion.
Given the array [5, 2, 9, 1, 5], show the array after one pass of bubble sort.
Write the logic to find the GCD of two numbers using the Euclidean algorithm.
Write a loop-based C++ code snippet to check whether a number is prime.
What is Bubble Sort? Mention its best and worst-case time complexities.
Write a function to find the GCD of two numbers using Euclidean algorithm.
Define the base condition in recursion and give one example.
Write a function to check if a number is prime.
What is Selection sort?. Write its Time Complexity.
What is the difference between merge sort and quick sort?
What is a linear search? Give its time complexity.
Write a C++ function to check if an array is sorted.
What is the worst-case time complexity of merge sort? Explain briefly.
What does the term ‘peak element’ mean in an array? Give one example.
Write the logic to reverse an array in-place.
Write a C++ function to find the largest element in an array.
What is a palindrome? Write a simple code to check if a string is palindrome.
Write a C++ function to find the largest element in an array.
What is the difference between singly and doubly linked list?
Given a string, write a function to reverse the words in it. (e.g., "I love DSA" → "DSA love
I")
What is a rotated sorted array? Give one example.
Write the logic to detect a cycle in a linked list.
What is the purpose of lower bound in binary search?
Define quick sort and mention one key difference between quick sort and merge sort.
Explain merge sort with an example. Write the recursive steps clearly.
Explain the quick sort algorithm with an example.
Explain bubble sort algorithm with an example. Write its time complexity.
Explain insertion sort with one dry run on an example array.
Explain insertion sort with a dry run on the array: [5, 3, 4, 1, 2]
Write a recursive function to reverse a string and explain how the recursion stack is used
during the process.
Write a program to delete a node from a singly linked list given its position (not value).
Explain how binary search works. Perform binary search on the array [2, 4, 6, 8, 10, 12]
to find the element 8.
Explain linear search and binary search. Highlight the differences with example arrays.
Describe singly linked list reversal with a simple C++ function.
Write a recursive function to find the Nth Fibonacci number and explain how recursion
works.
Given a sorted rotated array, write an algorithm to find the position of a target element
using binary search.
i have exam mids give me answers for this in clear way
Assistant
Sure! I'll provide clear and concise answers to all your questions. Let's dive in:
1. Define time and space complexity with an example.
Time Complexity: Measures the amount of time an algorithm takes to run as a function of the input size.
Example: A linear search has a time complexity of O(n) because it checks each element once.
Space Complexity: Measures the amount of memory an algorithm uses as a function of the input size.
Example: A program that uses a single variable to store data has a space complexity of O(1).
2. What is the difference between linear search and binary search? Give an example.
Linear Search:
Checks each element sequentially.
Time Complexity: O(n).
Example: Searching for 5 in [3, 1, 4, 5, 9] requires checking each element one by one.
Binary Search:
Works on sorted arrays by repeatedly dividing the search interval in half.
Time Complexity: O(log n).
Example: Searching for 5 in [1, 3, 4, 5, 9] starts at the middle (4), then moves right to find 5.
3. Define time complexity. Why is it important in algorithm analysis?
Definition: Time complexity measures how the runtime of an algorithm grows as the input size increases.
Importance: It helps us understand the efficiency of an algorithm and compare different algorithms for the same problem.
4. What is space complexity? Explain with a small example.
Definition: Space complexity measures the memory used by an algorithm as a function of the input size.
Example: A recursive function to calculate factorial uses O(n) space due to the recursion stack.
5. Define recursion. Mention one problem where recursion can be used effectively.
Definition: Recursion is a technique where a function calls itself to solve smaller subproblems.
: Calculating the factorial of a number (e.g., ).
Problem
factorial(5) = 5 * factorial(4)
6. Write a function to calculate the factorial of a number using recursion.
int factorial(int n) {
if (n == 0 || n == 1) // Base condition
return 1;
return n * factorial(n - 1); // Recursive call
}
7. Write a function to calculate factorial of a number using recursion.
(Same as above.)
8. Given the array [5, 2, 9, 1, 5], show the array after one pass of bubble sort.
Bubble Sort compares adjacent elements and swaps them if they are in the wrong order.
After one pass: [2, 5, 1, 5, 9].
9. Write the logic to find the GCD of two numbers using the Euclidean algorithm.
Logic:
If b == 0, return a.
Else, compute gcd(b, a % b).
10. Write a loop-based C++ code snippet to check whether a number is prime.
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
11. What is Bubble Sort? Mention its best and worst-case time complexities.
Bubble Sort: Repeatedly swaps adjacent elements if they are in the wrong order.
Best Case: O(n) (when the array is already sorted).
Worst Case: O(n²) (when the array is reverse sorted).
12. Write a function to find the GCD of two numbers using Euclidean algorithm.
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
13. Define the base condition in recursion and give one example.
Base Condition: The condition under which the recursion stops.
Example: In factorial, if (n == 0 || n == 1) return 1.
14. Write a function to check if a number is prime.
(Same as Q10.)
15. What is Selection Sort? Write its Time Complexity.
Selection Sort: Repeatedly selects the smallest element and places it at the correct position.
Time Complexity: O(n²).
16. What is the difference between merge sort and quick sort?
Merge Sort: Divides the array into halves, sorts them, and merges them. Stable and O(n log n).
Quick Sort: Picks a pivot, partitions the array, and recursively sorts. Unstable but faster in practice (O(n log n) average).
17. What is a linear search? Give its time complexity.
Linear Search: Checks each element sequentially.
Time Complexity: O(n).
18. Write a C++ function to check if an array is sorted.
bool isSorted(int arr[], int n) {
for (int i = 1; i < n; i++) {
if (arr[i] < arr[i - 1])
return false;
}
return true;
}
19. What is the worst-case time complexity of merge sort? Explain briefly.
Worst Case: O(n log n).
Explanation: Merge sort divides the array into halves (log n) and merges them in linear time (n).
20. What does the term ‘peak element’ mean in an array? Give one example.
Peak Element: An element greater than or equal to its neighbors.
Example: In [1, 3, 20, 4, 1], 20 is a peak.
21. Write the logic to reverse an array in-place.
void reverseArray(int arr[], int n) {
for (int i = 0; i < n / 2; i++) {
swap(arr[i], arr[n - i - 1]);
}
}
22. Write a C++ function to find the largest element in an array.
int largestElement(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max)
max = arr[i];
}
return max;
}
23. What is a palindrome? Write a simple code to check if a string is palindrome.
bool isPalindrome(string s) {
int left = 0, right = s.size() - 1;
while (left < right) {
if (s[left] != s[right])
return false;
left++, right--;
}
return true;
}
24. Write a C++ function to find the largest element in an array.
(Same as Q22.)
25. What is the difference between singly and doubly linked list?
Singly Linked List: Each node points to the next node only.
Doubly Linked List: Each node points to both the next and previous nodes.
26. Given a string, write a function to reverse the words in it.
string reverseWords(string s) {
vector<string> words;
string word = "";
for (char c : s) {
if (c == ' ') {
words.push_back(word);
word = "";
} else {
word += c;
}
}
words.push_back(word);
reverse(words.begin(), words.end());
string result = "";
for (auto w : words) {
result += w + " ";
}
return result.substr(0, result.size() - 1);
}
27. What is a rotated sorted array? Give one example.
Rotated Sorted Array: A sorted array rotated at some pivot.
Example: [4, 5, 6, 1, 2, 3].
28. Write the logic to detect a cycle in a linked list.
Use Floyd’s Cycle Detection Algorithm (Tortoise and Hare).
29. What is the purpose of lower bound in binary search?
Finds the first element not less than the target.
30. Define quick sort and mention one key difference between quick sort and merge sort.
Quick Sort: Picks a pivot and partitions the array.
Difference: Quick Sort is in-place; Merge Sort is not.
31. Explain merge sort with an example. Write the recursive steps clearly.
(Split, sort, merge.)
32. Explain the quick sort algorithm with an example.
(Pick pivot, partition, recurse.)
33. Explain bubble sort algorithm with an example. Write its time complexity.
(Swap adjacent elements. O(n²).)
34. Explain insertion sort with one dry run on an example array.
(Insert elements into the correct position.)
35. Explain insertion sort with a dry run on the array: [5, 3, 4, 1, 2].
(Step-by-step sorting.)
36. Write a recursive function to reverse a string and explain how the recursion stack is used during the process.
void reverseString(string &s, int left, int right) {
if (left >= right) return;
swap(s[left], s[right]);
reverseString(s, left + 1, right - 1);
}
37. Write a program to delete a node from a singly linked list given its position (not value).
(Traverse to position and unlink.)
38. Explain how binary search works. Perform binary search on the array [2, 4, 6, 8, 10, 12] to find the element 8.
(Find mid, compare, recurse.)
39. Explain linear search and binary search. Highlight the differences with example arrays.
(Linear: Sequential. Binary: Divide and conquer.)
40. Describe singly linked list reversal with a simple C++ function.