You only have free questions left (including this one).

But it doesn't have to end here! Sign up for the 7-day coding interview crash course and you'll get a free Interview Cake problem every week.

A crack team of love scientists from OkEros (a hot new dating site) have devised a way to represent dating profiles as rectangles on a two-dimensional plane.

They need help writing an algorithm to find the intersection of two users' love rectangles. They suspect finding that intersection is the key to a matching algorithm so powerful it will cause an immediate acquisition by Google or Facebook or Obama or something.

Two rectangles overlapping a little. It must be love.

Write a function to find the rectangular intersection of two given love rectangles.

As with the example above, love rectangles are always "straight" and never "diagonal." More rigorously: each side is parallel with either the x-axis or the y-axis.

They are defined as instances of the Rectangle class:

class Rectangle { private: // coordinates of bottom left corner int leftX_; int bottomY_; // dimensions int width_; int height_; public: Rectangle() : leftX_(0), bottomY_(0), width_(0), height_(0) { } Rectangle(int leftX, int bottomY, int width, int height) : leftX_(leftX), bottomY_(bottomY), width_(width), height_(height) { } int getLeftX() const { return leftX_; } int getBottomY() const { return bottomY_; } int getWidth() const { return width_; } int getHeight() const { return height_; } bool operator==(const Rectangle& other) const { return leftX_ == other.leftX_ && bottomY_ == other.bottomY_ && width_ == other.width_ && height_ == other.height_; } bool operator!=(const Rectangle& other) const { return !(*this == other); } };

Your output rectangle should be a Rectangle object as well.

What if there is no intersection? Does your function do something reasonable in that case?

What if one rectangle is entirely contained in the other? Does your function do something reasonable in that case?

What if the rectangles don't really intersect but share an edge? Does your function do something reasonable in that case?

Do some parts of your function seem very similar? Can they be refactored so you repeat yourself less?

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

time and space.

What if we had a vector of rectangles and wanted to find all the rectangular overlaps between all possible pairs of two rectangles within the vector? Note that we'd be returning a vector of rectangles.

What if we had a vector of rectangles and wanted to find the overlap between all of them, if there was one? Note that we'd be returning a single rectangle.

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

Reset editor

Powered by qualified.io

. . .