Static Questions
Static Questions
You're managing the inventory levels of different items in a warehouse. The inventory levels are
stored in an array of integers, where each integer represents the quantity of a specific item.
1. Traversal: Go through the list of inventory quantities to verify the stock levels.
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int x, n, m;
// Handle insertions
for (const auto& item : newItems) {
insertItem(inventory, item);
}
// Handle deletions
for (const auto& item : removeItems) {
removeItem(inventory, item);
}
return 0;
}
Problem Statement2:
You're managing a list of product IDs on an e-commerce platform. Each product ID is an integer
representing a unique product. You need to search for specific products in the list using two
different search methods: linear search and binary search.
Linear Search: This method is used when the list of product IDs is unsorted. You'll traverse the
entire list to find the desired product ID.
Binary Search: This method is used when the list of product IDs is sorted in ascending order.
You'll repeatedly divide the list in half to locate the desired product ID.
#include <iostream>
#include <vector>
#include <algorithm>
if (products[mid] == key) {
return mid; // Found the key at index mid
}
if (products[mid] < key) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1; // Key not found
}
int main() {
int n;
cin >> n;
vector<int> products(n);
int key;
cin >> key;
if (sorted) {
cout << "Using Binary Search" << endl;
int index = binarySearch(products, key);
if (index != -1) {
cout << key << " found at index " << index << endl;
} else {
cout << key << " not found in the list" << endl;
}
} else {
cout << "Using Linear Search" << endl;
int index = linearSearch(products, key);
if (index != -1) {
cout << key << " found at index " << index << endl;
} else {
cout << key << " not found in the list" << endl;
}
}
return 0;
}
Problem Statement3:
You're managing employee records for a company. The employee IDs from two different
departments are stored in two separate arrays, both sorted in ascending order. Your task is to
merge these two sorted arrays into a single sorted array, ensuring that the order is maintained.
Input format :
The first line of input contains an integer n, representing the number of employees in the first
department.
The second line contains n space-separated integers, each representing a sorted employee ID
from the first department.
The third line contains an integer m, representing the number of employees in the second
department.
The fourth line contains m space-separated integers, each representing a sorted employee ID
from the second department.
Output format :
The output prints the merged sorted employee IDs from both departments.
#include <iostream>
#include <vector>
return merged;
}
int main() {
int n, m;
return 0;
}
Problem Statement4:
You're an instructor managing a list of exam scores for a group of students. The scores are
stored in an array, and you need to sort this array in ascending order so that you can easily
identify the highest and lowest scores. You'll use two different sorting algorithms: bubble sort
and selection sort.
Bubble sort is selected if the array is nearly sorted. An array is considered nearly sorted if at
most one pair of elements is out of order. The selection sort is chosen if the array is not nearly
sorted.
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int n;
cin >> n;
vector<int> scores(n);
if (nearlySorted) {
cout << "Using Bubble Sort" << endl;
bubbleSort(scores);
} else {
cout << "Using Selection Sort" << endl;
selectionSort(scores);
}
return 0;
}
Problem Statement5:
You're a coach managing a list of finishing times for athletes in a race. The times are stored in
an array, and you need to sort this array in ascending order to determine the rankings.
You'll use the insertion sort algorithm to accomplish this.
Input format :
The first line of input contains an integer n, representing the number of athletes.
The second line contains n space-separated integers, each representing the finishing time of an
athlete in seconds.
Output format :
The output prints the sorted finishing times of the athletes in ascending order.
#include <iostream>
#include <vector>
int main() {
int n;
vector<int> times(n);
return 0;
}