---
title: "Unfair service centre simulation"
subject: "Data Structure and Algorithm"
chapter: 2
key: "semester_4/data_structure_and_algorithm:unfair-service-centre-simulation"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/unfair-service-centre-simulation
site: BCT Engineering
---

# Unfair service centre simulation

Data Structure and Algorithm · Chapter 2: The Stack and Queue

- Asked in: 2068 Magh, 4 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/unfair-service-centre-simulation
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-2

## Question

In some unfair customer service centre, a person who comes at last gets services at first and the persons who come at first are still waiting for the services up to last. Devise a suitable algorithm required for the simulation of this system.

## Short answer

A stack (LIFO) models the centre. Push arriving customers; when counter free, pop and serve. Example: C1, C2, C3 arrive, C3 served first; later C4 arrives, served before C2 and C1. Early arrivals may starve if new customers keep arriving.

## Answer

#### Data structure

The last person to arrive is served first, and the first person waits longest. This is **LIFO**, so a **stack** models the service centre. Arrival is a `push`; serving a customer is a `pop`.

```text
   arrive -> push                 serve -> pop
              | C3 | <- top  (served first)
              | C2 |
              | C1 | <- bottom (waits longest)
```

#### Algorithm

```text
1. Create an empty stack S  (top = -1 for an array of size MAX)
2. Repeat until the simulation ends:
     if a customer c arrives:
         if S is full:  print "Centre full, customer turned away"
         else           PUSH(S, c)
     if the counter is free:
         if S is empty: print "No customer waiting"
         else           c = POP(S);  serve c
3. When closing time comes, customers left in S are never served (starvation).
```

```text
PUSH(S, c):  if top == MAX-1 -> overflow;  else top = top + 1; S[top] = c
POP(S):      if top == -1    -> underflow; else c = S[top]; top = top - 1; return c
```

#### Example trace

| Event | Stack (bottom → top) | Served |
|---|---|---|
| C1 arrives | C1 | - |
| C2 arrives | C1, C2 | - |
| C3 arrives | C1, C2, C3 | - |
| Counter free | C1, C2 | C3 |
| C4 arrives | C1, C2, C4 | - |
| Counter free | C1, C2 | C4 |
| Counter free | C1 | C2 |
| Counter free | (empty) | C1 |

If new customers keep arriving before the counter becomes free, C1 and C2 are never served, which is why the centre is "unfair".

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which data structure models a centre where the last arrival is served first?
   A. Queue
   B. Stack
   C. Linked list only
   D. Array of sorted names
   Answer: B. Last in first out is the stack's LIFO behaviour. (Key point: Choice of structure)

2. What do arrival and serving correspond to?
   A. Arrival = pop, serving = push
   B. Arrival = push, serving = pop
   C. Both are push
   D. Both are pop
   Answer: B. A customer is pushed on arrival and popped to be served. (Key point: Operations)

3. If a customer arrives when the stack is full, what does the algorithm do?
   A. Overwrites the top
   B. Turns the customer away
   C. Serves them immediately
   D. Pops the bottom
   Answer: B. A full stack means overflow so the customer is turned away. (Key point: Full stack)

4. Why do early customers like C1 may never be served?
   A. They are deleted
   B. New arrivals keep being served before them
   C. The stack is a queue
   D. They are in the top
   Answer: B. While new customers keep arriving, they sit above C1 and are popped first. (Key point: Starvation)

## Related in Data Structure and Algorithm

- [Petrol pump queue simulation](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Apetrol-pump-simulation) — Data Structure and Algorithm, chapter 2
- [Doubly linked list: bank customer records](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-bank-customer) — Data Structure and Algorithm, chapter 4
- [Delete last node of doubly linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adelete-end-node-doubly) — Data Structure and Algorithm, chapter 4
- [Depth first traversal in graph](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adfs-graph-explain) — Data Structure and Algorithm, chapter 10, asked 4×
- [Breadth first traversal in graph](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abfs-graph-explain) — Data Structure and Algorithm, chapter 10, asked 2×

## Related in other subjects

- [KUKL customer system design and code](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Akukl-use-case-class-diagram-code) — Object Oriented Analysis and Design, chapter 4
- [Simulation of computer system serving WWW](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Acomputer-system-www-request) — Simulation and Modeling, chapter 10, asked 2×
- [Define system, model, simulation](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Asystem-model-simulation-define) — Simulation and Modeling, chapter 1
- [Continuous system simulation; analog computer example](https://www.bctengineer.com/question.md?key=semester_8%2Fsimulation_and_modeling%3Acontinuous-simulation-analog-example) — Simulation and Modeling, chapter 3
