Implement a Generic Stack in Java

viaGlassdoor

Requirements: Implement a generic (type-parameterized) Stack class in Java supporting push, pop, peek, and isEmpty operations.

Design considerations: Use Java generics (Stack<T>) backed by an array or a linked list; handle stack-empty and stack-overflow (if array-backed with fixed capacity) edge cases; ensure O(1) time for all core operations.

Approach: Back the stack with a dynamic array (ArrayList<T>) or a singly linked list of generic nodes. push() adds to the top/head, pop() removes and returns the top/head (throwing/handling an exception on empty stack), peek() returns without removing, isEmpty() checks size == 0.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account