Practice

Stack Push and Pop

HARDStacks12 marks

Implement a stack of up to five integers using an array and a top pointer.

For each operation:

  • P is followed by a value. Push it and output TRUE, or output FALSE if the stack is full.
  • O pops and outputs the top value, or outputs -1 if the stack is empty.

Input: An operation count followed by operations and push values. Output: One result per operation.

Example:

Input:  4
        P 10
        P 20
        O
        O
Output: TRUE
        TRUE
        20
        10
Premium is coming soon. All grading features are currently unlocked.

Sample Test Cases

Test 1: LIFO order
Inputs: 4, P, 10, P, 20, O, O
Expected: TRUE TRUE 20 10
Test 2: Empty stack
Inputs: 2, O, O
Expected: -1 -1