Practice question · Sort into groups
A stack ADT is being documented. Sort each statement by whether it belongs in the published interface or is an implementation detail.
Groups: Belongs to the interface · Is an implementation detail
- pop removes and returns the most recently pushed element
- peek returns the top element without removing it
- The elements are held in a contiguous array of capacity 64
- An integer field records how many elements are currently stored
Hints
- Ask of each statement: would it still be true if the stack were rebuilt on linked nodes instead?
- Anything that survives a change of internal representation is part of the promise; anything that does not is machinery.
Show the answer
Belongs to the interface: pop removes and returns the most recently pushed element, peek returns the top element without removing it
Is an implementation detail: The elements are held in a contiguous array of capacity 64, An integer field records how many elements are currently stored
Why
The interface is what callers may rely on, the behaviour of push, pop and peek, including what happens in the error case. Arrays, capacities, counters and doubling are all machinery that a linked-node implementation would replace entirely, so no caller may depend on them.
Practise Abstract Data Use
The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.
More questions on Abstract Data Use
- A stack ADT implemented with a fixed-size array can overflow when pushed too many times; the same ADT over a…
- A team replaces a stack's array implementation with a linked-node one. The published operations behave…
- A library documents a function's behaviour but not its complexity. Callers then write loops assuming it is…
- Order what happens when an object is created and then used.
- Because an ADT's interface says nothing about cost, two correct implementations of the same interface can…