• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Find if two rectangles overlap

August 27, 2014 by Dhaval Dave

Given two rectangles, find if the given two rectangles overlap or not.

Note that a rectangle can be represented by two coordinates, top left and bottom right. So mainly we are given following four coordinates.

l1: Top Left coordinate of first rectangle.
r1: Bottom Right coordinate of first rectangle.
l2: Top Left coordinate of second rectangle.
r2: Bottom Right coordinate of second rectangle

Two rectangles do not overlap if one of the following conditions is true.

1) if  l1.x > r2.x  OR l2.x > r1.x  // If one rectangle is on left side of other Case (3)
2) if  l1.y < r2.y  OR  l2.y < r1.y // If one rectangle is above other Case(4) not presented here.

Case 1) and 2) are Overlapping.
hence here it should return false.

Code : Following code returns true if two rectangles overlap.

struct Coord
{
    int x, y;
};
bool RectOverlap(Coord l1, Coord r1, Coord l2, Coord r2)
{
    if (l1.x > r2.x || l2.x > r1.x) return false;
    if (l1.y < r2.y || l2.y < r1.y) return false;

    return true; //else in all condition
}

Similar Articles

Filed Under: Amazon Interview Question, Interview Questions, Microsoft Interview Questions, problem Tagged With: Mathematical

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Amazon Interview Experience – SDE Chennai

SAP Off Campus Hiring_ March 2015 Sample Questions

Calculate price of parking from parking start end time prices

Connect n ropes with minimum cost

Add Sub Multiply very large number stored as string

Longest Increasing Subsequence

Inorder and Preorder traversals of a Binary Tree given. Output the Postorder traversal of it.

Count number of ways to reach a given score in a game

SAP Off Campus Hiring_ March 2015 Verbal Skills

LeetCode : Word Search

Maximum difference between two elements s.t larger element appears after the smaller number

Generic Object Oriented Stack with Template

Daughter’s Age VeryGood Puzzle

Walmart Labs Interview Experience

Printing each word reverse in string

Singly linked list

SAP Off Campus Hiring_ March 2015 Computer Skills

Adobe Interview Questions 8 month Exp

Interfaces in C++ (Abstract Classes in C++)

Find the number ABCD such that when multipled by 4 gives DCBA.

CodeChef Code SGARDEN

DFS (Depth First Search)

Flipkart SDET Interview Experience

Given a sorted array and a number x, find the pair in array whose sum is closest to x

FizzBuzz Solution C C++

Coin Collection Dynamic Programming

Find position of the only set bit

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

Find min element in Sorted Rotated Array (Without Duplicates)

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

Copyright © 2026 · Genesis Framework · WordPress · Log in