You are on page 1of 13

Question 1 Answer--

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:

1. Initialize an empty min-heap.

2. Iterate through the elements in the array one by one.

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.

implemented solution in Python:


import heapq

def kth_largest_element(arr, k):


min_heap = []

for num in arr:


if len(min_heap) < k:
heapq.heappush(min_heap, num)
else:
if num > min_heap[0]:
heapq.heappop(min_heap)
heapq.heappush(min_heap, num)

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:

1. Create the Karnaugh Map (K-map):

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

```

2. Fill in the K-map with 1s:

Based on the given terms and don't-care conditions, place '1' in the corresponding cells of the K-map.

- For F(A, B, C) (0, 1, 2, 4, 5), place '1' in cells (0, 1, 2, 4, 5).

- For D(3, 6), place '1' in cells (3, 6).

The K-map will look like this:

```
| AB\ C | 00 | 01 | 11 | 10 |

|-------|----|----|----|----|

| 00 | | | | |

| 01 | 1 | 1 | | |

| 11 | | | 1 | |

| 10 | 1 | | | |

```

3. Group adjacent '1' cells:

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.

In this case, there are three groups of '1' cells:

- Group 1: Cells (0, 1, 2)

- Group 2: Cells (3)

- Group 3: Cells (4, 5, 6)

4. Write down the simplified 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).

5. Combine the terms:


Combine the terms from step 4 using the OR operator ( + ) to obtain the final simplified Boolean
expression.

Final Simplified Expression: F(A, B, C) = A'B'C + AC' + AB'

Final Answer:

The simplified Boolean expression for F(A, B, C) considering the given don't-care conditions is:

F(A, B, C) = A'B'C + AC' + AB'

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:

For the "Book" Schema:

1. Identify the key:


- Given that (Author, Title) is the key for both schemas, the key for the "Book" schema is (Author,
Title).

2. Determine functional dependencies:

- title, Author -> Catalog no (from the first functional dependency)

- Catalog no -> title, Author, publisher, year (from the second functional dependency)

- publisher, title, year -> price (from the third functional dependency)

3. Check for BCNF:

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

- Therefore, the "Book" schema is in BCNF.

For the "Collection" Schema:

1. Identify the key:

- Given that (Author, Title) is the key for both schemas, the key for the "Collection" schema is (Author,
Title).

2. Determine functional dependencies:

- There is only one functional dependency: title, Author -> Catalog no.

3. Check for BCNF:

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

4. Check for 3NF:

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

- Therefore, the "Collection" schema is not in 3NF.

Final Answer:

Based on the analysis:

- The "Book" schema is in BCNF.

- The "Collection" schema is not in 3NF.

Therefore, the correct answer is:

(c) Book is in 2NF and Collection in 3NF.

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:

Let's design the ER diagram step by step:

Entities and Their Attributes:


1. Student Entity:

- Attributes:

- Student ID (Primary Key)

- First Name

- Last Name

- Date of Birth

- Address

- Relationships:

- Enrolls in Courses (Many-to-Many)

- Belongs to a Department (Many-to-One)

2. Course Entity:

- Attributes:

- Course Code (Primary Key)

- Course Name

- Credits

- Relationships:

- Taken by Students (Many-to-Many)

- Belongs to a Department (Many-to-One)

3. Department Entity:

- Attributes:

- Department Name (Primary Key)

- Relationships:

- Offers Courses (One-to-Many)

- Employs Professors (One-to-Many)

4. Professor Entity:

- Attributes:
- Professor ID (Primary Key)

- First Name

- Last Name

- Email

- Relationships:

- Teaches Courses (One-to-Many)

- Belongs to a Department (Many-to-One)

Relationships:

- Enrolls in Courses (Many-to-Many between Student and Course)

- Taken by Students (Many-to-Many between Course and Student)

- Offers Courses (One-to-Many between Department and Course)

- Employs Professors (One-to-Many between Department and Professor)

- Teaches Courses (One-to-Many between Professor and Course)

- Belongs to a Department (Many-to-One between Student, Course, and Professor)

Cardinality Ratios:

- Enrolls in Courses: Many Students can enroll in Many Courses.

- Taken by Students: Many Courses can be taken by Many Students.

- Offers Courses: One Department can offer Many Courses.

- Employs Professors: One Department can employ Many Professors.

- Teaches Courses: One Professor can teach Many Courses.

- Belongs to a Department: Many Students, Courses, and Professors can belong to One Department.

Primary Keys:

- Student ID for the Student Entity


- Course Code for the Course Entity

- Department Name for the Department Entity

- Professor ID for the Professor Entity

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) {}

std::string getName() const {


return name;
}

std::string getPassportNumber() const {


return 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) {}

std::string getFlightNumber() const {


return flightNumber;
}

bool isAvailable() const {


return passengers.size() < capacity;
}

virtual double calculatePrice() const = 0;

void addPassenger(const Passenger& passenger) {


if (isAvailable()) {
passengers.push_back(passenger);
std::cout << "Reservation successful for " << passenger.getName() << " on flight
" << flightNumber << std::endl;
} else {
std::cout << "Sorry, the flight is full." << std::endl;
}
}

protected:
std::string flightNumber;
std::string origin;
std::string destination;
int capacity;
std::vector<Passenger> passengers;
};

class DomesticFlight : public Flight {


public:
DomesticFlight(std::string flightNumber, std::string origin, std::string destination, int
capacity, double baggageFee)
: Flight(flightNumber, origin, destination, capacity), baggageFee(baggageFee) {}
double calculatePrice() const override {
// Calculate price for a domestic flight
return 200.0 + passengers.size() * 100.0 + passengers.size() * baggageFee;
}

private:
double baggageFee;
};

class InternationalFlight : public Flight {


public:
InternationalFlight(std::string flightNumber, std::string origin, std::string destination, int
capacity, double tax)
: Flight(flightNumber, origin, destination, capacity), tax(tax) {}

double calculatePrice() const override {


// Calculate price for an international flight
return 400.0 + passengers.size() * 150.0 + passengers.size() * tax;
}

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

You might also like