Meta interview question

Find the intersection between 2 NSRanges

Interview Answers

Anonymous

4 Nov 2017

Did you manage to finish the question. How many questions did he gave you ?

Anonymous

10 June 2018

func findIntersection(range1: NSRange, range2: NSRange) -> NSRange { let intersection = NSIntersectionRange(range1, range2) return intersection }

Anonymous

13 Apr 2020

extension NSRange { func my_intersection(with range: NSRange) -> NSRange? { guard range.lowerBound <= self.upperBound || self.lowerBound <= range.upperBound else { return nil } let start = max(self.lowerBound, range.lowerBound) let end = min(self.upperBound, range.upperBound) return NSRange(location: start, length: end - start) } }