Il 0% ha trovato utile questo documento (0 voti)
1 visualizzazioni134 pagine

LLD All Inclusive

Il documento raccoglie una serie di problemi di design LLD (Low-Level Design) e le relative soluzioni e modelli in TypeScript, coprendo 25 domande. Ogni sezione include un riepilogo del design, i modelli utilizzati e rappresentazioni UML. Sono fornite implementazioni complete per alcune domande, come il gioco Snake & Ladder.

Caricato da

qvk0m30qj2
Copyright
© All Rights Reserved
Per noi i diritti sui contenuti sono una cosa seria. Se sospetti che questo contenuto sia tuo, rivendicalo qui.
Formati disponibili
Scarica in formato PDF, TXT o leggi online su Scribd
Il 0% ha trovato utile questo documento (0 voti)
1 visualizzazioni134 pagine

LLD All Inclusive

Il documento raccoglie una serie di problemi di design LLD (Low-Level Design) e le relative soluzioni e modelli in TypeScript, coprendo 25 domande. Ogni sezione include un riepilogo del design, i modelli utilizzati e rappresentazioni UML. Sono fornite implementazioni complete per alcune domande, come il gioco Snake & Ladder.

Caricato da

qvk0m30qj2
Copyright
© All Rights Reserved
Per noi i diritti sui contenuti sono una cosa seria. Se sospetti che questo contenuto sia tuo, rivendicalo qui.
Formati disponibili
Scarica in formato PDF, TXT o leggi online su Scribd

LLD Problems -> TypeScript Solutions and Patterns (Q1-Q25)

Generated: 2025-09-25 18:17:19

T
h
i
s

d
o
c
u
m
e
n
t

c
o
l
l
e
c
t
s

t
h
e

L
L
D

p
r
o
b
l
e
m
s

(
Q
1
-
Q
2
5
)
,

c
o
m
p
a
c
t

d
e
s
i
g
n

s
u
m
m
a
r
y

f
o
r

e
a
c
h
,

d
e
s
i
g
n

p
a
t
t
e
r
n
s

m
a
p
p
i
n
g
,

U
M
L

(
t
e
x
t

f
o
r
m
)
,

a
n
d
e
x
a
m
p
l
e

T
y
p
e
S
c
r
i
p
t

i
m
p
l
e
m
e
n
t
a
t
i
o
n
s
.

F
u
l
l

i
m
p
l
e
m
e
n
t
a
t
i
o
n
s

a
r
e

p
r
o
v
i
d
e
d

w
h
e
r
e

i
n
d
i
c
a
t
e
d

(
S
n
a
k
e

&

L
a
d
d
e
r

i
n
c
l
u
d
e
d

i
n

f
u
l
l
)
.

I
f

y
o
u

w
a
n
t

i
n
d
i
v
i
d
u
a
l

T
S

f
i
l
e
s

f
o
r

e
a
c
h

q
u
e
s
t
i
o
n
,

a
s
k

a
n
d
I
'
l
l

e
x
p
o
r
t

t
h
e
m
.

Table of Contents (Q1-Q25)


Q1: Parking Lot
Q2: Stack Overflow
Q3: Vending Machine
Q4: Logging Framework
Q5: Traffic Signal Control System
Q6: Coffee Vending Machine
Q7: Task Management System
Q8: ATM
Q9: LinkedIn
Q10: LRU Cache
Q11: Tic Tac Toe
Q12: Pub Sub System
Q13: Elevator System
Q14: Car Rental System
Q15: Online Auction System
Q16: CricInfo
Q17: Splitwise
Q18: Chess Game
Q19: Snake & Ladder
Q20: Ride-Sharing Service
Q21: Course Registration System
Q22: Movie Ticket Booking System
Q23: Online Stock Brokerage System
Q24: Music Streaming Service
Q25: Online Food Delivery Service
Q1: Parking Lot
D
e
s
i
g
n

p
a
r
k
i
n
g

l
o
t

w
i
t
h

m
u
l
t
i
p
l
e

s
l
o
t

t
y
p
e
s
,

e
n
t
r
y
/
e
x
i
t
,

t
i
c
k
e
t
i
n
g
,

a
n
d

p
r
i
c
i
n
g
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:

S
i
n
g
l
e
t
o
n
,

F
a
c
t
o
r
y
,

S
t
r
a
t
e
g
y
,

O
b
s
e
r
v
e
r
,

S
t
a
t
e

U
M
L

(
T
e
x
t
)
:

