Meta questions

Sum Fifteen

MetaOnsiteHard

A card game uses a deck of cards whose values are small integers. Implement play(deck, table_size).

The first table_size cards of deck are dealt face up in a row, in slot order (or the whole deck, if it is shorter). On each turn the player removes three cards from the table whose values sum to exactly 15, and then up to three cards are taken from the front of the unused deck and appended to the end of the table, in deck order; if fewer than three remain, all of them are taken. The game ends when no three cards on the table sum to 15.

play returns the number of triples removed.

Part 1: First available triple

Each turn the player removes the triple of table slots (i, j, k) with i < j < k that comes first in lexicographic order among all triples of slots whose values sum to 15. The three cards are removed, the remaining cards keep their order, and the next cards are drawn as usual. Return the number of triples removed.

  1. Example 1
    deck
    [5,5,5,1,9,5,4,6]
    table_size
    4
    Output
    2

    Why: The table starts as [5, 5, 5, 1]. The first triple of slots is the three 5s, so they go, [1] stays, and the next three cards 9, 5, 4 are added to give [1, 9, 5, 4]. Slots 0, 1, 2 sum to 15 again, so 1, 9, 5 go and [4] stays. Only the card 6 is left to draw, giving [4, 6], which has no triple. That is 2 triples.

Constraints

  • 1 <= len(deck) <= 100
  • 1 <= deck[i] <= 25
  • 3 <= table_size <= 16
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Sum Fifteen: Meta interview question | LoopCoach