Bird
Raised Fist0
LLDsystem_design~20 mins

Board and piece hierarchy in LLD - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Board and Piece Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding inheritance in board and piece hierarchy

In a board game design, which class should be the base class to represent all pieces on the board?

AA <strong>Game</strong> class that manages the rules and flow.
BA <strong>Player</strong> class that owns pieces.
CA <strong>Board</strong> class that contains all pieces and their positions.
DA generic <strong>Piece</strong> class that all specific pieces inherit from.
Attempts:
2 left
💡 Hint

Think about what all pieces share regardless of type.

Architecture
intermediate
1:30remaining
Designing the board class for scalability

Which design choice best supports a scalable board that can handle different game types and sizes?

AUse a fixed-size 2D array inside the <strong>Board</strong> class for all games.
BImplement the <strong>Board</strong> class with a dynamic data structure like a map or dictionary keyed by coordinates.
CStore pieces directly inside the <strong>Piece</strong> class without a board container.
DUse global variables to track piece positions outside any class.
Attempts:
2 left
💡 Hint

Consider flexibility for different board sizes and shapes.

scaling
advanced
2:00remaining
Handling piece movement validation efficiently

For a complex game with many piece types and movement rules, which approach best scales validation logic?

ADefine movement rules inside each <strong>Piece</strong> subclass using polymorphism.
BHardcode movement rules in the UI layer.
CUse a global function that checks moves for all pieces without class methods.
DPut all movement rules in a single large method inside the <strong>Board</strong> class.
Attempts:
2 left
💡 Hint

Think about how to keep code organized and easy to extend.

tradeoff
advanced
2:00remaining
Choosing between composition and inheritance for piece abilities

Which design choice balances flexibility and code reuse when pieces have multiple abilities?

AUse composition by creating separate ability classes and attaching them to pieces.
BUse deep inheritance chains where each subclass adds one ability.
CDuplicate ability code in each piece subclass that needs it.
DImplement all abilities in the base <strong>Piece</strong> class with flags.
Attempts:
2 left
💡 Hint

Consider how to add or remove abilities without rewriting classes.

estimation
expert
3:00remaining
Estimating memory usage for a large chess-like game

Estimate the approximate memory needed to store the state of a 100x100 board with 10,000 pieces, each piece storing position (2 integers), type (1 byte), and owner (1 byte). Assume 4 bytes per integer and ignore overhead.

AAbout 600 KB
BAbout 6 MB
CAbout 100 KB
DAbout 600 MB
Attempts:
2 left
💡 Hint

Calculate bytes per piece and multiply by number of pieces.

Practice

(1/5)
1. What is the main purpose of having a base Piece class in a board game design?
easy
A. To manage network communication between players
B. To define common properties like position and type for all pieces
C. To handle user input events
D. To store the entire board layout

Solution

  1. Step 1: Understand the role of a base class

    A base class provides shared properties and methods for all derived classes, avoiding repetition.
  2. Step 2: Apply to board game pieces

    All pieces share common traits like position and type, so the base Piece class holds these.
  3. Final Answer:

    To define common properties like position and type for all pieces -> Option B
  4. Quick Check:

    Base class = common properties [OK]
Hint: Base class holds shared traits for all pieces [OK]
Common Mistakes:
  • Confusing board layout storage with piece properties
  • Thinking base class handles user input
  • Assuming base class manages network tasks
2. Which of the following is the correct way to declare a subclass King that extends a base Piece class in a typical object-oriented design?
easy
A. class King extends Piece { constructor(position) { super(position); } }
B. function King() { this.position = position; } extends Piece
C. class King inherits Piece { constructor() { } }
D. King = Piece + position

Solution

  1. Step 1: Identify correct subclass syntax

    In modern OOP, a subclass uses extends keyword and calls super() in constructor.
  2. Step 2: Check each option

    class King extends Piece { constructor(position) { super(position); } } uses correct syntax: class King extends Piece { constructor(position) { super(position); } }.
  3. Final Answer:

    class King extends Piece { constructor(position) { super(position); } } -> Option A
  4. Quick Check:

    Subclass syntax = extends + super() [OK]
Hint: Subclass uses extends and calls super() in constructor [OK]
Common Mistakes:
  • Using incorrect keywords like inherits
  • Placing extends after function declaration
  • Trying to add properties with '+' operator
3. Given this code snippet for a board and pieces, what will be the output of console.log(board.pieces[0].type);?
class Piece {
  constructor(type, position) {
    this.type = type;
    this.position = position;
  }
}
class Board {
  constructor() {
    this.pieces = [];
  }
  addPiece(piece) {
    this.pieces.push(piece);
  }
}
const board = new Board();
board.addPiece(new Piece('Knight', 'B1'));
medium
A. undefined
B. "B1"
C. Error: pieces is not defined
D. "Knight"

Solution

  1. Step 1: Understand object creation and storage

    A new Piece with type 'Knight' and position 'B1' is created and added to board.pieces.
  2. Step 2: Access the first piece's type

    board.pieces[0] refers to the first piece, so board.pieces[0].type is 'Knight'.
  3. Final Answer:

    "Knight" -> Option D
  4. Quick Check:

    First piece type = 'Knight' [OK]
Hint: First piece type is stored in pieces[0].type [OK]
Common Mistakes:
  • Confusing position with type
  • Assuming pieces array is empty
  • Expecting an error due to missing pieces
4. Identify the error in this piece hierarchy code snippet:
class Piece {
  constructor(type, position) {
    this.type = type;
    this.position = position;
  }
}
class Queen extends Piece {
  constructor(position) {
    this.type = 'Queen';
    this.position = position;
  }
}
medium
A. Position should not be passed to constructor
B. Queen class should not have a constructor
C. Missing call to super() in Queen constructor
D. Type should be passed as parameter to Queen constructor

Solution

  1. Step 1: Review subclass constructor rules

    In subclasses, the constructor must call super() before using this.
  2. Step 2: Check Queen constructor

    Queen constructor assigns this.type and this.position without calling super(), causing an error.
  3. Final Answer:

    Missing call to super() in Queen constructor -> Option C
  4. Quick Check:

    Subclass constructor must call super() first [OK]
Hint: Always call super() before using this in subclass constructor [OK]
Common Mistakes:
  • Forgetting super() call in subclass constructor
  • Trying to assign this before super()
  • Assuming constructor is optional in subclass
5. You want to design a scalable board game system where each piece type has unique movement rules. Which design approach best supports adding new piece types without changing existing code?
hard
A. Use a base Piece class and create subclasses for each piece type implementing their own move logic
B. Store all piece types and moves in a single large switch-case statement
C. Keep piece types as strings and handle moves in a separate global function with if-else
D. Use a flat list of pieces with no hierarchy and hardcode moves in the board class

Solution

  1. Step 1: Understand scalability and extensibility

    Good design allows adding new piece types without modifying existing code, following open-closed principle.
  2. Step 2: Evaluate design options

    Subclassing Piece lets each piece implement its own move logic, enabling easy extension.
  3. Final Answer:

    Use a base Piece class and create subclasses for each piece type implementing their own move logic -> Option A
  4. Quick Check:

    Subclassing = scalable and extensible design [OK]
Hint: Subclass each piece type for unique moves [OK]
Common Mistakes:
  • Using large switch-case blocks that are hard to maintain
  • Handling moves globally with if-else reduces flexibility
  • Hardcoding moves in board class limits scalability