P
a
r
k
i
n
g
S
e
r
v
i
c
e
-
>

P
a
r
k
i
n
g
L
o
t

-
>

S
l
o
t

-
>

V
e
h
i
c
l
e

Q2: Stack Overflow


D
e
s
i
g
n

Q
&
A
s
y
s
t
e
m

f
o
r

q
u
e
s
t
i
o
n
s
,

a
n
s
w
e
r
s
,

c
o
m
m
e
n
t
s
,

v
o
t
e
s
,

a
n
d

n
o
t
i
f
i
c
a
t
i
o
n
s
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:
O
b
s
e
r
v
e
r
,

F
a
c
t
o
r
y
,

S
t
a
t
e
,

C
o
m
p
o
s
i
t
e

U
M
L

(
T
e
x
t
)
:

U
s
e
r

-
>

Q
u
e
s
t
i
o
n

-
>

A
n
s
w
e
r

-
>

C
o
m
m
e
n
t

Q3: Vending Machine


D
e
s
i
g
n

v
e
n
d
i
n
g

m
a
c
h
i
n
e

s
u
p
p
o
r
t
i
n
g

i
t
e
m
s
,

p
a
y
m
e
n
t
s
,

a
n
d

s
t
a
t
e

t
r
a
n
s
i
t
i
o
n
s
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:

S
t
a
t
e
,

F
a
c
t
o
r
y
,

S
t
r
a
t
e
g
y
,

C
o
m
m
a
n
d

U
M
L

(
T
e
x
t
)
:

V
e
n
d
i
n
g
M
a
c
h
i
n
e

-
>

I
n
v
e
n
t
o
r
y

-
>

P
a
y
m
e
n
t

-
>

D
i
s
p
e
n
s
e
r

Q4: Logging Framework


D
e
s
i
g
n

l
o
g
g
i
n
g

w
i
t
h

a
p
p
e
n
d
e
r
s
,
f
i
l
t
e
r
s
,

a
n
d

f
o
r
m
a
t
t
e
r
s
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:

S
i
n
g
l
e
t
o
n
,

S
t
r
a
t
e
g
y
,

O
b
s
e
r
v
e
r
,

C
h
a
i
n

o
f

R
e
s
p
o
n
s
i
b
i
l
i
t
y

U
M
L

(
T
e
x
t
)
:

L
o
g
g
e
r

-
>

A
p
p
e
n
d
e
r

-
>
F
o
r
m
a
t
t
e
r

Q5: Traffic Signal Control System


D
e
s
i
g
n

t
r
a
f
f
i
c

s
i
g
n
a
l
s

f
o
r

i
n
t
e
r
s
e
c
t
i
o
n
s

w
i
t
h

t
i
m
i
n
g

s
t
r
a
t
e
g
i
e
s

a
n
d

c
o
o
r
d
i
n
a
t
i
o
n
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:

S
t
a
t
e
,

S
t
r
a
t
e
g
y
,

M
e
d
i
a
t
o
r

U
M
L

(
T
e
x
t
)
:

C
o
n
t
r
o
l
l
e
r

-
>

I
n
t
e
r
s
e
c
t
i
o
n

-
>

S
i
g
n
a
l

Q6: Coffee Vending Machine


D
e
s
i
g
n

c
o
f
f
e
e

m
a
c
h
i
n
e

s
u
p
p
o
r
t
i
n
g

m
u
l
t
i
p
l
e

d
r
i
n
k

t
y
p
e
s

a
n
d

c
u
s
t
o
m
i
z
a
t
i
o
n
s
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:

S
t
a
t
e
,

F
a
c
t
o
r
y
,

B
u
i
l
d
e
r
,
D
e
c
o
r
a
t
o
r

U
M
L

(
T
e
x
t
)
:

C
o
f
f
e
e
M
a
c
h
i
n
e

-
>

B
e
v
e
r
a
g
e

-
>

I
n
g
r
e
d
i
e
n
t

Q7: Task Management System


D
e
s
i
g
n

t
a
s
k

m
a
n
a
g
e
m
e
n
t

t
o
o
l

w
i
t
h

t
a
s
k
s
,

s
u
b
t
a
s
k
s
,

a
s
s
i
g
n
m
e
n
t
s
,

a
n
d

n
o
t
i
f
i
c
a
t
i
o
n
s
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:

O
b
s
e
r
v
e
r
,
S
t
a
t
e
,

C
o
m
p
o
s
i
t
e
,

T
e
m
p
l
a
t
e

M
e
t
h
o
d

U
M
L

(
T
e
x
t
)
:
U
s
e
r

-
>

T
a
s
k

-
>

S
u
b
t
a
s
k

-
>

C
o
m
m
e
n
t

Q8: ATM
D
e
s
i
g
n
A
T
M

w
i
t
h

c
a
r
d

a
u
t
h
,

t
r
a
n
s
a
c
t
i
o
n

t
y
p
e
s
,

c
a
s
h

d
i
s
p
e
n
s
e
r
,

a
n
d

r
e
c
e
i
p
t
s
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:
C
o
m
m
a
n
d
,

S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
i
s
h
-
s
u
b
s
c
r
i
b
e

s
y
s
t
e
m

w
i
t
h

t
o
p
i
c
s
,

p
u
b
l
i
s
h
e
r
s
,

s
u
b
s
c
r
i
b
e
r
s
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:

O
b
s
e
r
v
e
r
,

S
i
n
g
l
e
t
o
n
,

M
e
d
i
a
t
o
r

U
M
L

(
T
e
x
t
)
:

B
r
o
k
e
r
-
>

T
o
p
i
c

-
>

S
u
b
s
c
r
i
b
e
r

-
>

P
u
b
l
i
s
h
e
r

Q13: Elevator System


D
e
s
i
g
n
e
l
e
v
a
t
o
r

c
o
n
t
r
o
l

w
i
t
h

s
c
h
e
d
u
l
i
n
g
,

m
u
l
t
i
p
l
e

e
l
e
v
a
t
o
r
s
,

a
n
d

r
e
q
u
e
s
t
s
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:
S
t
a
t
e
,

S
t
r
a
t
e
g
y
,

M
e
d
i
a
t
o
r
,

C
o
m
m
a
n
d

U
M
L

(
T
e
x
t
)
:

C
o
n
t
r
o
l
l
e
r

-
>

E
l
e
v
a
t
o
r

-
>

F
l
o
o
r

-
>

R
e
q
u
e
s
t
Q14: Car Rental System
D
e
s
i
g
n

c
a
r

r
e
n
t
a
l

w
i
t
h

i
n
v
e
n
t
o
r
y
,

b
o
o
k
i
n
g
s
,
p
r
i
c
i
n
g
,

a
n
d

r
e
t
u
r
n
s
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:
F
a
c
t
o
r
y
,

S
t
r
a
t
e
g
y
,

S
i
n
g
l
e
t
o
n
,

D
e
c
o
r
a
t
o
r

U
M
L

(
T
e
x
t
)
:

R
e
n
t
a
l
S
e
r
v
i
c
e

-
>

V
e
h
i
c
l
e

-
>

B
o
o
k
i
n
g

Q15: Online Auction System


D
e
s
i
g
n

a
u
c
t
i
o
n

p
l
a
t
f
o
r
m

w
i
t
h

l
i
s
t
i
n
g
s
,

b
i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
,

C
o
m
m
a
n
d
,

M
e
m
e
n
t
o

U
M
L

(
T
e
x
t
)
:

G
a
m
e

-
>

B
o
a
r
d

-
>

P
i
e
c
e
-
>

M
o
v
e
H
i
s
t
o
r
y

Q19: Snake & Ladder


D
e
s
i
g
n

S
n
a
k
e

&

L
a
d
d
e
r

b
o
a
r
d
g
a
m
e
:

p
l
a
y
e
r
s
,

d
i
c
e
,

s
n
a
k
e
s
,

l
a
d
d
e
r
s
,

t
u
r
n
s
.
D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:

S
t
r
a
t
e
g
y

(
D
i
c
e
)
,

F
a
c
t
o
r
y

(
B
o
a
r
d
)
,

O
b
s
e
r
v
e
r

(
o
p
t
i
o
n
a
l
)
,

S
t
a
t
e

(
o
p
t
i
o
n
a
l
)

U
M
L

(
T
e
x
t
)
:

G
a
m
e

-
>

B
o
a
r
d

-
>

P
l
a
y
e
r

-
>

D
i
c
e

Q20: Ride-Sharing Service


D
e
s
i
g
n

r
i
d
e
-
s
h
a
r
i
n
g

p
l
a
t
f
o
r
m

w
i
t
h

m
a
t
c
h
i
n
g
,

p
r
i
c
i
n
g
,

d
i
s
p
a
t
c
h
,

p
a
y
m
e
n
t
s
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:

S
i
n
g
l
e
t
o
n
,

S
t
r
a
t
e
g
y
,

F
a
c
t
o
r
y
,

O
b
s
e
r
v
e
r

U
M
L

(
T
e
x
t
)
:

R
i
d
e
S
e
r
v
i
c
e

-
>

D
i
s
p
a
t
c
h
e
r

-
>
D
r
i
v
e
r

-
>

R
i
d
e
r

Q21: Course Registration System


D
e
s
i
g
n

c
o
u
r
s
e

r
e
g
i
s
t
r
a
t
i
o
n
w
i
t
h

p
r
e
r
e
q
u
i
s
i
t
e
s
,

c
a
p
a
c
i
t
y
,

s
c
h
e
d
u
l
e

c
h
e
c
k
s
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:

S
i
n
g
l
e
t
o
n
,

F
a
c
t
o
r
y
,

S
t
r
a
t
e
g
y
,

O
b
s
e
r
v
e
r

U
M
L

(
T
e
x
t
)
:

R
e
g
i
s
t
r
a
t
i
o
n
M
a
n
a
g
e
r

-
>

C
o
u
r
s
e
C
a
t
a
l
o
g

-
>

S
t
u
d
e
n
t

Q22: Movie Ticket Booking


D
e
s
i
g
n

m
o
v
i
e

b
o
o
k
i
n
g

s
y
s
t
e
m

w
i
t
h

s
e
a
t

l
o
c
k
i
n
g
,

p
a
y
m
e
n
t
,
a
n
d

c
o
n
f
i
r
m
a
t
i
o
n
s
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:

S
i
n
g
l
e
t
o
n
,

F
a
c
t
o
r
y
,

S
t
r
a
t
e
g
y
,

S
t
a
t
e
,

O
b
s
e
r
v
e
r
,

S
A
G
A

U
M
L

(
T
e
x
t
)
:

B
o
o
k
i
n
g
M
a
n
a
g
e
r

-
>

S
h
o
w

-
>

S
e
a
t

-
>

P
a
y
m
e
n
t

Q23: Online Stock Brokerage


D
e
s
i
g
n

t
r
a
d
i
n
g

s
y
s
t
e
m
:

o
r
d
e
r

b
o
o
k
,

m
a
t
c
h
i
n
g

e
n
g
i
n
e
,

t
r
a
d
e
s
,

p
o
r
t
f
o
l
i
o
s
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:

S
t
r
a
t
e
g
y
,

F
a
c
t
o
r
y
,

O
b
s
e
r
v
e
r
,

C
o
m
m
a
n
d
,

S
t
a
t
e

U
M
L

(
T
e
x
t
)
:

E
x
c
h
a
n
g
e

-
>

O
r
d
e
r
B
o
o
k

-
>

O
r
d
e
r

-
>

T
r
a
d
e

Q24: Music Streaming Service


D
e
s
i
g
n

s
t
r
e
a
m
i
n
g

p
l
a
t
f
o
r
m

w
i
t
h

t
r
a
c
k
s
,

p
l
a
y
l
i
s
t
s
,

p
l
a
y
b
a
c
k
,

r
e
c
o
m
m
e
n
d
a
t
i
o
n
s
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:

S
t
a
t
e
,

S
t
r
a
t
e
g
y
,

S
i
n
g
l
e
t
o
n
,

P
r
o
x
y

U
M
L

(
T
e
x
t
)
:

S
t
r
e
a
m
i
n
g
S
e
r
v
i
c
e

-
>

T
r
a
c
k

-
>

P
l
a
y
l
i
s
t

-
>

U
s
e
r

Q25: Online Food Delivery


D
e
s
i
g
n

f
o
o
d

d
e
l
i
v
e
r
y

w
i
t
h

r
e
s
t
a
u
r
a
n
t
s
,

o
r
d
e
r
s
,

d
i
s
p
a
t
c
h
,

d
e
l
i
v
e
r
y

s
t
a
t
e
s
.

D
e
s
i
g
n

P
a
t
t
e
r
n
s

U
s
e
d
:
S
t
a
t
e
,

S
t
r
a
t
e
g
y
,

S
i
n
g
l
e
t
o
n
,

F
a
c
a
d
e
,

O
b
s
e
r
v
e
r
U
M
L

(
T
e
x
t
)
:

F
o
o
d
D
e
l
i
v
e
r
y
S
e
r
v
i
c
e

-
>

O
r
d
e
r

-
>
R
e
s
t
a
u
r
a
n
t

