← 返回博客列表
Booking

Booking OA Real Questions: Maximal Square DP + Hotel Review Keyword Scoring

2026-03-31

Two Booking OA problems — one classic 2D DP, one string processing + sorting challenge. Clean thinking matters but so do the details. Full walkthrough below.

Booking OA Q1


Exam Overview

Item Details
Platform HackerRank
Questions 2
Difficulty Medium / Medium
Role SDE
Core Topics Dynamic Programming, HashMap, HashSet, Custom Sorting

Q1: Maximal Square

Booking OA Q1

Core Idea

Given a 2D matrix of '0's and '1's, find the largest square containing only 1s and return its area.

Solution Approach

Brute Force (O(mn·min(m,n)²) — TLE)

Enumerate each cell as the top-left corner of a square, try expanding the side length, and check whether the region is all 1s. Too slow for large matrices.

Optimal: 2D Dynamic Programming O(mn)

Definition: dp[i][j] = the side length of the largest all-1 square whose bottom-right corner is at (i, j).

Transition:

$$dp[i][j] = \min(dp[i-1][j],\ dp[i][j-1],\ dp[i-1][j-1]) + 1$$

Intuition: The largest square ending at (i, j) is constrained by the smallest of its three neighbors (top, left, top-left) — the "weakest link" determines the square's size.

Answer: Traverse the entire dp table, record the maximum side length max_side, return max_side * max_side.

Solution Template

def maximalSquare(matrix):
    if not matrix:
        return 0

    m, n = len(matrix), len(matrix[0])
    dp = [[0] * n for _ in range(m)]
    max_side = 0

    for i in range(m):
        for j in range(n):
            if matrix[i][j] == '1':
                if i == 0 or j == 0:
                    dp[i][j] = 1
                else:
                    dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1
                max_side = max(max_side, dp[i][j])

    return max_side * max_side

Complexity

Key Insight

dp[i][j] only depends on three neighbors. Taking the minimum reflects that a square's side length is determined by its shortest "edge" — a classic matrix DP state definition.


Q2: Hotel Review Keyword Scoring

![Booking OA Q2](/images/booking/image copy.png)

Core Idea

Given two sets of keywords (positive and negative) and a list of hotel reviews (each tagged with a HotelID), compute a total score per hotel based on keyword hits, then return the top-k hotel IDs sorted by score descending, with ties broken by ID ascending.

Solution Approach

Overall flow:

  1. Load positive and negative keywords into two HashSets for O(1) lookup
  2. For each review:
    • Strip punctuation with regex, convert to lowercase
    • Split into words
    • For each word: +1 if in positive set, −1 if in negative set
    • Accumulate into HashMap<HotelID, TotalScore>
  3. Convert the map to a list, sort with a custom comparator: score descending, then ID ascending
  4. Return the first k hotel IDs

Solution Template

import re
from collections import defaultdict

def topKHotels(positive_words, negative_words, reviews, k):
    pos_set = set(positive_words)
    neg_set = set(negative_words)

    score_map = defaultdict(int)  # HotelID -> TotalScore

    for hotel_id, review_text in reviews:
        # Strip punctuation, lowercase, split
        words = re.sub(r'[^\w\s]', '', review_text.lower()).split()
        for word in words:
            if word in pos_set:
                score_map[hotel_id] += 1
            elif word in neg_set:
                score_map[hotel_id] -= 1

    # Custom sort: score descending, ID ascending on tie
    sorted_hotels = sorted(score_map.keys(),
                           key=lambda h: (-score_map[h], h))

    return sorted_hotels[:k]

Complexity

Key Insight

HashSet gives O(1) keyword lookup; HashMap accumulates scores in one pass. The custom comparator handles both sort dimensions (score desc, ID asc) cleanly in a single sorted() call.


Side-by-Side Comparison

Question Core Technique Time Space
Q1 Maximal Square 2D DP, min of 3 neighbors + 1 O(mn) O(mn)
Q2 Hotel Scoring HashSet + HashMap + custom sort O(W log W) O(H + K)

Common Mistakes

Question Mistake Correct Approach
Q1 Enumerate all squares, O(mn²) TLE Use DP recurrence, single O(mn) pass
Q1 Use sum instead of min in transition Take min — square is limited by shortest edge
Q2 Linear scan through keyword list Use HashSet for O(1) lookup
Q2 Ignore tie-breaking rule on equal scores Custom comparator: score desc then ID asc

Booking OA Strategy

On-site Pacing

  1. Read both problems first — allocate time by relative difficulty
  2. Q1 type (matrix DP): Think "bottom-right corner" as the anchor — transition almost always involves min/max of three neighbors
  3. Q2 type (string counting): Nail the data structures first (HashSet lookup + HashMap accumulate), then handle sorting
  4. Run through examples; verify edge cases (empty matrix, k larger than hotel count)

Related LeetCode Practice

# Problem Covers
221 Maximal Square Q1 identical
1277 Count Square Submatrices with All Ones Q1 variant
85 Maximal Rectangle Q1 advanced
692 Top K Frequent Words Q2 sorting pattern
1 Two Sum Q2 HashMap fundamentals

🚀 Need Booking OA Assistance?

oavoservice provides stable, long-term real-time OA/VO support for Google, Amazon, TikTok, Booking, Uber, Bloomberg, Microsoft and more — both domestic and overseas interviews covered. High question-bank coverage.

Add WeChat: Coding0201

What you get:

📱 WeChat: Coding0201 | 💬 Telegram: @OAVOProxy | 📧 [email protected]


Contact

Email: [email protected]
Telegram: @OAVOProxy