-2
\$\begingroup\$

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.

\$\endgroup\$
5
  • 2
    \$\begingroup\$ While interview questions aren't necessarily off-topic, CodeReview does require fully-implemented, working code. This code doesn't compile or run. \$\endgroup\$ Commented Sep 5, 2023 at 7:40
  • \$\begingroup\$ @user673679 it is a class design problem. objective is to make object oriented code. why are you trying to compile or run? I did not paste here for build errors or run time bugs. I pasted here for seeking suggestion on an object oriented code for a real life problem. \$\endgroup\$ Commented Sep 5, 2023 at 17:31
  • 1
    \$\begingroup\$ CodeReview requires working code (not pseudo-code) from an actual project, see: codereview.stackexchange.com/help/on-topic . Perhaps softwareengineering.stackexchange.com would be a better fit for a design question. \$\endgroup\$ Commented Sep 5, 2023 at 18:14
  • \$\begingroup\$ @user673679 I pasted my question in the portal you sent but it is closed automatically. any idea why it is closed and how to reopen it? softwareengineering.stackexchange.com/questions/447425/… \$\endgroup\$ Commented Sep 6, 2023 at 18:14
  • \$\begingroup\$ dunno, sorry. I still think software engineering was more appropriate, but I've stuck an answer below anyway. /o\ \$\endgroup\$ Commented Sep 7, 2023 at 13:09

1 Answer 1

1
\$\begingroup\$

Well, I feel like this deserves an answer somewhere, even if it's probably off-topic here. So I'm just going to write an answer and mark it as a community-wiki:


The task:

Depending on how this stage of the interview was conducted, the interviewers may have been more interested in your process (how you think about the design, what you ask about, etc.) than in the actual code produced. But either way, producing well-formed, compileable code (or as close as you can get) shows off your skills better.

The specification given is not detailed enough for any "real" code to be produced immediately. I know I'd have a lot of questions about this:

  • Where is the code running: On the device? but then there's no need for a "set of classes" to simulate other devices - or if each class is only running on the relevant device, there's no need for polymorphism within those classes). On a server somewhere? that might make more sense... but raises questions about why we need to simulate all these devices there at all.

  • It looks like the "say" and "display" responses are always the same... so why are we splitting them into two? We could feed a single message to the device, and then let it decide how to do the displaying. (It seems like we're expected to handle three separate processes here: querying each device for the power status, turning that power status into a message (the details of this step we don't care about too much), and then giving that message back to the device to do the relevant output).

  • Are they actually expecting you to output (what seems to be) well-formatted JSON? (This is an annoying complexity without using a library). Is that supposed to go to std output?

  • The linked Microwave appears (at a first glance) to have a screen too rudimentary to display the supplied message. What to do there?

  • With multiple displays, do we actually need to add more messages with the "display" key (which has JSON implications), give each one a unique name, or stick to a single message?

If they gave you an opportunity to ask questions like this, it's important to take it! If not, pick an option that seems sensible, and write a short comment in the code to say why you made that decision.

In short: this is a very hypothetical exercise, with little "real-world" value. But... perhaps they can tell a lot about how a candidate approaches things, and even about their knowledge of a specific programming language, by giving them even a vague problem like this.


The code:

When using polymorphism in C++, you'd want to:

  • Use the virtual keyword when defining an interface, and the override keyword when writing derived class members.
  • Ensure your classes have a virtual destructor.
  • Show knowledge of why you're using polymorphism in the first place (storing multiple classes of different types in the same container (e.g. a vector) and using them via the same interface). (i.e. you really want to write a main() function for your program that shows how to use the classes).
  • Use a std::unique_ptr to store those objects safely, instead of doing raw memory management.

I'd suggest that polymorphism would not necessarily be appropriate here at all (the "prefer composition to inheritance" design guideline of C++).


string getStatus() { string msg = to_string(percentage); return msg; } 

This function should be const (as should the various printStatus functions).

Although the instructions are sparse on detail, they are fairly precise about the actual message you should be returning, so it's best to be careful to return the exact reply.

It's important to write std::to_string, and include the relevant header at the top. (You should always avoid using namespace std;.)


I think the Battery should also handle charging, and there's no need for a separate Power class.

You also don't seem to use the Battery class anywhere.


The OutputType member isn't actually used anywhere, so it's not clear why this needs to exist.


You don't really need polymorphism for the outputs. The FireTV can keep the speaker and display(s) as separate member variables, just like the other devices.

We should use a std::vector, not a std::list here too. (A list is usually slower than a vector, unless we have many thousands of items to insert and remove, and we don't need iterating the container to be fast. Here we have very few items, and we need to iterate them.)


There's no need to use pointers for the various device member variables. It's much easier to use simple member variables on the stack. (Also, the code doesn't show how those pointers are allocated or cleaned-up - even if you did need pointers, why not use std::unique_ptr instead of raw pointers?).


Range-based for loops in C++ look like for (auto const& output : outputs) .... They don't use foreach. Of course you might not care if you're writing pseudo-code, but it's something basic that the interviewers might notice if they're expecting C++ specifically.


The code doesn't try to actually produce the desired messages; the instructions do say: Focus more on ..., and returning the correct responses. So there's a lot of missing detail here... e.g. how do you return the formatted JSON? how do you show "say" vs "display".


They give you links to each device (most of them, anyway). So if you had the time, it might have been worth looking at each one, and making sure you modelled them all as accurately as possible. (Being systematic and thorough is good. Showing knowledge of their products (being a bit of a suck-up :p ) might not be bad either).

\$\endgroup\$
1
  • \$\begingroup\$ Thanks a lot. I think they want to have a repository of this extensible code for all devices in their sever. They want to deploy it in each device. Yes I did some mistakes - 1. did not use unique_ptr. 2. outputType enum is redundant and should not be used. I did not use battery object because I did not write classes for devices that have batteries. FireTV do have battery but I did not get time to write FireTV class completely. can you please write a pseudo code for this problem? need ans of some qu - firetv dynamic display , echodot and echoshow have lot of common code. \$\endgroup\$ Commented Sep 15, 2023 at 14:22

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.