I was asked below quesion in Amazon sde2 low level design interview recently and I have shown below my solution during the interview. I was rejected in this round.
// There are a wide variety of Alexa devices // 1. Alexa devices that only have a speaker (Echo Dot, Echo Flex, https://www.amazon.com/dp/B07FZ8S74R) // 2. Alexa devices that only have a screen/display (Alexa enabled Microwave or AC, https://www.amazon.com/dp/B07894S727) // 3. Alexa devices that have both, speaker and screen (Echo Show, Echo Spot, https://www.amazon.com/dp/B08KJN3333). // 4. Alexa devices that have neither a speaker, nor a screen (Echo Input, Echo link, https://www.amazon.com/dp/B0798DVZCY). // 5. Alexa devices that have a speaker, but can be connected to a display (FireTV cube, https://www.amazon.com/dp/B08XMDNVX6). // // Also, // 1. Some Alexa devices that have batteries (Fire Tablets, Echo Tap, Echo Buds, https://www.amazon.com/dp/B085WTYQ4X) // 2. Others that do not have batteries (Echo Dot, Echo Show). // // Design a set of classes that will report the current battery/power status to the user. // Depending on the hardware, the response may need to be spoken, or displayed on a screen, or both. // Also, depending on whether there is a battery or not, the status message will differ. // For example, if the device is a Tablet which has a battery, a speaker, and a display, and currently // it happens to be plugged in and recharging (let's say at 75%), then your code should return the following: // { // "say": "Current battery level is 75% and charging", // "display": "Current battery level is 75% and charging" // } // // Whereas if the device is an Echo Dot, which has a speaker but no battery and no screen, // then your code should only return: // { // "say": "Currently plugged into wall power" // } // // and should NOT attempt to display anything (since there is no screen). // // For simplicity, ignore the details of speech generation and image/visual card generation, we can safely assume those are provided. // Focus more on modeling the Alexa devices and their properties, and returning the correct responses. My solution is below in C++
class Battery { int percentage; public: string getStatus() { string msg = to_string(percentage); return msg; } }; class Power { bool isCharging; public: string getStatus() { string msg = isCharging ? "" : ""; return msg; } }; enum OutputType { DISPLAY , SPEAKER }; class Output { OutputType outputType; public: void print(string msg) = 0; }; class Speaker : public Output { public: void print(string msg) { ... } }; class Display : public Output { public: void print(string msg) { ... } }; class Device { public: virtual void printStatus() = 0; }; class EchoDot : public Device { Output* speaker; Power *power; public: void printStatus() { string msg = power->getStatus(); speaker->print(msg); } }; class EchoShow : public Device { Output* speaker; Output* display; Power *power; public: void printStatus() { string msg = power->getStatus(); speaker->print(msg); display->print(msg); } }; class FireTV : public Device { list<Output*> outputs; Power *power; public: void printStatus() { string msg = power->getStatus(); foreach output in outputs output->print(msg); } void addOutput(Output* output) { outputs.push_front(output); } void removeOutput(Output* output) { } }; Interviewer told me to make the design extensible so that tomorrow if a new device comes up with an array of led displays, it can be added easily with minimal changes to existing code.
Note that FireTV can be connected to a Display dynamically.
Also he told me that EchoShow and EchoDot has lot of common and that should be put in a common base class.