-
>

R
i
d
e
r
Q19: Snake & Ladder -- Full TypeScript Implementation (Interview-ready, commented)
B
e
l
o
w

i
s

f
u
l
l
,

c
o
m
m
e
n
t
e
d

T
y
p
e
S
c
r
i
p
t

i
m
p
l
e
m
e
n
t
a
t
i
o
n

o
f

t
h
e

S
n
a
k
e

&

L
a
d
d
e
r

g
a
m
e
.

/**
* Snake & Ladder -- TypeScript Implementation (Interview-ready)
*
* Patterns used:
* - Strategy: Dice rolling strategies (Normal, Crooked, Weighted)
* - Factory: BoardFactory to create default or custom board
* - Observer: Game observers can subscribe to move events (UI, logger)
* - State (optional): Player lifecycle (Waiting, Playing, Finished)
*
* Save as: snake_and_ladder.ts
* Run with: npx ts-node snake_and_ladder.ts
*/

/* ---------------------------
TypeScript notes:
- Use `number` (primitive) for numeric types (avoid `Number` wrapper).
- Use `readonly` for values that should not be reassigned.
- Interfaces define contracts (like Strategy interfaces).
- Use `Map<number, number>` for snakes/ladders mapping.
- Use explicit access modifiers (`public`, `private`, `protected`) for clarity.
--------------------------- */

// ------------------- Dice Strategy -------------------


// Strategy interface for dice rolling. Different implementations can be provided.
interface DiceStrategy {
roll(): number; // return an integer roll (e.g., 1..6)
}

// Normal 6-sided dice


class NormalDice implements DiceStrategy {
roll(): number {
// [Link]([Link]()*6) + 1 yields 1..6
return [Link]([Link]() * 6) + 1;
}
}

// Crooked dice: only even values (2,4,6)


class CrookedDice implements DiceStrategy {
roll(): number {
const evens = [2, 4, 6];
return evens[[Link]([Link]() * [Link])];
}
}

// Weighted dice: emphasize higher numbers (example)


class WeightedDice implements DiceStrategy {
roll(): number {
const choices = [1,2,3,4,5,6,6,6]; // 6 is more likely
return choices[[Link]([Link]() * [Link])];
}
}

// ------------------- Board & Factory -------------------


class Board {
readonly size: number; // total cells (e.g., 100)
readonly snakes: Map<number, number>;
readonly ladders: Map<number, number>;

constructor(size: number, snakes?: Map<number, number>, ladders?: Map<number, number>) {


[Link] = size;
[Link] = snakes ?? new Map<number, number>();
[Link] = ladders ?? new Map<number, number>();
}

// apply snakes/ladders chain until no more present at that square


applySnakesAndLadders(pos: number): number {
let cur = pos;
// loop till no snake/ladder found to handle chained ladders/snakes
while (true) {
if ([Link](cur)) {
cur = [Link](cur)!;
} else if ([Link](cur)) {
cur = [Link](cur)!;
} else {
break;
}
}
return cur;
}
}

class BoardFactory {
// create standard 10x10 board with some default snakes/ladders
static createDefaultBoard(): Board {
const snakes = new Map<number, number>([
[99, 21],
[95, 75],
[92, 88],
[89, 68],
[74, 53],
[62, 19],
[64, 60],
[49, 11],
[46, 25],
[16, 6]
]);
const ladders = new Map<number, number>([
[2, 38],
[7, 14],
[8, 31],
[15, 26],
[21, 42],
[28, 84],
[36, 44],
[51, 67],
[71, 91],
[78, 98],
[87, 94]
]);
return new Board(100, snakes, ladders);
}
}

// ------------------- Observer (Game events) -------------------


interface GameObserver {
onMove(playerName: string, from: number, to: number, roll: number): void;
onWin(playerName: string): void;
}

class ConsoleLogger implements GameObserver {


onMove(playerName: string, from: number, to: number, roll: number) {
[Link](`${playerName} rolled ${roll} and moved from ${from} -> ${to}`);
}
onWin(playerName: string) {
[Link](`[WIN] ${playerName} wins the game!`);
}
}

// ------------------- Player (basic, optional state) -------------------


enum PlayerState {
WAITING = "WAITING",
PLAYING = "PLAYING",
FINISHED = "FINISHED"
}

