1. Define time and space complexity with an example. 2… · The Chat Archive1. Define time and space complexity with an example. 2. What is the difference between…
Download ▾
From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with
qwen-max-2025-01-25’s answer.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
-
Define time and space complexity with an example.
-
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
AssistantHere are clear and concise answers to your questions to help you prepare for your midterm exam:
1. Define Time and Space Complexity with an Example
- Time Complexity: Measures the time taken by an algorithm to run as a function of input size.
- Example: Linear search has a time complexity of O(n) because it checks each element once.
- Space Complexity: Measures the memory space required by an algorithm as a function of input size.
- Example: A function that stores an array of size
n has a space complexity of O(n).
2. Difference Between Linear Search and Binary Search
| Linear Search | Binary Search |
|---|
| Works on unsorted arrays | Requires a sorted array |
| Time Complexity: O(n) | Time Complexity: O(log n) |
| Checks each element sequentially | Divides the array into halves and eliminates half in each step |
Example:
- Linear Search: Searching for
5 in [2, 4, 1, 5, 3] → Checks all elements.
- Binary Search: Searching for
5 in [1, 2, 3, 4, 5] → Compares middle element and narrows down.
3. Define Time Complexity. Why is it Important?
- Time Complexity measures how the runtime of an algorithm grows with input size.
- Importance: Helps compare algorithms, optimize performance, and predict scalability.
4. What is Space Complexity? Explain with an Example
5. Define Recursion. Mention a Problem Where It Can Be Used Effectively
- Recursion: A function calling itself to solve smaller instances of the same problem.
Example Problem: Factorial Calculation (e.g., 5! = 5 * 4!).
6. Write a Function to Calculate Factorial Using Recursion
int factorial(int n) {
if (n == 0 || n == 1) // Base case
return 1;
return n * factorial(n - 1); // Recursive call
}
7. Same as Q6 (Duplicate Question)
8. One Pass of Bubble Sort on [5, 2, 9, 1, 5]
- Bubble Sort: Repeatedly swaps adjacent elements if they are in the wrong order.
- After 1st Pass:
[2, 5, 1, 5, 9] (Largest element 9 bubbles to the end).
9. Logic for GCD Using Euclidean Algorithm
- Euclidean Algorithm:
GCD(a, b) = GCD(b, a % b) until b = 0.
- Example:
GCD(48, 18) → GCD(18, 12) → GCD(12, 6) → GCD(6, 0) → 6.
10. C++ Code to Check if a Number is Prime
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
11. Bubble Sort and Its Time Complexities
- Bubble Sort: Repeatedly swaps adjacent elements to sort the array.
- Best Case: O(n) (Already sorted).
- Worst Case: O(n²) (Reverse sorted).
12. Function to Find GCD Using Euclidean Algorithm
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
13. Base Condition in Recursion with Example
- Base Condition: Stops recursion to prevent infinite loops.
- Example: In factorial,
if (n == 0) return 1; is the base case.
14. Function to Check if 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;
}
15. Selection Sort and Its Time Complexity
- Selection Sort: Repeatedly finds the minimum element and swaps it into place.
- Time Complexity: O(n²) (Always, regardless of input order).
16. Difference Between Merge Sort and Quick Sort
| Merge Sort | Quick Sort |
|---|
| Stable (order preserved) | Not stable |
| Always O(n log n) | Worst case O(n²) (rare) |
| Uses extra space | In-place sorting |
17. Linear Search and Its Time Complexity
- Linear Search: Checks each element sequentially.
- Time Complexity: O(n) (Worst case).
18. 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. Worst-Case Time Complexity of Merge Sort
- O(n log n) (Always divides the array into halves and merges).
20. Peak Element in an Array
- Peak Element: An element greater than its neighbors.
- Example: In
[1, 3, 5, 4, 2], 5 is a peak.
21. Logic to Reverse an Array In-Place
void reverseArray(int arr[], int n) {
int start = 0, end = n - 1;
while (start < end) {
swap(arr[start], arr[end]);
start++;
end--;
}
}
22. C++ Function to Find the Largest Element in an Array
int findLargest(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. Palindrome and Code to Check It
- Palindrome: A string that reads the same backward (e.g., "madam").
- Code:
bool isPalindrome(string s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s[left] != s[right]) return false;
left++;
right--;
}
return true;
}
24. Same as Q22 (Duplicate Question)
25. Difference Between Singly and Doubly Linked List
| Singly Linked List | Doubly Linked List |
|---|
Each node has next pointer | Each node has next and prev pointers |
| Traversal in one direction | Traversal in both directions |
| Less memory usage | More memory usage |
26. Function to Reverse Words in a String
string reverseWords(string s) {
reverse(s.begin(), s.end());
int start = 0;
for (int i = 0; i <= s.size(); i++) {
if (s[i] == ' ' || i == s.size()) {
reverse(s.begin() + start, s.begin() + i);
start = i + 1;
}
}
return s;
}
27. Rotated Sorted Array
- Rotated Sorted Array: A sorted array rotated at some pivot (e.g.,
[4, 5, 6, 1, 2, 3] is rotated from [1, 2, 3, 4, 5, 6]).
28. Logic to Detect a Cycle in a Linked List
- Floyd’s Cycle Detection: Use slow (
1x) and fast (2x) pointers. If they meet, a cycle exists.
29. Purpose of Lower Bound in Binary Search
- Lower Bound: Finds the first occurrence of a value ≥ target in a sorted array.
30. Quick Sort and Key Difference from Merge Sort
- Quick Sort: Picks a pivot, partitions the array, and recursively sorts subarrays.
- Key Difference: Quick Sort is in-place, while Merge Sort requires extra space.
31. Merge Sort with Example
- Steps:
- Divide the array into halves.
- Recursively sort each half.
- Merge the sorted halves.
- Example: Sorting
[3, 1, 4, 2] → Divide → Sort [3,1] and [4,2] → Merge.
32. Quick Sort Algorithm with Example
- Steps:
- Choose a pivot (e.g., last element).
- Partition the array such that elements < pivot are on the left.
- Recursively sort the left and right partitions.
- Example: Sorting
[5, 3, 8, 4] → Pivot 4 → Partition → Sort subarrays.
33. Bubble Sort Algorithm with Time Complexity
- Algorithm: Repeatedly swap adjacent elements if out of order.
- Time Complexity: O(n²) (Worst case).
34. Insertion Sort with Dry Run
- Algorithm: Builds the sorted array one element at a time.
- Dry Run on
[5, 3, 4, 1, 2]:
- Pass 1:
[3, 5, 4, 1, 2]
- Pass 2:
[3, 4, 5, 1, 2]
- Pass 3:
[1, 3, 4, 5, 2]
- Pass 4:
[1, 2, 3, 4, 5]
35. Same as Q34 (Duplicate Question)
36. Recursive Function to Reverse a String
void reverseString(string &s, int start, int end) {
if (start >= end) return;
swap(s[start], s[end]);
reverseString(s, start + 1, end - 1);
}
- Recursion Stack: Each call processes a substring until the base case (
start >= end).
37. Delete a Node from a Singly Linked List Given Position
void deleteNode(Node* head, int pos) {
if (pos == 0) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* current = head;
for (int i = 0; i < pos - 1 && current; i++) {
current = current->next;
}
if (!current || !current->next) return;
Node* temp = current->next;
current->next = current->next->next;
delete temp;
}
38. Binary Search on [2, 4, 6, 8, 10, 12] for 8
- Steps:
- Compare
8 with middle element 6 → Search right half.
- Compare
8 with middle element 8 → Found.
39. Linear Search vs Binary Search
| Linear Search | Binary Search |
|---|
| Works on unsorted arrays | Requires sorted arrays |
| Time: O(n) | Time: O(log n) |
40. Singly Linked List Reversal
Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* current = head;
while (current) {
Node* next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
41. Recursive Function for Nth Fibonacci Number
int fibonacci(int n) {
if (n <= 1) return n; // Base case
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive call
}
- Recursion Stack: Each call branches into two until reaching base cases.
42. Algorithm to Find Target in Rotated Sorted Array
- Steps:
- Find the pivot (where rotation occurs).
- Perform binary search in the appropriate half.
These answers should help you prepare effectively for your exam. Good luck! 🚀