← Questions

Collect coins with tokens that move three steps right

0
GoogleCodingLast asked Dec 2025

A coding interview question asked in Google's HackerRank assessment, last asked Dec 2025. It tests greedy, simulation and strings.

A one-row board is a string of . (empty), C (one coin) and T (one of your tokens). You may move any token any number of times, in any order. A move sends a token exactly 3 cells to the right. It is invalid if the landing cell holds another token or lies off the board. Landing on a coin collects it; the cells jumped over are not visited. Return the most coins you can collect.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • max_coins(board: str) -> int.

Example

max_coins("T..C..C")     ->  2    # the token lands on 3, then 6
max_coins("C..T..C")     ->  1    # the coin at 0 is behind the token
max_coins("TC.CC.C")     ->  2    # coins at 3 and 6; the coins at 1 and 4 have no token in their lane
max_coins("C.TT.CC.C")   ->  3    # token 2 takes 5 and 8, token 3 takes 6
max_coins("CCC")         ->  0

Constraints

  • 1 <= len(board) <= 100.
  • A coin and a token never start on the same cell.

Clarifying questions

  • Can two tokens share a cell, even for a moment?
  • Can moves of different tokens be interleaved?
  • Is a coin under a cell the token jumps over collected?
  • Do coins come back after being collected?

What a strong answer covers

  • A token at i only ever stands on i, i + 3, i + 6, …, so the board splits into three independent lanes by index % 3; sum the lanes.
  • Within a lane, every coin to the right of the lane's leftmost token can be collected: move the rightmost token first, all the way to the end, then the next one, so no token blocks another. Coins left of every token in the lane are out of reach.
  • O(n) time, O(1) space, with a short argument for why blocking never costs a coin.
  • Avoiding over-engineering: no search or DP is needed once the lanes are seen.
  • Tests: no tokens, no coins, several tokens in one lane, coins on both sides of a token.

Follow-ups

  • A move is k cells instead of 3, with k up to 10^9 and boards up to 10^5 cells.
  • Coins have non-negative values; maximise the total value.
  • Tokens may also move 3 cells left. Now every coin in a lane that has a token is reachable.
  • Return one concrete move sequence that reaches the maximum.

Where it's asked

  • Google · HackerRank assessment · Last asked Dec 202590 min · Two problems on HackerRank · Some early-career assessments enable the built-in assistant; others forbid AI. The invitation says which.

What it tests

greedy · simulation · strings

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding