🧠
Using 1D Interval Overlap Helper Function
💡 This approach introduces a helper function to check 1D overlap, improving code clarity and reusability, which is a good practice in interviews.
Intuition
Since rectangle overlap depends on overlap in both x and y intervals, we can write a function to check if two intervals overlap and reuse it for both axes.
Algorithm
- Define a helper function that returns true if two 1D intervals overlap.
- Use this helper to check if the x intervals of the rectangles overlap.
- Use this helper to check if the y intervals of the rectangles overlap.
- Return true only if both x and y intervals overlap.
💡 Breaking the problem into smaller reusable parts makes the solution easier to understand and maintain.
def intervals_overlap(a_start, a_end, b_start, b_end):
return a_start < b_end and b_start < a_end
def isRectangleOverlap(rec1, rec2):
return intervals_overlap(rec1[0], rec1[2], rec2[0], rec2[2]) and intervals_overlap(rec1[1], rec1[3], rec2[1], rec2[3])
# Driver code
if __name__ == '__main__':
print(isRectangleOverlap([0,0,2,2], [1,1,3,3])) # True
print(isRectangleOverlap([0,0,1,1], [1,0,2,1])) # False
Line Notes
def intervals_overlap(a_start, a_end, b_start, b_end):Defines a reusable function to check 1D interval overlap
return a_start < b_end and b_start < a_endChecks if intervals overlap by verifying start of one is before end of other
return intervals_overlap(rec1[0], rec1[2], rec2[0], rec2[2]) and intervals_overlap(rec1[1], rec1[3], rec2[1], rec2[3])Checks overlap on both x and y axes using helper
print(isRectangleOverlap([0,0,2,2], [1,1,3,3]))Tests the function with overlapping rectangles
public class RectangleOverlap {
private static boolean intervalsOverlap(int aStart, int aEnd, int bStart, int bEnd) {
return aStart < bEnd && bStart < aEnd;
}
public static boolean isRectangleOverlap(int[] rec1, int[] rec2) {
return intervalsOverlap(rec1[0], rec1[2], rec2[0], rec2[2]) &&
intervalsOverlap(rec1[1], rec1[3], rec2[1], rec2[3]);
}
public static void main(String[] args) {
System.out.println(isRectangleOverlap(new int[]{0,0,2,2}, new int[]{1,1,3,3})); // true
System.out.println(isRectangleOverlap(new int[]{0,0,1,1}, new int[]{1,0,2,1})); // false
}
}
Line Notes
private static boolean intervalsOverlap(int aStart, int aEnd, int bStart, int bEnd) {Helper method to check if two intervals overlap
return aStart < bEnd && bStart < aEnd;Overlap exists if intervals intersect in any positive length
return intervalsOverlap(rec1[0], rec1[2], rec2[0], rec2[2]) && intervalsOverlap(rec1[1], rec1[3], rec2[1], rec2[3]);Checks overlap on both axes
System.out.println(isRectangleOverlap(...));Runs test cases to verify correctness
#include <iostream>
#include <vector>
using namespace std;
bool intervalsOverlap(int aStart, int aEnd, int bStart, int bEnd) {
return aStart < bEnd && bStart < aEnd;
}
bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
return intervalsOverlap(rec1[0], rec1[2], rec2[0], rec2[2]) &&
intervalsOverlap(rec1[1], rec1[3], rec2[1], rec2[3]);
}
int main() {
vector<int> rec1 = {0,0,2,2};
vector<int> rec2 = {1,1,3,3};
cout << boolalpha << isRectangleOverlap(rec1, rec2) << endl; // true
rec1 = {0,0,1,1};
rec2 = {1,0,2,1};
cout << boolalpha << isRectangleOverlap(rec1, rec2) << endl; // false
return 0;
}
Line Notes
bool intervalsOverlap(int aStart, int aEnd, int bStart, int bEnd) {Helper function to check 1D interval overlap
return aStart < bEnd && bStart < aEnd;Returns true if intervals intersect with positive length
return intervalsOverlap(rec1[0], rec1[2], rec2[0], rec2[2]) && intervalsOverlap(rec1[1], rec1[3], rec2[1], rec2[3]);Checks overlap on both x and y axes
cout << boolalpha << isRectangleOverlap(rec1, rec2) << endl;Prints boolean result for test cases
function intervalsOverlap(aStart, aEnd, bStart, bEnd) {
return aStart < bEnd && bStart < aEnd;
}
function isRectangleOverlap(rec1, rec2) {
return intervalsOverlap(rec1[0], rec1[2], rec2[0], rec2[2]) &&
intervalsOverlap(rec1[1], rec1[3], rec2[1], rec2[3]);
}
// Test cases
console.log(isRectangleOverlap([0,0,2,2], [1,1,3,3])); // true
console.log(isRectangleOverlap([0,0,1,1], [1,0,2,1])); // false
Line Notes
function intervalsOverlap(aStart, aEnd, bStart, bEnd) {Defines helper to check 1D interval overlap
return aStart < bEnd && bStart < aEnd;Returns true if intervals overlap positively
return intervalsOverlap(rec1[0], rec1[2], rec2[0], rec2[2]) && intervalsOverlap(rec1[1], rec1[3], rec2[1], rec2[3]);Checks overlap on both axes
console.log(isRectangleOverlap(...));Runs test cases to verify correctness
Only constant time checks and function calls are made.
💡 This approach is just as fast as brute force but better organized.
Interview Verdict: Accepted
This approach is clean and efficient, suitable for interviews.