Mock Test
Mock Test
Mock Test
1. Introduction:
Finding the kth largest element in an array of integers is a common problem in computer science and
data analysis. Sorting the entire array to find the kth largest element can be inefficient, especially for
large arrays. In this program, we will implement a solution in Python that efficiently finds the kth largest
element without sorting the entire array. We will use a suitable data structure to achieve this.
2. Step-by-Step Solution:
We can efficiently find the kth largest element using a min-heap data structure. The idea is to maintain a
min-heap of size k, where the heap will always contain the k largest elements seen so far. Here's the
step-by-step solution:
3. For each element, compare it with the smallest element in the min-heap (the root of the heap).
- If the element is greater than the smallest element in the heap, remove the smallest element from
the heap and insert the current element into the heap.
- If the element is smaller or equal to the smallest element in the heap, ignore it.
4. After processing all elements in the array, the root of the min-heap will contain the kth largest
element.
return min_heap[0]
# Example usage:
arr = [3, 1, 4, 2, 5]
k=2
result = kth_largest_element(arr, k)
print(f"The {k}th largest element is: {result}")
Final Answer--
In this program, we efficiently found the kth largest element in an array of integers without sorting the
entire array. We achieved this by using a min-heap data structure to maintain the k largest elements
seen so far while iterating through the array. This approach has a time complexity of O(n * log(k)),
where n is the number of elements in the array, making it suitable for large datasets.
Question 2 Answer—
Introduction:
Simplifying Boolean expressions is a common task in digital logic design. One method to simplify these
expressions is by using Karnaugh Maps (K-maps). K-maps help us visualize and identify patterns in the
truth table to find the simplest Boolean expression. In this case, we will simplify the given Boolean
expression F(A, B, C) and its don't-care conditions using a Karnaugh Map.
Step-by-Step Solution:
Construct a K-map with three variables A, B, and C. The map should have 2^3 = 8 cells, one for each
combination of values for A, B, and C.
```
| AB\ C | 00 | 01 | 11 | 10 |
|-------|----|----|----|----|
| 00 | | | | |
| 01 | | | | |
| 11 | | | | |
| 10 | | | | |
```
Based on the given terms and don't-care conditions, place '1' in the corresponding cells of the K-map.
```
| AB\ C | 00 | 01 | 11 | 10 |
|-------|----|----|----|----|
| 00 | | | | |
| 01 | 1 | 1 | | |
| 11 | | | 1 | |
| 10 | 1 | | | |
```
Look for groups of adjacent '1' cells in the K-map. Each group should be as large as possible (2, 4, or 8
cells) and represent a term in the simplified Boolean expression.
For each group, write down the corresponding term in the simplified Boolean expression. Use the
variables A, B, and C as needed.
- Group 1: A'B'C (since all cells in this group have '0' in the C column).
- Group 2: AC' (since cell 3 has '1' for A and '0' for B and C).
- Group 3: AB' (since all cells in this group have '0' in the A column).
Final Answer:
The simplified Boolean expression for F(A, B, C) considering the given don't-care conditions is:
Question 3 Answer—
Introduction:
In database design, we often use normalization techniques to ensure that the database schema is free
from anomalies and redundancy. Two commonly used normalization forms are the Boyce-Codd Normal
Form (BCNF) and the Third Normal Form (3NF). Given the relational schemas for a library database and
the provided functional dependencies, we need to determine the correct normalization forms for the
"Book" and "Collection" tables.
Step-by-Step Solution:
Let's analyze each schema separately and determine their normalization forms:
- Catalog no -> title, Author, publisher, year (from the second functional dependency)
- publisher, title, year -> price (from the third functional dependency)
- A schema is in BCNF if, for every non-trivial functional dependency X -> Y, X is a superkey.
- In this case, the functional dependencies do not violate BCNF because (Author, Title) is the key, and
all the functional dependencies have the key on the left side.
- Given that (Author, Title) is the key for both schemas, the key for the "Collection" schema is (Author,
Title).
- There is only one functional dependency: title, Author -> Catalog no.
- title, Author -> Catalog no is a non-trivial functional dependency, but (Author, Title) is the key, and it
includes all the attributes. Therefore, the "Collection" schema does not violate BCNF.
- A schema is in 3NF if, for every non-trivial functional dependency X -> Y, X is a superkey or Y is a
prime attribute (an attribute that is part of the key).
- In this case, title, Author is not a superkey, and Catalog no is not a prime attribute.
Final Answer:
Question 4 Answer—
Introduction:
Designing an Entity-Relationship (ER) diagram is a crucial step in creating a database system. In this case,
we are designing an ER diagram for a university database system. The system needs to store information
about students, courses, departments, professors, and their relationships. This diagram will help us
visualize the entities, their attributes, relationships, and other key details.
Step-by-Step Solution:
- Attributes:
- First Name
- Last Name
- Date of Birth
- Address
- Relationships:
2. Course Entity:
- Attributes:
- Course Name
- Credits
- Relationships:
3. Department Entity:
- Attributes:
- Relationships:
4. Professor Entity:
- Attributes:
- Professor ID (Primary Key)
- First Name
- Last Name
- Relationships:
Relationships:
Cardinality Ratios:
- Belongs to a Department: Many Students, Courses, and Professors can belong to One Department.
Primary Keys:
Weak Entities:
None of the entities mentioned are weak entities. All have their own primary keys.
Question 5 Answer—
Here's a basic outline with classes for flights, passengers, reservations, and payment
processing, along with inheritance for different types of flights (domestic and
international):
C++
#include <iostream>
#include <vector>
#include <string>
class Passenger {
public:
Passenger(std::string name, std::string passportNumber)
: name(name), passportNumber(passportNumber) {}
private:
std::string name;
std::string passportNumber;
};
class Flight {
public:
Flight(std::string flightNumber, std::string origin, std::string destination, int capacity)
: flightNumber(flightNumber), origin(origin), destination(destination),
capacity(capacity) {}
protected:
std::string flightNumber;
std::string origin;
std::string destination;
int capacity;
std::vector<Passenger> passengers;
};
private:
double baggageFee;
};
private:
double tax;
};
int main() {
// Create passengers
Passenger passenger1("John Doe", "ABC12345");
Passenger passenger2("Jane Smith", "XYZ98765");
// Create flights
DomesticFlight domesticFlight("DL123", "New York", "Chicago", 100, 25.0);
InternationalFlight internationalFlight("AI456", "New York", "London", 200, 50.0);
// Make reservations
domesticFlight.addPassenger(passenger1);
domesticFlight.addPassenger(passenger2);
internationalFlight.addPassenger(passenger1);
internationalFlight.addPassenger(passenger2);
// Calculate prices
double domesticPrice = domesticFlight.calculatePrice();
double internationalPrice = internationalFlight.calculatePrice();
std::cout << "Total price for domestic flight: $" << domesticPrice << std::endl;
std::cout << "Total price for international flight: $" << internationalPrice << std::endl;
return 0;
}
Building a flight reservation system in C++ involves designing and implementing classes for flights,
passengers, reservations, and payment processing. The system should offer features like searching for
flights, making reservations, and managing bookings. Inheritance is used to distinguish between
domestic and international flights