3 849
Obunachilar
Ma'lumot yo'q24 soatlar
-297 kunlar
-12730 kunlar
Postlar arxiv
3 849
Single File Programming Question
Problem Statement
You are tasked with implementing a double-ended queue data structure using a linked list. A deque is a linear data structure that supports operations for adding and removing elements at both ends.
Your program should provide the following functionality:
Initialize an empty deque.
Check if the deque is empty.
Insert elements in the deque.
Display the odd and even elements separately.
Input format :
The input consists of the elements that should be inserted in the deque.
The input is terminated by entering -1.
Output format :
The output prints the even and odd elements in the given deque.
Refer to the sample output for the exact text and format.
Sample test cases :
Input 1 :
1
2
3
4
-1
Output 1 :
Even elements: 2 4
Odd elements: 1 3
Input 2 :
1
2
-1
Output 2 :
Even elements: 2
Odd elements: 1
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test ca
3 849
#include <iostream>
#include <queue>
#include <string>
#include <iomanip>
using namespace std;
// Structure to represent a flight
struct Flight {
string flightNumber;
int departureTime;
double ticketPrice;
// Overload the '<' operator to prioritize flights based on departure time and ticket price
bool operator<(const Flight& other) const {
if (departureTime != other.departureTime) {
return departureTime > other.departureTime;
}
return ticketPrice > other.ticketPrice;
}
};
int main() {
priority_queue<Flight> flights; // Priority queue to manage flights
int choice;
while (true) {
cin >> choice;
if (choice == 1) {
Flight flight;
cin >> flight.flightNumber;
cin >> flight.departureTime;
//awasthi
cin >> flight.ticketPrice;
flights.push(flight);
cout << "Flight added to the system." << endl;
} else if (choice == 2) {
if (flights.empty()) {
cout << "No flights available." << endl;
} else {
Flight bookedFlight = flights.top();
flights.pop();
cout << "Booked ticket for flight: " << bookedFlight.flightNumber << endl;
}
} else if (choice == 3) {
if (flights.empty()) {
cout << "No flights available." << endl;
} else {
Flight nextFlight = flights.top();
cout << "Next available flight: " << nextFlight.flightNumber << endl;
}
} else if (choice == 4) {
cout << "Exiting the application." << endl;
break;
} else {
cout << "Invalid choice." << endl;
}
}
return 0;
}
3 849
Single File Programming Question
Problem Statement
Your task is to create a basic airline flight reservation system. This system allows users to manage available flights, book tickets, and view information about upcoming flights.
The program employs a priority queue to efficiently manage the flights based on their departure times and ticket prices. Each flight is represented by its flight number, departure time, and ticket price.
The program offers the following options: Adding a flight, Booking a ticket, Viewing the next available flight, and Exit.
Input format :
The input consists of an integer representing the choice from the menu.
Choice 1: Add Flight
Choice 2: Book Ticket
Choice 3: View the next available flight
Choice 4: Exit
For choice 1, the program expects the following inputs on separate lines:
Flight number (string)
Departure time (integer)
Ticket price (floating-point number)
For choice 2 and for choice 3 and choice 4, no additional input is required.
Output format :
The program outputs messages based on the user's choices.
When adding a flight to the system, the program outputs: "Flight added to the system."
When booking a ticket for a flight, the program outputs: "Booked ticket for flight: [Flight Number]"
A lower departure time indicates the highest priority.
When viewing the next available flight, the program outputs: "Next available flight: [Flight Number]"
When exiting the application, the program outputs: "Exiting the application."
For invalid choices, the program outputs: "Invalid choice."
When there are no flights available, the program outputs: "No flights available."
Code constraints :
The departure time should be an integer between 0 and 23 (inclusive).
The ticket price can be a floating-point number.
The flight number should be a non-empty string.
The choice entered by the user should be an integer corresponding to the available menu options.
Sample test cases :
Input 1 :
1
BA456
8
350.50
1
DL789
10
275.75
2
2
4
Output 1 :
Flight added to the system.
Flight added to the system.
Booked ticket for flight: BA456
Booked ticket for flight: DL789
Exiting the application.
Input 2 :
1
UA987
14
400.25
2
3
4
Output 2 :
Flight added to the system.
Booked ticket for flight: UA987
No flights available.
Exiting the application.
Input 3 :
2
4
Output 3 :
No flights available.
Exiting the application.
Input 4 :
5
4
Output 4 :
Invalid choice.
Exiting the application.
Input 5 :
1
AA123
8
200.50
1
BA456
10
300.75
1
DL789
9
250.64
2
2
2
4
Output 5 :
Flight added to the system.
Flight added to the system.
Flight added to the system.
Booked ticket for flight: AA123
Booked ticket for flight: DL789
Booked ticket for flight: BA456
Exiting the application.
Input 6 :
1
AA123
12
250.50
3
4
Output 6 :
Flight added to the system.
Next available flight: AA123
Exiting the application.
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
3 849
#include <iostream>
using namespace std;
class OrderQueue {
private:
int* orders;
int front;
int rear;
int capacity;
int size;
public:
OrderQueue(int cap) {
capacity = cap;
orders = new int[capacity];
front = rear = -1;
size = 0;
}
bool isFull() {
return size == capacity;
}
bool isEmpty() {
return size == 0;
}
void insertOrder(int orderID) {
if (isFull()) {
cout << "Queue is full." << endl;
return;
}
if (isEmpty()) {
front = rear = 0;
} else {
rear = (rear + 1) % capacity;
}
orders[rear] = orderID;
size++;
cout << "Order ID " << orderID << " is inserted in the queue." << endl;
}
int processOrder() {
if (isEmpty()) {
cout << "Queue is empty." << endl;
return -1; // Return -1 to indicate an empty queue
}
int processedOrder = orders[front];
front = (front + 1) % capacity;
size--;
cout << "Processed Order ID: " << processedOrder << endl;
return processedOrder;
}
void displayQueue() {
if (isEmpty()) {
cout << "Queue is empty." << endl;
return;
}
cout << "Order IDs in the queue are:";
int i = front;
for (int count = 0; count < size; count++) {
cout << " " << orders[i];
i = (i + 1) % capacity;
}
cout << endl;
}
~OrderQueue() {
delete[] orders;
}
};
int main() {
OrderQueue orderQueue(5);
int option, orderID;
while (cin >> option) {
switch (option) {
case 1:
cin >> orderID;
if (orderID == -1) {
return 0; // Exit the program if -1 is entered
}
orderQueue.insertOrder(orderID);
break;
case 2:
orderQueue.processOrder();
break;
case 3:
orderQueue.displayQueue();
break;
default:
cout << "Invalid option." << endl;
break;
}
}
//awasthi
return 0;
}
3 849
Single File Programming Question
Problem Statement
You are developing an order processing system for a company. To efficiently manage incoming orders, you decide to implement a queue data structure using an array. The queue will store order IDs.
Implement the following operations:
Insert Order: Add an order ID to the end of the queue.
Process Order: Remove and process the next order ID from the queue.
Display Queue: Display the order IDs in the queue.
Input format :
The input consists of an integer option representing the action to be performed:
Option 1: Enqueue a new order ID into the queue. The next line contains an integer representing the element to be inserted.
Option 2: Dequeue an order ID from the queue for processing.
Option 3: Display the list of order IDs currently in the queue.
Output format :
The program provides appropriate outputs based on the choice:
When enqueuing an order (option 1), the program outputs the order ID that is inserted into the queue.
When dequeuing an order (option 2), the program outputs the order ID that is being processed.
When displaying the order IDs (option 3), the program shows the order IDs in the queue.
If an enqueue operation is attempted when the queue is full, the program outputs "Queue is full."
If a dequeue operation is attempted when the queue is empty, the program outputs "Queue is empty."
If the user provides an invalid option, the program outputs an "Invalid option."
Refer to the sample output for the exact text and format.
Code constraints :
The maximum size of the queue is defined as max = 5.
The queue can store integer values.
Each order is identified by a unique positive integer order ID.
Sample test cases :
Input 1 :
1
10
3
Output 1 :
Order ID 10 is inserted in the queue.
Order IDs in the queue are: 10
Input 2 :
1
30
1
40
2
3
Output 2 :
Order ID 30 is inserted in the queue.
Order ID 40 is inserted in the queue.
Processed Order ID: 30
Order IDs in the queue are: 40
Input 3 :
3
4
Output 3 :
Queue is empty.
Invalid option.
Input 4 :
1
10
1
20
1
30
1
40
1
50
1
60
Output 4 :
Order ID 10 is inserted in the queue.
Order ID 20 is inserted in the queue.
Order ID 30 is inserted in the queue.
3 849
#include <iostream>
using namespace std;
const int MAX_SIZE = 100;
class Queue {
private:
int arr[MAX_SIZE];
int front;
int rear;
int size;
public:
Queue() {
front = 0;
rear = -1;
size = 0;
}
bool isEmpty() {
return size == 0;
}
bool isFull() {
return size == MAX_SIZE;
}
void enqueue(int element) {
if (!isFull()) {
rear = (rear + 1) % MAX_SIZE;
arr[rear] = element;
size++;
}
}
int dequeue() {
if (!isEmpty()) {
int element = arr[front];
front = (front + 1) % MAX_SIZE;
size--;
return element;
}
return -1; // Sentinel value for error
}
void display() {
for (int i = front; i <= rear; i++) {
cout << arr[i];
if (i < rear) {
cout << " ";
}
}
cout << endl;
}
};
int main() {
int N;
cin >> N;
Queue queue;
for (int i = 0; i < N; i++) {
int registrationID;
cin >> registrationID;
if (registrationID % 2 == 0) {
queue.enqueue(registrationID);
} else {
cout << "Invalid element " << registrationID << ", only even numbers can be enqueued" << endl;
}
}
queue.display();
//awasthi
return 0;
}
3 849
Single File Programming Question
Problem Statement
You are designing an event registration system for a conference.
As part of the system, you need to implement a Queue data structure using an array that stores only the even numbers representing the registration IDs of the participants.
The queue will be used to keep track of the order in which participants register for the event.
Input format :
The first line of input consists of an integer N, representing the number of participants to register.
The following N lines consist of integers, representing the registration IDs of the participants.
Output format :
The output prints only the even number registration IDs of the participants in the order they registered, separated by space.
For odd registration IDs, print "Invalid element Only even numbers can be enqueued".
Code constraints :
1 <= N <= 100
Sample test cases :
Input 1 :
6
2
4
6
8
10
12
Output 1 :
2 4 6 8 10 12
Input 2 :
4
14
36
55
48
Output 2 :
Invalid element 55, only even numbers can be enqueued
14 36 48
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
3 849
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
using namespace std;
int precedence(char op) {
if (op == '+' op == '-') {
return 1;
} else if (op == '*' op == '/') {
return 2;
} else if (op == '^') {
return 3;
}
return 0; // Default precedence for operands
}
string infixToRPN(const string& infix) {
stack<char> operators;
string rpn = "";
for (char c : infix) {
if (isdigit(c)) {
rpn += c;
rpn += ' '; // Separate operands by space
} else if (c == '+' c == '-' c == '*' c == '/' c == '^') {
while (!operators.empty() && operators.top() != '(' &&
precedence(operators.top()) >= precedence(c)) {
rpn += operators.top();
rpn += ' '; // Separate operators by space
operators.pop();
}
operators.push(c);
} else if (c == '(') {
operators.push(c);
} else if (c == ')') {
while (!operators.empty() && operators.top() != '(') {
rpn += operators.top();
rpn += ' '; // Separate operators by space
operators.pop();
}
if (!operators.empty() && operators.top() == '(') {
operators.pop(); // Pop '('
}
}
}
while (!operators.empty()) {
rpn += operators.top();
rpn += ' '; // Separate operators by space
operators.pop();
}
return rpn;
}
int main() {
string infixExpression;
getline(cin, infixExpression);
string rpnExpression = infixToRPN(infixExpression);
//awasthi
cout << "The Reverse Polish Notation (RPN) expression is: " << rpnExpression << endl;
return 0;
}
3 849
Single File Programming Question
Problem Statement
You are developing a program that converts infix expressions into Reverse Polish Notation (RPN). To ensure the conversion function works correctly, you want to create a scenario where a user inputs an infix expression, and your program then converts it to RPN.
Here's a program that asks the user to input an infix expression and then converts it to Reverse Polish Notation (RPN). You can test it by providing an example infix expression and checking the RPN output.
Note: This is a sample question asked in an HCL Interview.
Input format :
The input consists of a single string representing an infix expression, where each operator and operand is separated by a space.
The operands in the input expression will consist of digits (0–9).
The operators in the input expression will be limited to +, -, *, /, and ^ (exponentiation).
Output format :
The output consists of a string in Reverse Polish Notation (RPN) for the provided input. Each operator and operand in the output are separated by a space. The format to print the output:
"The Reverse Polish Notation (RPN) expression is: <>"
Refer to the sample output for the exact format.
Code constraints :
Stack Size<=100
The input should be given without any parentheses.
Sample test cases :
Input 1 :
4 * 5 + 6
Output 1 :
The Reverse Polish Notation (RPN) expression is: 4 5 * 6 +
Input 2 :
5 + 3 * 2 ^ 4
Output 2 :
The Reverse Polish Notation (RPN) expression is: 5 3 2 4 ^ * +
Input 3 :
4 / 2 - 1
Output 3 :
The Reverse Polish Notation (RPN) expression is: 4 2 / 1 -
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
3 849
#include <iostream>
#include <stack>
#include <string>
#include <sstream>
#include <cctype>
#include <climits> // Include this header for INT_MIN
using namespace std;
int evaluateRPNExpression(const string& expression) {
stack<int> operands;
istringstream iss(expression);
string token;
while (iss >> token) {
if (isdigit(token[0])) {
operands.push(stoi(token));
} else if (token == "+" token == "-" token == "*" || token == "/") {
if (operands.size() < 2) {
cerr << "Error: Not enough operands for operator." << endl;
return INT_MIN; // Return a sentinel value for error
}
int operand2 = operands.top();
operands.pop();
int operand1 = operands.top();
operands.pop();
int result;
if (token == "+") {
result = operand1 + operand2;
} else if (token == "-") {
result = operand1 - operand2;
} else if (token == "*") {
result = operand1 * operand2;
} else if (token == "/") {
if (operand2 == 0) {
cerr << "Error: Division by zero." << endl;
return INT_MIN; // Return a sentinel value for error
}
result = operand1 / operand2;
}
operands.push(result);
} else {
cerr << "Error: Invalid token in expression." << endl;
return INT_MIN; // Return a sentinel value for error
}
}
if (operands.size() == 1) {
return operands.top();
} else {
cerr << "Error: Invalid expression format." << endl;
} }
int main() {
string expression;
getline(cin, expression);
int result = evaluateRPNExpression(expression);
if (result != INT_MIN) {
cout << "The result is: " << result << endl;
} else {
cerr << "Invalid expression format." << endl;
}
return 0;
}
3 849
Single File Programming Question
Problem Statement
You are developing a calculator application that supports evaluating arithmetic expressions in Reverse Polish Notation (RPN). As part of the testing process, you need to verify the correctness of your implementation. You decide to create a scenario where a user enters an RPN expression, and your application should correctly evaluate and display the result.
Write a program that allows a user to input an arithmetic expression in RPN and then evaluate and display the result using a stack-based approach. Test your code by providing an example RPN expression and its expected result.
Note: This is a sample question asked in a Capgemini interview.
Input format :
The input is a string representing the RPN expression. A space separates each value in the input expression.The operators can be addition (+), subtraction (-), multiplication (*), or division (/).
Output format :
The output evaluates the RPN expression of the given input string and displays an integer value as the output in the format:
"The result is: <>"
Refer to the sample output for the formatting specifications.
Code constraints :
Stack_size<=100
Values in input expression > 0
Sample test cases :
Input 1 :
2 1 + 3 *
Output 1 :
The result is: 9
Input 2 :
100 - 50 / 2 + 25 * 3
Output 2 :
The result is: 3
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
3 849
#include <iostream>
#include <stack>
#include <string>
using namespace std;
// Function to evaluate a postfix expression
int evaluatePostfixExpression(string expression) {
stack<int> operands;
// Iterate through each character in the expression
for (char ch : expression) {
if (isdigit(ch)) {
// If the character is a digit, push it onto the stack
operands.push(ch - '0'); // Convert char to int
} else {
// If the character is an operator, pop the top two values from the stack
int operand2 = operands.top();
operands.pop();
int operand1 = operands.top();
operands.pop();
// Perform the operation based on the operator
switch (ch) {
case '+':
operands.push(operand1 + operand2);
break;
case '-':
operands.push(operand1 - operand2);
break;
case '*':
operands.push(operand1 * operand2);
break;
case '/':
operands.push(operand1 / operand2);
break;
default:
cerr << "Invalid operator: " << ch << endl;
exit(1);
}
}
}
// The final result should be on top of the stack
return operands.top();
}
int main() {
string postfixExpression;
cout << "Enter a postfix expression: ";
cin >> postfixExpression;
int result = evaluatePostfixExpression(postfixExpression);
cout << "Result: " << result << endl;
//awasthi
return 0;
}
3 849
Single File Programming Question
Problem Statement
Raja is working on a programming project that involves evaluating postfix expressions. He needs a tool that can help him efficiently compute the results of such expressions.
You are an experienced programmer, so Raja asks for your help in creating a postfix expression evaluator.
Write a program to help Raja evaluate postfix expressions and get the desired results.
Note: This is a sample question asked in CTS recruitment.
Input format :
The input consists of a postfix mathematical expression.
The expression will contain integers and arithmetic operators (+, -, *, /) without any space.
Output format :
The output prints the result of evaluating the given postfix expression.
Code constraints :
Max_Size = 100
The operators that the program supports are: +, -, *, and /.
The input expression should not exceed 100 characters in length.
Sample test cases :
Input 1 :
45+6*
Output 1 :
54
Input 2 :
47+
Output 2 :
11
Input 3 :
523*+84/-3+
Output 3 :
12
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
3 849
#include <iostream>
using namespace std;
const int max_n = 15; // Maximum stack size
class Stack {
private:
int arr[max_n];
int top;
public:
Stack() {
top = -1;
}
void push(int x) {
if (top == max_n - 1) {
cout << "Stack Overflow" << endl;
return;
}
arr[++top] = x;
}
bool pop(int &x) {
if (isEmpty()) {
return false;
}
x = arr[top--];
return true;
}
void display() {
if (isEmpty()) {
cout << "Stack is empty." << endl;
return;
}
for (int i = 0; i <= top; i++) {
cout << arr[i] << " ";
}
}
bool isEmpty() {
return top == -1;
}
};
int main() {
int n;
cin >> n;
if (n > max_n) {
cout << "Stack Overflow" << endl;
return 0;
}
Stack stack;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
stack.push(x);
}
if (stack.isEmpty()) {
cout << "Stack is empty." << endl;
} else {
stack.display();
cout << endl;
int poppedValue;
stack.pop(poppedValue);
cout << "Top element: " << poppedValue << endl;
stack.display();
}
//awasthi
return 0;
}
3 849
Single File Programming Question
Problem Statement
Lala is studying data structures and wants to practice implementing a stack data structure using an array. She is looking for a program that allows her to push elements onto a stack, pop elements from it, and check the top element. Can you help Lala by writing an array program that demonstrates these stack operations?
Implements a stack and performs the following operations:
push(int x): Add an integer element x to the top of the stack.
pop(int &x): Remove and retrieve the top element from the stack, storing it in variable x.
display(): Display all elements in the stack.
isEmpty(): Check if the stack is empty.
Note: This is a sample question asked in a Capgemini recruitment.
Input format :
The first line contains an integer n, representing the number of elements Lala wants to push onto the stack.
The next line contains n space-separated integers, each representing the elements to be pushed onto the stack.
Output format :
The output displays the following information:
After pushing elements onto the stack, it prints the elements on the stack.
If the top element is popped from the stack successfully, it prints the popped value and the updated stack elements.
If the stack is empty after popping, it prints, "Stack is empty."
If there is a stack overflow (n >= 16) (i.e., attempting to push more elements than the stack can hold), it prints "Stack Overflow."
Refer to the sample output for the formatting specifications.
Code constraints :
max_n= 15
1 <= n <= 15
1 <= elements <= 100
3 849
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
//awasthi
vector<int> findNextGreaterElement(const vector<int>& arr) {
int n = arr.size();
stack<int> s;
vector<int> result(n, -1); // Initialize the result array with -1.
for (int i = 0; i < n; i++) {
// While the stack is not empty and the current element is greater than
// the element at the top of the stack, set the NGE for the top element.
while (!s.empty() && arr[i] > arr[s.top()]) {
result[s.top()] = arr[i];
s.pop();
}
// Push the current element's index onto the stack.
s.push(i);
}
return result;
}
int main() {
int n;
cin >> n;
vector<int> elements(n);
for (int i = 0; i < n; i++) {
cin >> elements[i];
}
// Find the Next Greater Element for each element in the array.
vector<int> ngeList = findNextGreaterElement(elements);
// Print the result in the specified format.
for (int i = 0; i < n; i++) {
cout << elements[i] << " " << ngeList[i] << endl;
}
return 0;
}
3 849
Single File Programming Question
Problem Statement
Nayana is given an array of integers and wants to find the Next Greater Element (NGE) for each element in the array. The NGE for an element arr[i] is the first greater element to the right of arr[i] in the array. If there is no greater element to the right, the NGE is considered -1. She needs your help to write a program to find the Next Greater Element (NGE) for each element in an array using a stack-based approach.
Examples:
a) For any array, the rightmost element always has the next greater element as -1.
b) For an array that is sorted in decreasing order, all elements have the next greater element as -1.
Note: This is a sample question asked in Capgemini recruitment.
Input format :
The first line contains an integer n, representing the number of elements in the array.
The next line contains n space-separated integers, representing the elements in the array.
Output format :
The output prints the next greater element for each element in the array.
Code constraints :
1 <= n <= 10
1 <= elements <= 100
