Counting cars on a machine (could be considered part of LLD/problem-solving overlap)
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2019-06-14
Problem Statement
Design a Car Counting System that detects and counts vehicles passing through a specific point, e.g., on a toll booth, highway lane, or factory assembly line. The focus is on object-oriented design, modularity, and real-time event handling. This problem can also involve problem-solving and low-level design aspects.
✅ Functional Requirements
1.Vehicle Detection
•Detect a car passing a sensor (e.g., weight sensor, laser sensor, camera input).
•Ensure one car is counted once, even if it lingers on the sensor.
2.Counting and Logging
•Maintain a count of vehicles that have passed.
•Log timestamp and optionally vehicle type (car, truck, bike).
3.Multiple Lanes / Sensors
•Support counting on multiple lanes or sensors simultaneously.
•Maintain per-lane counts.
4.Alerts & Thresholds (Optional)
•Trigger alerts when vehicle count crosses a threshold.
•Optionally detect congestion or abnormal patterns.
5.Real-time Queries
•Retrieve total count and per-lane counts.
•Optionally, retrieve counts over a time window (e.g., last hour).
🔐 Constraints & Assumptions
•Sensors can generate events asynchronously.
•Focus is on LLD and event-driven system, not actual hardware integration.
•Assume time-stamped events for each detected vehicle.
🧱 Key Components & Classes
1.Sensor Class
1
2class Sensor {
3private:
4 string sensorId;
5 string laneId;
6public:
7 void detectVehicle(); // triggers a vehicle detection event
8 string getLaneId();
9};1.Vehicle Class (Optional)
1
2class Vehicle {
3private:
4 string vehicleId;
5 string type; // car, truck, bike
6 DateTime timestamp;
7};1.Counter Class
1
2class Counter {
3private:
4 int totalCount;
5 unordered_map<string, int> laneCounts; // laneId -> count
6public:
7 void increment(string laneId);
8 int getTotalCount();
9 int getLaneCount(string laneId);
10};1.EventManager / Controller Class
1
2class EventManager {
3private:
4 Counter* counter;
5 vector<Sensor*> sensors;
6public:
7 void registerSensor(Sensor* sensor);
8 void vehicleDetected(Sensor* sensor);
9};🔄 Example Workflow
1.A car passes a sensor → Sensor.detectVehicle() is called.
2.EventManager.vehicleDetected(sensor) is triggered.
3.Counter.increment(sensor.getLaneId()) updates counts.
4.System logs the event and updates real-time count.
5.Retrieve counts:
•counter.getTotalCount()
•counter.getLaneCount("Lane1")
🧪 Expected Deliverables
•Class diagram showing Sensor, Vehicle, Counter, and EventManager.
•Implementation of vehicle detection, counting, and per-lane tracking.
•Test cases:
•Single lane counting
•Multiple sensors/lane counting
•Handling rapid consecutive detections
•Optional: vehicle type differentiation
💡 Discussion Points
•Handling concurrent sensor events and synchronization.
•Debouncing sensor events to avoid double counting.
•Extensibility for time-based queries, analytics, and alerts.
•Optional: integrating camera-based vehicle detection or classification.