Interview Experience: Python + SQL Round
I recently had an interview after clearing an aptitude test (40 questions in 15 minutes).
I got a call from HR for the first technical round (Python + SQL) - 30 mins.
🔹 Python Question
Movie Booking System
- A theater has 200 seats.
- Rules:
- Single booking → assign a random seat from available ones.
- Group booking → pick a random starting seat, then allocate following seats in serial order.
- Example: If user books 3 seats, and seat 67 is randomly picked, then next seats could be 69, 71 (skipping already booked ones like 68 and 70).
I had to come up with an algorithm in Python for this.
✅ Python Solution
import random
class MovieTheater:
def __init__(self, total_seats=200):
# 0 = available, 1 = booked
self.seats = [0] * total_seats
self.total_seats = total_seats
def get_random_available_seat(self):
"""Pick a random available seat, or return -1 if full."""
available = [i for i, s in enumerate(self.seats) if s == 0]
if not available:
return -1
return random.choice(available)
def book_tickets(self, tickets=1):
"""Book tickets following the rules."""
if tickets <= 0:
return []
booked = []
# Case 1: Single ticket
if tickets == 1:
seat = self.get_random_available_seat()
if seat == -1:
return []
self.seats[seat] = 1
return [seat]
# Case 2: Group booking
first_seat = self.get_random_available_seat()
if first_seat == -1:
return []
self.seats[first_seat] = 1
booked.append(first_seat)
# Find next seats in serial order, skipping already booked
current = first_seat + 1
while len(booked) < tickets and current < self.total_seats:
if self.seats[current] == 0:
self.seats[current] = 1
booked.append(current)
current += 1
# If we couldn’t fulfill all requested tickets, rollback
if len(booked) < tickets:
for s in booked:
self.seats[s] = 0
return []
return booked🔹 SQL Question
Find the date when most people watched Movie ID = 123
Table Schema:
Booking(
Booking_id INT,
Booking_date DATETIME,
Show_date DATETIME,
Movie_id INT,
Seat_number INT
);SELECT Show_date, COUNT(DISTINCT Seat_number) AS viewers
FROM Booking
WHERE Movie_id = 123
GROUP BY Show_date
ORDER BY viewers DESC
LIMIT 1;
Comments (1)

