Mock interview 2: a medium problem and OOP
60 min · Advanced
Need help? Ask a tutor on WhatsApp (opens in a new tab)A second round goes deeper: one medium problem and OOP design questions. This mock gives the script, the model answers and the follow-ups interviewers like to ask.
How a second round runs
A second round is a deeper technical interview where one medium problem and a design or OOP discussion test how you think, not only what you remember.
A second round is about 45 minutes: a medium problem for 25 minutes, then object-oriented questions for 15, then your questions. The interviewer pushes with follow-ups such as "can it be faster?". Example 1 shows the minutes.
Example 1 · The 45-minute script
00-03 WARM-UP one line on your last project03-28 PROBLEM medium: idea, code, tests, then "can it be faster?"28-43 OOP 3 questions: inheritance, properties, class members43-45 YOUR Q one question about the team's code review3 + 25 + 15 + 2 = 45 minutes
What this code does
- The problem takes more than half the time; do not rush the idea.
- OOP questions are short, so a clear definition plus a mini example is enough.
You are judged on four things: the idea, the code, the tests and how clearly you explain your choices.
The medium problem: the sliding window
A sliding window is a range of positions in a list that grows on the right and shrinks on the left, so one pass finds the best range.
The question: given product codes of orders in a day, find the longest stretch with no code repeated. Say the slow idea, then the window idea. Examples 2 and 3 show both ideas and the tests.
Example 2 · Brute force and the window
def longest_slow(codes): best = 0 for start in range(len(codes)): seen = set() for code in codes[start:]: if code in seen: break seen.add(code) best = max(best, len(seen)) return best
def longest(codes): last_seen = {} start = best = 0 for i, code in enumerate(codes): if code in last_seen and last_seen[code] >= start: start = last_seen[code] + 1 last_seen[code] = i best = max(best, i - start + 1) return best
data = ["a", "b", "c", "a", "d", "b"]print(longest_slow(data), longest(data))4 4
What this code does
- The slow version starts at every position and extends until a repeat: O(n squared).
- The window moves
startpast the earlier copy of a repeated code, so no position is visited twice: O(n) time, O(k) space for k different codes. - The check
>= startignores a code that was seen before the window began.
Example 3 · Tests to say aloud
def longest(codes): last_seen = {} start = best = 0 for i, code in enumerate(codes): if code in last_seen and last_seen[code] >= start: start = last_seen[code] + 1 last_seen[code] = i best = max(best, i - start + 1) return best
print(longest([]))print(longest(["x"]))print(longest(["x", "x", "x"]))print(longest(["a", "b", "b", "a"]))0 1 1 2
What this code does
- Empty list gives 0, one item gives 1, all equal gives 1.
- The last test checks that an old "a" does not move the start backwards.
The window idea keeps the start of the stretch and a dictionary of where each code was last seen. Each item is visited once: O(n) time.
OOP questions: inheritance, properties and class members
Expect questions on super(), overriding, @property, __repr__ and __eq__, and class versus instance data. Answer with a definition and a tiny class. Examples 4 to 6 give the script.
Example 4 · OOP Q1: inheritance, super and polymorphism
class Account: def __init__(self, owner, balance): self.owner = owner self.balance = balance
def monthly_fee(self): return 10
class Savings(Account): def __init__(self, owner, balance, rate): super().__init__(owner, balance) self.rate = rate
def monthly_fee(self): return 0
for acc in [Account("Asha", 500), Savings("Ravi", 900, 0.04)]: print(acc.owner, acc.monthly_fee())Asha 10 Ravi 0
What this code does
super().__init__reuses the parent setup, so the child adds only its own data.- The loop calls the same method on both objects and each class answers in its own way: polymorphism.
- Model answer: "A subclass reuses the parent and changes one rule, and the caller does not need to know which class it has."
Example 5 · OOP Q2: property and repr
class Product: def __init__(self, name, price): self.name = name self.price = price
@property def price(self): return self._price
@price.setter def price(self, value): if value < 0: raise ValueError("price cannot be negative") self._price = value
def __repr__(self): return f"Product({self.name!r}, {self.price})"
item = Product("Desk", 120)print(item)try: item.price = -5except ValueError as error: print(error)Product('Desk', 120)
price cannot be negativeWhat this code does
- The property keeps the check in one place, while callers still write
item.price. __repr__makes the object readable in logs and when printed.- Model answer: "A property lets me add a rule later without changing the code that uses the attribute."
Example 6 · OOP Q3: class data and class methods
class Order: count = 0 tax_rate = 0.1
def __init__(self, total): self.total = total Order.count += 1
@classmethod def from_text(cls, text): return cls(float(text))
@staticmethod def is_valid(total): return total > 0
a = Order(100)b = Order.from_text("50")a.tax_rate = 0.2print(Order.count, Order.tax_rate, a.tax_rate, Order.is_valid(b.total))2 0.1 0.2 True
What this code does
countbelongs to the class. Settinga.tax_ratemade a new instance attribute and left the class value alone.- A class method receives the class and builds objects; a static method is a plain function kept inside the class.
Always say why. "Subclasses can change one rule without changing the callers" beats "it is inheritance".
Strong and weak answers
Example 7 sets a weak and a strong reply side by side. The strong one names the design choice, the cost and a test.
Example 7 · A strong and a weak reply
Question: "When would you use inheritance, and when composition?"Weak: "Inheritance is when a class gets another class's things. Composition is when you use classes."Strong: "Inheritance when the child really is a kind of the parent, like Savings is an Account. Composition when one object has another, like an Order has Items. I prefer composition when unsure, because it is easier to change later."Weak: no rule, no example. Strong: rule + two examples + a reason.
What this code does
- The weak reply repeats words without a rule.
- The strong reply gives the is-a and has-a test and a default choice.
Score yourself with the rubric: idea 4, code 4, tests 4, explanation 4, OOP 4. Below 12 means repeat the lesson.
Next step
Check your understanding
10 quick questions