• 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

Implement LRU Cache

Wrong Directions given find minimum moves so that he can reach to the destination

How strtok() Works

Memory Efficient LinkedList

Check Binary Tree is Binary Search Tree or not

LeetCode : Word Search

Client Server C program

Maximum of all subarrays of size k

K’th Largest Element in BST when modification to BST is not allowed

Connect n ropes with minimum cost

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Find if a binary tree is height balanced ?

Right view of Binary tree

Common Ancestor in a Binary Tree or Binary Search Tree

Trapping Rain Water

Given a float number convert it into the string WITHOUT using any inbuilt Function

Amazon Interview On-Campus For Internship – 1

The Magic HackerEarth Nirvana solutions Hiring Challenge

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

Maximum path sum between two leaves

Binary Tree Isomorphic to each other

Python Array String

SAP Interview Questions

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Maximum size of square sub matrix with all 1’s in a binary matrix

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

Password Predictor

Generate next palindrome number

Knight Tour Problem (Graph – Breadth First Search)

Copyright © 2025 · Genesis Framework · WordPress · Log in