Magicsheet logo

Alice and Bob Playing Flower Game

Medium
62.6%
Updated 6/1/2025

Asked by 4 Companies

Topics

Alice and Bob Playing Flower Game

What is this problem about?

The "Alice and Bob Playing Flower Game interview question" is a mathematical game-theory problem that focuses on parity and combinations. In this game, Alice and Bob choose numbers from a given range (let's say 1 to n and 1 to m). The winner is determined by the sum of their chosen numbers being odd or even. Specifically, if the sum of the two chosen numbers is odd, Alice wins; otherwise, Bob wins. The challenge is to calculate how many pairs of numbers (x, y) exist such that Alice wins, given the constraints on x and y.

Why is this asked in interviews?

Firms like Microsoft and Rubrik often use the "Alice and Bob Playing Flower Game coding problem" to test a candidate's ability to simplify a problem using logic rather than brute force. While a double loop would solve this for small constraints, the "Math interview pattern" is required for large values of n and m. It checks if the candidate can recognize that the sum of two numbers is odd if and only if one number is even and the other is odd.

Algorithmic pattern used

This problem follows the Combinatorics and Parity pattern. To get an odd sum, you have two distinct cases:

  1. x is even and y is odd.
  2. x is odd and y is even. By counting how many odd and even numbers exist in the ranges [1, n] and [1, m], you can use basic multiplication to find the total number of winning combinations for Alice.

Example explanation

Suppose n = 3 and m = 2.

  • Range [1, n] contains: {1, 2, 3}. (Odd: 2, Even: 1)
  • Range [1, m] contains: {1, 2}. (Odd: 1, Even: 1) Alice wins if x + y is odd:
  • Case 1 (x even, y odd): 1 * 1 = 1 pair (2, 1).
  • Case 2 (x odd, y even): 2 * 1 = 2 pairs (1, 2) and (3, 2). Total winning pairs for Alice = 1 + 2 = 3.

Common mistakes candidates make

  • Brute Force: Trying to use nested loops to iterate through all possible pairs, which leads to a Time Limit Exceeded (TLE) error for large inputs.
  • Integer Overflow: Forgetting that the product of two large numbers (up to 10910^9) can exceed the capacity of a standard 32-bit integer.
  • Incorrect Parity Counting: Miscounting the number of odd or even integers in a range. Remember that for a range [1, k], there are floor(k/2) even numbers and ceil(k/2) odd numbers.

Interview preparation tip

Always look for a mathematical shortcut when a problem involves large numeric ranges. If a problem depends on whether a sum is even or odd, immediately think about the properties of parity. Practice identifying when a problem can be solved in O(1)O(1) or O(logN)O(log N) time rather than O(N2)O(N^2).

Similar Questions