[Snowflake] OA 2025 Start – 26 Feb (Generic)

Coloring Houses

The city of Hackerland can be represented with an even number n houses arranged in a row. A painter must paint the houses using at most three colors. The following conditions must hold true:

  1. No two adjacent houses are the same color.
  2. The houses which are at the same distance from both ends must not be colored with the same color. For example, n = 6 then houses will be {1,2,3,4,5,6}, so the houses at the same distance from both ends will be [1,6], [2,5], [3,4].

The task is to find the number of ways to paint the houses using at most three colors such that both the above conditions hold true. Since the answer can be large, report it modulo 10⁹ + 7. Two ways are considered different if at least one house is colored differently.

Example

For n = 4, some of the possible valid arrangements are:

  • (color1, color2, color3, color2)
  • (color1, color3, color1, color3)

The number of ways to paint 4 houses using three colors is 18. Return 18 modulo (10⁹ + 7) which is 18.

Function Description

Complete the countWaysToColorHouses function in the editor below.

countWaysToColorHouses takes in a single parameter:

  • int n: the number of houses

Return

  • int: the number of ways in which the houses can be colored, calculated as a modulo of (10⁹ + 7)

我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。

We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.

Leave a Reply

Your email address will not be published. Required fields are marked *