As reported: Oracle OSM IC3 Senior Software Engineer (Hyderabad), round 2 (technical: project + DSA), Feb 2025 — design your own stack with push and pop, backed by a linked list.
Implement a stack of integers on a singly linked list you write yourself (no built-in stack, list or array to hold the elements):
LinkedStack()creates an empty stack.push(x)putsxon top.pop()removes the top element and returns it; returns-1if the stack is empty.peek()returns the top element without removing it; returns-1if the stack is empty.isEmpty()returns whether the stack has no elements.
Every operation must run in O(1) time.
Example
["LinkedStack","push","push","peek","pop","pop","isEmpty"]
[[],[1],[2],[],[],[],[]]
Output: [null,null,null,2,2,1,true]
Constraints: 0 ≤ x ≤ 10^9; at most 10^5 calls.
Code runner format: line 1 holds the operation names and line 2 their arguments (LeetCode's format); the harness prints the list of return values (null for the constructor and push). In Go the methods are Push, Pop, Peek, IsEmpty and the constructor is Constructor().
Clarifications
popandpeekon an empty stack return-1(pushed values are never negative).