Meta (Facebook) AI-Enabled Interview Question — Triplet Card Game Strategy & Illegal Move Detection

26 Views
No Comments

Triplet Card Game — AI Strategy Task

You can see there is a 4×4 grid of cards, Ace through Nine.
Each turn consists of picking up 3 cards which add up to 15 (Aces count as 1);
these cards are then replaced from the deck.

If you can’t pick up exactly 3 cards that add up to 15, then the game is over.
In a perfect game, you would pick up all 36 cards.

Currently, pressing Return makes the game end immediately.
This is because MyStrategy is a skeleton that does not pick any cards.
You will implement that later.

Now run the unit tests in TripletGameTests. The game works if the Strategy plays fairly,
but the test shows that the game allows players to cheat by choosing a card that isn’t on the table.
Let’s fix that first!

Tasks:

  1. Fix the illegal play issue: prevent choosing a card that is not currently on the table.
  2. Ensure that the game ends immediately after an illegal play.
  3. Implement MyStrategy so that it selects valid moves (pick three cards whose sum = 15).
  4. The score should be calculated correctly based on legal triplets picked.

Unit Tests include:
testLegalPlaysAreAllowed()
testEndGameAfterIllegalPlay()

Card.java

class Card {
    public enum Rank {
        private final int value;
        private final String displayChar;

        Rank(int value, String displayChar) {
            this.value = value;
            this.displayChar = displayChar;
        }

        public int getValue() {return value;}

        public String getDisplayChar() {return displayChar;}
    }

    private final Suit suit;
    private final Rank rank;

    public Card(Suit suit, Rank rank) {
        this.suit = suit;
        this.rank = rank;
    }

    public Suit getSuit() {return suit;}

    public Rank getRank() {return rank;}

    public int getValue() {return rank.getValue();
    }

    public String toString() {return rank.getDisplayChar() + suit.getDisplayChar();}
}

TripletGameTests.java

@Test
public void testLegalPlaysAreAllowed() {Deck testDeck = stackDeckForTable(TABLE_ONE);
    TripletGame.Strategy strategy =
            new StaticStrategy(new Card[][] {{c(ACE, SPADES), c(FIVE, SPADES), c(NINE, SPADES)},
                            {c(ACE, HEARTS), c(FIVE, HEARTS), c(NINE, HEARTS)},
                            {c(ACE, DIAMONDS), c(FIVE, DIAMONDS), c(NINE, DIAMONDS)}
                    });

    TripletGame game = new TripletGame(testDeck, 4, strategy);
    assertFalse(game.playTurn());
    assertFalse(game.playTurn());
    assertTrue(game.playTurn());
    assertEquals(45, game.getScore());
}
@Test
public void testEndGameAfterIllegalPlay() {Deck testDeck = stackDeckForTable(TABLE_ONE);
    TripletGame.Strategy strategy =
            new StaticStrategy(new Card[][] {{c(TWO, SPADES), c(FIVE, SPADES), c(EIGHT, SPADES)},
                    });

    TripletGame game = new TripletGame(testDeck, 4, strategy);
    assertTrue(game.playTurn());
    assertEquals(0, game.getScore());
}

Main.java

public class Main {private static void updateDisplay(TripletGame game) {System.out.print("\u001b[H\u001b[2J");
        System.out.println("Current Game State:" + game.getScore());

        List<List<Card>> table = game.readOnlyTable();
        for (int rowIndex = 0; rowIndex < table.size(); rowIndex++) {List<Card> row = table.get(rowIndex);
            for (int cardIndex = 0; cardIndex < row.size(); cardIndex++) {String cardText = row.get(cardIndex) == null ? ".." : row.get(cardIndex).toString();
                System.out.print("" + cardText +" ");
            }
            System.out.println();}
    }

    public static void main(String[] args) {TripletGame game = new TripletGame(4, new MyStrategy());
        Scanner scanner = new Scanner(System.in);

        updateDisplay(game);
        while (!game.gameOver()) {System.out.println("Press enter to play a turn");
            scanner.nextLine();
            updateDisplay(game);
            game.playTurn();}

        System.out.println("Game Over! Final Score:" + game.getScore());
        scanner.close();}
}

MyStrategy.java

class MyStrategy implements TripletGame.Strategy {
    @Override
    public Card[] selectCards(List<List<Card>> table) {return new Card[0]; // TODO: choose 3 cards that sum to 15
    }
}

TripletGame.java

public boolean playTurn() {
    // TODO validate selected cards
    return false;
}

The table is a 4×4 card grid where each move picks exactly three cards that sum to 15.

The current flaw is that the Strategy can select cards not actually present on the table.

Fix by validating card positions in playTurn() and ending the game if any selected card is illegal.

Implement MyStrategy by scanning all card triples and selecting the first valid combination.

Ensure scoring and replacements work properly and unit tests pass (testLegalPlaysAllowed, testEndGameAfterIllegalPlay).

The VOprep team has long accompanied candidates through various major company OAs and VOs, including Meta, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for these companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.

END
 0