All academies
All challenges

Build a Generic Stack

mediumclasses100 XP

Problem

Implement a generic Stack<T> class backed by a private array, with push(item: T): void, pop(): T | undefined, peek(): T | undefined, and a size getter. A Stack<number> must reject strings at compile time, and pop/peek must reflect that the stack may be empty by including undefined in their return types.

Expected output

3
2
2

Hints

Stuck? Reveal hints one at a time.

Solution

Try solving it yourself first — revealing is always available.

Press Run in the editor — your console output is checked automatically.
No errors
Loading...

Console output appears here. Press Run or just type — the code compiles automatically.