JS Built-ins
TypeScriptintermediate

Generics β€” Generic Stack Class

πŸ“– Usage Example
const stack = new Stack<number>()
stack.push(1)
stack.push(2)
stack.pop()   // β†’ 2

A generic Stack<T> class enforces a single element type across push, pop, and peek.

Generic Classes: Stack<T>

Challenge: Implement a generic Stack<T> class with push, pop, peek, and a size getter.

const s = new Stack<number>()
s.push(1); s.push(2)
s.peek()  // β†’ 2
s.pop()   // β†’ 2
s.size    // β†’ 1
πŸ’‘ Hints (1)
  • β€’ Arrays have push/pop built-in which map directly to stack operations.