class Player {
position: number = 0;
state: PlayerState = [Link];

constructor(public readonly name: string) {}

setState(s: PlayerState) { [Link] = s; }


}
// ------------------- Game -------------------
class Game {
private currentIdx: number = 0;
private observers: GameObserver[] = [];

constructor(
public readonly board: Board,
public readonly players: Player[],
private readonly dice: DiceStrategy
) {
if ([Link] === 0) throw new Error("At least one player required");
for (const p of players) [Link]([Link]);
players[0].setState([Link]);
}

subscribe(obs: GameObserver) { [Link](obs); }


unsubscribe(obs: GameObserver) { [Link] = [Link](o => o !== obs); }

private notifyMove(player: Player, from: number, to: number, roll: number) {


for (const o of [Link]) [Link]([Link], from, to, roll);
}
private notifyWin(player: Player) {
for (const o of [Link]) [Link]([Link]);
}

playTurn(): boolean {
const player = [Link][[Link]];
if ([Link] === [Link]) {
[Link]();
return false;
}

const roll = [Link]();


const from = [Link];
let tentative = from + roll;

if (tentative > [Link]) {


[Link](player, from, from, roll);
[Link]();
return false;
}

const finalPos = [Link](tentative);


[Link] = finalPos;

[Link](player, from, finalPos, roll);

if (finalPos === [Link]) {


[Link]([Link]);
[Link](player);
return true;
}

if (roll === 6) {
return false;
}

[Link]();
return false;
}

private advanceTurn() {
const n = [Link];
for (let i = 1; i <= n; i++) {
const idx = ([Link] + i) % n;
if ([Link][idx].state !== [Link]) {
[Link] = idx;
break;
}
}
}

playAll() {
let ended = false;
while (!ended) {
ended = [Link]();
}
}
}

// ------------------- Demo usage -------------------


function demo() {
const board = [Link]();
const players = [new Player("Alice"), new Player("Bob"), new Player("Charlie")];
const dice = new NormalDice(); // swap strategies: CrookedDice, WeightedDice
const game = new Game(board, players, dice);
[Link](new ConsoleLogger());
[Link]();
}

// Run demo if this script is executed directly (in Node)


// demo();
Notes & Next Steps
T
h
i
s

d
o
c
u
m
e
n
t

i
n
c
l
u
d
e
s

c
o
m
p
a
c
t

d
e
s
i
g
n
s

f
o
r
Q
1
-
Q
2
5

a
n
d

f
u
l
l

T
y
p
e
S
c
r
i
p
t

i
m
p
l
e
m
e
n
t
a
t
i
o
n

f
o
r

Q
1
9

(
S
n
a
k
e

&

L
a
d
d
e
r
)
.

W
h
a
t

i
n
c
l
u
d
e
d
:

-
P
r
o
b
l
e
m

s
t
a
t
e
m
e
n
t
,

d
e
s
i
g
n

p
a
t
t
e
r
n
s
,

a
n
d

U
M
L

(
t
e
x
t
)

f
o
r

e
a
c
h

o
f

t
h
e

2
5

L
L
D

p
r
o
b
l
e
m
s
.

F
u
l
l
,

c
o
m
m
e
n
t
e
d

T
y
p
e
S
c
r
i
p
t

i
m
p
l
e
m
e
n
t
a
t
i
o
n

f
o
r

S
n
a
k
e

&

L
a
d
d
e
r

(
Q
1
9
)

w
i
t
h

S
t
r
a
t
e
g
y
+
F
a
c
t
o
r
y
+
O
b
s
e
r
v
e
r

a
n
d

o
p
t
i
o
n
a
l

S
t
a
t
e

u
s
a
g
e
.

N
e
x
t

s
t
e
p
s

(
I
c
a
n

d
o

o
n

r
e
q
u
e
s
t
)
:

E
x
p
o
r
t

f
u
l
l

T
y
p
e
S
c
r
i
p
t
f
i
l
e
s

f
o
r

e
a
c
h

q
u
e
s
t
i
o
n

i
n
t
o

z
i
p

(
o
n
e

f
i
l
e
p
e
r

q
u
e
s
t
i
o
n
)
.

I
n
c
l
u
d
e

r
u
n
n
a
b
l
e

d
e
m
o
s

a
n
d

u
n
i
t

t
e
s
t
s

f
o
r

s
e
l
e
c
t
e
d

p
r
o
b
l
e
m
s
.

P
r
o
d
u
c
e

o
n
e
-
p
a
g
e

p
r
i
n
t
a
b
l
e

c
h
e
a
t

s
h
e
e
t
s

p
e
r

q
u
e
s
t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