0% found this document useful (0 votes)
14 views8 pages

Static Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

Static Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Problem Statement1:

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.

You will perform the following operations:

1. Traversal: Go through the list of inventory quantities to verify the stock levels.

2. Insertion: Add a new item to the inventory, updating the quantity.

3. Deletion: Remove an item from the inventory when it is out of stock.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// Function to print the inventory list


void printInventoryList(const vector<int>& inventory) {
cout << "Inventory List: ";
for (const auto& quantity : inventory) {
cout << quantity << " ";
}
cout << endl;
}

// Function to handle the insertion of a new item


void insertItem(vector<int>& inventory, int quantity) {
cout << "Inserting Item: " << quantity << endl;
inventory.push_back(quantity);
printInventoryList(inventory);
}

// Function to handle the removal of an item


void removeItem(vector<int>& inventory, int quantity) {
cout << "Removing Item: " << quantity << endl;
auto it = find(inventory.begin(), inventory.end(), quantity);
if (it != inventory.end()) {
inventory.erase(it);
}
printInventoryList(inventory);
}

int main() {
int x, n, m;

// Input number of items in inventory


cin >> x;
vector<int> inventory(x);

// Input inventory quantities


for (int i = 0; i < x; ++i) {
cin >> inventory[i];
}

// Print initial inventory list


printInventoryList(inventory);

// Input number of items to be inserted


cin >> n;
vector<int> newItems(n);

// Input quantities of new items


for (int i = 0; i < n; ++i) {
cin >> newItems[i];
}

// Handle insertions
for (const auto& item : newItems) {
insertItem(inventory, item);
}

// Input number of items to be removed


cin >> m;
vector<int> removeItems(m);

// Input quantities of items to be removed


for (int i = 0; i < m; ++i) {
cin >> removeItems[i];
}

// 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>

using namespace std;

// Function to perform linear search


int linearSearch(const vector<int>& products, int key) {
for (size_t i = 0; i < products.size(); ++i) {
if (products[i] == key) {
return i; // Found the key at index i
}
}
return -1; // Key not found
}

// Function to perform binary search


int binarySearch(const vector<int>& products, int key) {
int left = 0;
int right = products.size() - 1;

while (left <= right) {


int mid = left + (right - left) / 2;

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
}

// Function to check if the list is sorted


bool isSorted(const vector<int>& products) {
for (size_t i = 1; i < products.size(); ++i) {
if (products[i - 1] > products[i]) {
return false; // List is not sorted
}
}
return true; // List is sorted
}

int main() {
int n;
cin >> n;

vector<int> products(n);

for (int i = 0; i < n; ++i) {


cin >> products[i];
}

int key;
cin >> key;

// Check if the list is sorted


bool sorted = isSorted(products);

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>

using namespace std;

// Function to merge two sorted arrays


vector<int> mergeSortedArrays(const vector<int>& arr1, const vector<int>& arr2) {
vector<int> merged;
size_t i = 0, j = 0;

// Merge both arrays while maintaining order


while (i < arr1.size() && j < arr2.size()) {
if (arr1[i] < arr2[j]) {
merged.push_back(arr1[i++]);
} else {
merged.push_back(arr2[j++]);
}
}

// If there are remaining elements in arr1


while (i < arr1.size()) {
merged.push_back(arr1[i++]);
}

// If there are remaining elements in arr2


while (j < arr2.size()) {
merged.push_back(arr2[j++]);
}

return merged;
}

int main() {
int n, m;

// Read the number of employees in the first department


cin >> n;
vector<int> department1(n);

// Read the sorted employee IDs for the first department


for (int i = 0; i < n; ++i) {
cin >> department1[i];
}

// Read the number of employees in the second department


cin >> m;
vector<int> department2(m);

// Read the sorted employee IDs for the second department


for (int i = 0; i < m; ++i) {
cin >> department2[i];
}

// Merge the two sorted arrays


vector<int> merged = mergeSortedArrays(department1, department2);

// Print the merged sorted array


for (const auto& id : merged) {
cout << id << " ";
}
cout << endl;

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>

using namespace std;

// Function to perform bubble sort


void bubbleSort(vector<int>& arr) {
int n = arr.size();
bool swapped;
for (int i = 0; i < n - 1; ++i) {
swapped = false;
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
}
}
if (!swapped) break; // If no elements were swapped, array is sorted
}
}

// Function to perform selection sort


void selectionSort(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
int minIndex = i;
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
swap(arr[i], arr[minIndex]);
}
}

// Function to check if the array is nearly sorted


bool isNearlySorted(const vector<int>& arr) {
int outOfOrderCount = 0;
int n = arr.size();
for (int i = 1; i < n; ++i) {
if (arr[i] < arr[i - 1]) {
++outOfOrderCount;
if (outOfOrderCount > 1) return false;
}
}
return true;
}

int main() {
int n;
cin >> n;

vector<int> scores(n);

for (int i = 0; i < n; ++i) {


cin >> scores[i];
}

// Determine if the array is nearly sorted


bool nearlySorted = isNearlySorted(scores);

if (nearlySorted) {
cout << "Using Bubble Sort" << endl;
bubbleSort(scores);
} else {
cout << "Using Selection Sort" << endl;
selectionSort(scores);
}

// Print the sorted array


for (const auto& score : scores) {
cout << score << " ";
}
cout << endl;

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>

using namespace std;

// Function to perform insertion sort


void insertionSort(vector<int>& arr) {
int n = arr.size();
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
// Move elements of arr[0..i-1], that are greater than key, to one position ahead of their
current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
--j;
}
arr[j + 1] = key;
}
}

int main() {
int n;

// Read the number of athletes


cin >> n;

vector<int> times(n);

// Read the finishing times


for (int i = 0; i < n; ++i) {
cin >> times[i];
}

// Sort the finishing times using insertion sort


insertionSort(times);

// Print the sorted finishing times


for (const auto& time : times) {
cout << time << " ";
}
cout << endl;

return 0;
}

You might also like