This assessment is to evaluate your ability to design and implement a robust, high-performance API using MongoDB, Mongoose, and Express. The system must handle real-time inventory updates, ensure data consistency etc during a flash sale event that occurs multiple times a month.
- Server Setup Develop a basic backend system that provides RESTful APIs to manage the flash sale event and user purchases. Establish a connection to a MongoDB database, ensuring it is structured to handle high transaction loads efficiently.✅ Implement proper application structure for scalability, maintainability, and performance.✅
- Flash Sale Simulation A product will be available for purchase with 200 allocated units at the start of each sale event. Users should be able to see in real time the remaining stock and buy the product, and after each successful transaction, the number of available units should decrease accurately. The system must prevent under-purchasing (units not reducing) or over-purchasing (going negative) under high traffic conditions, the total amount of units must be exact at the end of the sale so far users purchase successfully. The flash sale should be time-restricted, meaning: It begins at a specific, predefined time. It continues until all units are sold out. The sale can restart multiple times within a month, maintaining a possibility of proper inventory resets.
- Leaderboard API The system must also expose an API to fetch a list of all users who successfully purchased the product, sorted in chronological order based on purchase time. The leaderboard must be efficiently queryable, even under high database loads.
- Error Handling & System Robustness The system should gracefully reject purchases when stock runs out and provide meaningful error responses. Ensure users cannot bypass the sale timing restrictions to buy before the flash sale starts.
- Security Considerations If you can, Implement security measures to prevent fraudulent transactions Ensure that a user cannot purchase more than the allowed quantity per transaction (if any limit exists). Bonus Enhancements Consider query performance at scale Handle race conditions and concurrency control Implement basic authentication
const obj: { [key: string]: string } = {
key1: 'value1',
key2: 'value2'
}
const getObj = (key: string): string => {
return obj[key] || 'Value does not exist';
}
console.log(getObj('key3')); // Output: value1