Practice
Stack Find Largest and Smallest
2025/Oct/Nov·Variant 1·Q1·[10 marks]
MEDIUMStacks
A stack of up to 10 integers is stored in StackData with a Top pointer starting at -1.
Write:
- function
Push(Value)— store the value and returnTRUE, or returnFALSEif the stack is full - procedure
FindExtremes()— pop every remaining value until the stack is empty, then output the largest and smallest values that were popped, in the formatLargest: <n>thenSmallest: <n>
The test harness reads a count, then that many integers, pushes each one, and calls FindExtremes(). You can assume there is at least one value.
Input: A count, then that many integers. Output: Two lines — the largest then the smallest.
Example:
Input: 4
8
3
15
6
Output: Largest: 15
Smallest: 3
Premium is coming soon. All grading features are currently unlocked.
Sample Test Cases
Test 1: Mixed positives
Inputs: 4, 8, 3, 15, 6
Expected: TRUE
TRUE
TRUE
TRUE
Largest: 15
Smallest: 3
Test 2: Single value
Inputs: 1, 42
Expected: TRUE
Largest: 42
Smallest: 42