Author: Wen Tailai

Nine chapters algorithm “Object Oriented Design OOD” gold medal lecturer

Amazon senior engineer, years of experience as an interviewer, has received offers from Google, Facebook, Uber and many other companies

As you all know, a large number of technology companies have been laying off employees due to the epidemic this year. Amazon, where I work, is one of the few companies that has been hiring, and this time it will offer 20,000 technical jobs.

Almost every week I attend interviews (now VO) and mainly design interviews, so I know that many candidates have the same problems. During WFH, I’ll put together a list of the most common questions and explain how to judge a design interview with specific examples.

What is the difference between OOD and system design?

In the process of teaching for many years, students often ask: teacher, what is the difference between OOD and system design?

Here’s a simple picture to illustrate the difference between the two in an interview:

It should be emphasized that the comparison of these five aspects does not necessarily mean that you will encounter OOD or system design in which position you are in, but from a statistical point of view, when you are in this condition, you are more likely to be admitted.

The employment situation is very bad this year. The interview difficulty of companies including Linen has been increased. Many positions have passed the OOD examination and a round of system design.

It can be said that in addition to the algorithm, OOD must test! Interviewers often use this to judge a programmer’s fundamentals and big picture.

Special emphasis: I have met several students of level 4 or 5 this year, the algorithm answered very well, but the design was red light, we directly failed after review!

From the novice to the master, OOD solves the problem in 5 steps

Since OOD is so important, the question arises: How do you judge an OOD interview? What are the criteria?

Many students ask: when I know the face algorithm, I can judge whether I have done well by the time complexity or the space complexity. But the design I drew a whole whiteboard, also said a lot of a lot, but the face is still meng force!

Because OOD interviews cover a wide range of areas and there is no standard answer, it is easy to feel confused. The key to design interview is to communicate clearly and then start, than you do a big and complete thing more points, here is going to use 5C solution method.

What is the 5C solution?

  • Clarify: To Clarify questions by communicating with your interviewer
  • Core Objects: Identify the classes covered by the topic and the mapping between the classes
  • Cases: Identifies the scenarios and functions that need to be implemented in the title
  • Classes: Use class diagrams to concretely populate the Classes designed in the topic
  • Correctness: check your design to see if it meets the critical points

Combine a real question to feel the feelings:

“Can you design an elevator system for this building?”

This is a very classic design elevator problem, many people will think this problem I will, steady steady! Get started!

Step 1: Clarify requirements

Me: How many people and how much weight can this elevator carry? What’s the height of the building?

Interviewer: 13 persons, maximum 1050kg. 20 floors.

But how does knowing the numbers of these common attributes help you with your design results? Whether the elevator can carry three people or 13 people, 20 floors or 40 floors, it doesn’t really matter.

To think differently, you can ask your interviewer:

  • Do you need to consider overloading?

  • Is it necessary to design two types of elevators (passenger elevators or freight elevators)? If so, what is the relationship between them?

  • Are the floors reached by passenger and freight elevators different?

  • How many elevators in this building respond when someone presses the elevator button?

  • Which buttons respond when the elevator is moving?

These are common problems in the elevator system, but are often ignored by most people. Now, is anyone even more confused? There are so many directions, how can you think of everything?

Don’t panic! The interviewer is looking for clarity and positive communication, not to think of every possible scenario.

Step 2: Core Objects

When facing the OOD problem, you can first write on the whiteboard your design must have the class. So let’s just put the ElevatorSystem in there, and then do linear thinking, what comes in, what goes out?

  • Someone sent a Request to my elevator system, so Request;

  • I also need to schedule an Elevator to respond to each request, so Elevator;

  • Elevators also have floor buttons, fire buttons, etc., so you need to add the ElevatorButton

There is a mapping between ElevatorButton and Elevator, so we have to draw a lot of elevators and a lot of Elevator buttons underneath.

After completing the first two steps, your whiteboard should look like this:

The Core Objects step is used to:

  • Help the subsequent design figure out which classes you need;

  • It can connect the previous and the next, from clarify results, and become the basis of use case;

  • It also lays the foundation for drawing class diagram

If you want to know more about Good Practice in an interview, welcome to listen to my online sharing, and I will show you in more detail.

How to listen to online sharing?

Long press Scan code – jump and click free audition, or click object Oriented Design OOD

Step 3: Cases Identifies scenarios and functions

Cases are the second agreement you have with your interviewer in black and white, listing the functions you want to achieve; At the same time, it can also help you clarify your thinking and achieve a Case; Finally as a standard for inspection.

Each use case only needs to be described in one sentence. Considering space, I will put the figure directly:

Step 4: Classes populate the concrete Classes

An important part of OOD interview is to draw a class diagram. Why? You need to understand the role of class diagrams in a real interview scenario:

  • The first class diagram is the minimum deliverable

  • Save the interview time, not easy to struggle in coding

  • At the same time, it is established on the basis of use case, clear and easy to communicate and modify

  • Finally, if time permits, it is convenient to convert to code

This is where you need to know what the interviewer is thinking, and they are not going to judge you for just drawing and not writing code. Don’t panic!

Therefore, the probability of meeting OOD in the interview is very low, because it is often necessary to have a whiteboard to discuss and revise with the interviewer.

Step 5: Check the key points of the design

“Follow up: For this elevator system, if I had to have different algorithms for peak and low flow times, how should I design it?”

I’ll just show you two ways to do it:

【 mode 1】if – else

Here we have dealt with peak and trough (Normal), but this approach is not optimal, and if in the future MY rule becomes that weekdays are one system and weekends are another system, combining peak and trough conditions at the same time, this approach will need to be greatly modified.

【 mode 2】Strategy Design Pattern

This is also a core point I mentioned in the OOD free trial open class. I will share the class diagram first:

The advantage of this class diagram is that it encapsulates a variety of algorithms and policies so that algorithms/policies can be replaced with each other. It can be well read and understood by others and can be easily modified or reused in the future.

Some students may not fully understand the Strategy Pattern at this point, so you may wish to listen to my free sharing lesson, in which I will talk about the specific code implementation:

How to listen to the free sharing lesson?

Long press scan code, click free demo, or click OBJECT Oriented Design OOD.