Skip to content

Instantly share code, notes, and snippets.

@SwiftArchitect
Last active February 25, 2018 00:25
Show Gist options
  • Save SwiftArchitect/38c0dc7c6890091f0434290d717ef2a7 to your computer and use it in GitHub Desktop.
Save SwiftArchitect/38c0dc7c6890091f0434290d717ef2a7 to your computer and use it in GitHub Desktop.
Swift Xcode implementation of InterviewCake 'HiCal' calendar event merging using immutable Meeting objects. It also doesn't start by making a copy of the entire Meetings array, saving one linear iteration of O(n). ⚠️ Spoiler alert: this is an actual answer to the https://www.interviewcake.com/question/swift/merging-ranges question, so no peeking!
//
// Meeting.swift
//
import Foundation
class Meeting: CustomStringConvertible {
private(set) var startTime: Int
private(set) var endTime: Int
init(startTime: Int, endTime: Int) {
// number of 30 min blocks past 9:00 am
self.startTime = startTime
assert(endTime >= startTime) // Ensure that Meeting records are coherent
self.endTime = max(startTime, endTime)
}
var description: String {
return "(\(startTime), \(endTime))"
}
}
extension Meeting: Equatable {
static func ==(lhs: Meeting, rhs: Meeting) -> Bool {
return lhs.startTime == rhs.startTime && lhs.endTime == rhs.endTime
}
}
//
// MergeMeetingRange.swift
//
// Copyright © 2018 Xavier Schott
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
class MergeMeetingRange {
static func mergeRanges(_ source: [Meeting]) -> [Meeting] {
var result = [Meeting]()
if !source.isEmpty {
let byStartingTime = source.sorted() { (lhs, rhs) -> Bool in
return lhs.startTime <= rhs.startTime
}
var lastStartTime = byStartingTime[0].startTime
var lastEndTime = byStartingTime[0].endTime
var waitingForGap = true
byStartingTime.forEach { (meeting) in
if meeting.startTime > lastEndTime {
// gap
result.append(Meeting(startTime: lastStartTime, endTime: lastEndTime))
lastStartTime = meeting.startTime
lastEndTime = meeting.endTime
waitingForGap = false
} else {
// no gap
lastEndTime = max(lastEndTime, meeting.endTime)
waitingForGap = true
}
}
if waitingForGap {
result.append(Meeting(startTime: lastStartTime, endTime: lastEndTime))
}
}
return result
}
}
//
// MergeMeetingRangeTests.swift
//
// Copyright © 2018 Xavier Schott
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import XCTest
class MergeMeetingRangeTests: XCTestCase {
func testMergeMeetingRangeEmpty() {
let test = [Meeting]()
let expected = [Meeting]()
let result = MergeMeetingRange.mergeRanges(test)
XCTAssertEqual(expected, result)
}
func testMergeMeetingRangeSingleEntry() {
let test = [
Meeting(startTime: 7, endTime: 9)
]
let expected = [
Meeting(startTime: 7, endTime: 9)
]
let result = MergeMeetingRange.mergeRanges(test)
XCTAssertEqual(expected, result)
}
func testMergeMeetingRangeFromQuestion() {
let test = [
Meeting(startTime: 0, endTime: 1), Meeting(startTime: 3, endTime: 5),
Meeting(startTime: 4, endTime: 8), Meeting(startTime: 10, endTime: 12),
Meeting(startTime: 9, endTime: 10)
]
let expected = [
Meeting(startTime: 0, endTime: 1),
Meeting(startTime: 3, endTime: 8),
Meeting(startTime: 9, endTime: 12)
]
let result = MergeMeetingRange.mergeRanges(test)
XCTAssertEqual(expected, result)
}
func testMergeMeetingRangeFromAnswer() {
let test = [
Meeting(startTime: 1, endTime: 10),
Meeting(startTime: 2, endTime: 6),
Meeting(startTime: 3, endTime: 5),
Meeting(startTime: 7, endTime: 9)
]
let expected = [
Meeting(startTime: 1, endTime: 10)
]
let result = MergeMeetingRange.mergeRanges(test)
XCTAssertEqual(expected, result)
}
func testMergeMeetingRangeWithMixedValues() {
let test = [
Meeting(startTime: 0, endTime: 3), Meeting(startTime: 3, endTime: 5),
Meeting(startTime: 4, endTime: 8), Meeting(startTime: 10, endTime: 12),
Meeting(startTime: 9, endTime: 10), Meeting(startTime: 0, endTime: 1),
Meeting(startTime: 1, endTime: 2),
Meeting(startTime: 20, endTime: 21), Meeting(startTime: 21, endTime: 22)
]
let expected = [
Meeting(startTime: 0, endTime: 8),
Meeting(startTime: 9, endTime: 12),
Meeting(startTime: 20, endTime: 22)
]
let result = MergeMeetingRange.mergeRanges(test)
XCTAssertEqual(expected, result)
}
}
@SwiftArchitect
Copy link
Author

This implementation of mergeRanges allows for the Meeting class to be immutable. It's source was modified at

  • private(set) var startTime: Int
  • private(set) var endTime: Int

which wasn't a requirement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment