Glovo || software engineer 2 || barcelona

Round 1 : LIVE CODING

Problem: Dinner Plate Stacks

Problem Description

  • Design and implement a data structure to simulate stacks of dinner plates arranged in piles.
  • Each pile can hold a fixed number of plates (capacity).
  • When pushing a plate, it should go onto the leftmost pile that is not full.
  • When popping a plate, it should come from the rightmost non-empty pile.
  • You can also pop a plate from a specific pile by index.

API or Method Signatures
public void push(int val) //Pushes a plate with value val onto the leftmost pile with space.
public int pop() //Pops and returns the plate from the rightmost non-empty pile.
public int popAtStack(int index) //Pops and returns the plate from the pile index.

Constraints

  • Capacity is a positive integer.
  • Index in popAtStack may refer to a non-existent or empty pile.
  • If no plates are available to pop, return -1.
  • There is no limit on the number of plates

Example Input/Output
DinnerPlateStacks stacks = new DinnerPlateStacks(2);
stacks.push(1);
stacks.push(2);
stacks.push(3);
stacks.push(4);
stacks.push(5);
// The stacks are now: 2 4
// 1 3 5
// ﹈ ﹈ ﹈
stacks.popAtStack(0); // Output: 2
// The stacks are now: 4
// 1 3 5
// ﹈ ﹈ ﹈
stacks.push(20);
// The stacks are now: 20 4
// 1 3 5
// ﹈ ﹈ ﹈
stacks.push(21);
// The stacks are now: 20 4 21
// 1 3 5
// ﹈ ﹈ ﹈
stacks.popAtStack(0); // Output: 20
// The stacks are now: 4 21
// 1 3 5
// ﹈ ﹈ ﹈
stacks.popAtStack(2); // Output: 21
// The stacks are now: 4
// 1 3 5
// ﹈ ﹈ ﹈
stacks.pop(); // Output: 5
// The stacks are now: 4
// 1 3
// ﹈ ﹈ ﹈
stacks.pop(); // Output: 4
// The stacks are now:
// 1 3
// ﹈ ﹈ ﹈

PS : goal is give a working solution with test cases

Comments (4)