I am writing this as very less information is available for wayfair karat assesment.
They are really particular about time. Each segment ends at given time whether you are done with your answer or not .
They have divided 1 hr in 3 category
20 min : about you and your project
20 min : System desgn questions
1) very large XML which cant fit in RAM, how to process
2) song streaming service , saves songs through a LB which uses consistent hashing. What need to be improved when users will stream songs
3) University social n/w based on university id, planning to role out internationally, what needs to be added\changed in backend
4) An app which takes a sports link , download article run machine learning to find any bias and reports back to user. running for 6 months, planning to expand what all the resources need to be added
20 min : codepad, coding or debugging
Review below code and point what can be changed from OOP and best practices perspective.
class ShapesController {
private ObjectRepository<Shape> shapesRepo;
private DBConnection dbConnection;
public ShapesController() {
/* Database user is root with empty password */
dbConnection = new DBConnection("root", "");
/* Used to retrieve shape objects */
shapesRepo = new ObjectRepository<Shape>("shapes");
}
/**
* Return the shape type as a string
*
* @route GET /shapes/:id/type
*/
public Response type(Request req) {
Shape s = shapesRepo.find(req.id);
switch (s.type) {
case 1:
return new OkResponse("rectangle");
case 2:
return new OkResponse("circle");
case 3:
return new OkResponse("point");
default:
/* Invalid shape, fail */
return new BadRequestResponse();
}
}
/**
* Translate (move) a shape and return its new coordinates
*
* @route POST /shapes/:id/translate
*/
public Response translate(Request req) {
Shape s = shapesRepo.find(req.id);
int xOffset = req.params.getInt("x-offset");
int yOffset = req.params.getInt("y-offset");
/* Translation */
s.setX(s.getX() + xOffset);
s.setY(s.getY() + yOffset);
/* Write the change to database */
dbConnection.update("UPDATE Shapes SET x=" + s.getX() + ",y=" + s.getY() + " WHERE id=" + req.id);
String shapeJson = "{\"x\":" + s.getX() + ",\"y\":" + s.getY() + "}";
return new OkResponse(shapeJson, "application/json");
}
}
class Shape {
public int type = 0;
/**
* Each shape has a corresponding bounding box that the rendering mechanism requires
* x and y refer to the location of the top left corner of the bounding box
* width and height are the width and height of the bounding box
*/
protected int x, y, width, height;
public Shape(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public double getArea() throws UnsupportedOperationException {
throw new UnsupportedOperationException("Computed by child classes");
}
}
class Rectangle extends Shape {
public Rectangle(int x, int y, int width, int height) {
super(x, y, width, height);
this.type = 1;
}
public double getArea() {
return width * height;
}
}
class Circle extends Shape {
public Circle(int x, int y, int width, int height) {
super(x, y, width, height);
this.type = 2;
}
public void setWidth(int width) {
/* Because circle is bound by a square */
this.width = width;
this.height = width;
}
public void setHeight(int height) {
/* Because circle is bound by a square */
this.height = height;
this.width = height;
}
public double getArea() {
/* Radius is width of bounding box / 2 */
return Math.PI * squareNum(width / 2);
}
public double squareNum(double x) {
return x * x;
}
}
class Point extends Shape {
/**
* Weight is used by the rendering mechanism to determine how large the
* point looks on the canvas
*/
private int weight;
public Point(int x, int y, int weight) {
super(x, y, 0, 0);
this.type = 3;
this.weight = weight;
}
public double getArea() {
return 0;
}
public int getWeight() {
return weight;
}
}