• 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

Skiing on Mountains Matrix

Find Nearest Minimum number in left side in O(n)

Amazon Interview Experience – SDE Chennai

Check Binary Tree is Binary Search Tree or not

Possible sizes of bus to carry n groups of friends

FizzBuzz Solution C C++

Print Power Set of a Set

Print all nodes that are at distance k from a leaf node

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

Count Possible Decodings of a given Digit Sequence

Number of Islands BFS/DFS

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Level order traversal in Spiral form

Fibonacci Hashing & Fastest Hashtable

Generate largest number arranging a no. of given non negative integer numbers

BFS (Breath First Search)

Test Cases for Round Function

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Doubly linked list

Flipkart Set 1 On Campus with Answers

Best Java Book | Top Java Programming Book for Beginners

Reversal of LinkedList

Hackerearth : Counting Subarrays

Sort an array according to the order defined by another array

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

Printing intermediate Integers between one element & next element of array

Stickler thief

C Program for TAIL command of UNIX

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

Leetcode: Edit Distance

Copyright © 2023 · Genesis Framework · WordPress · Log in