Skip to content

Instantly share code, notes, and snippets.

@SwiftArchitect
Created February 24, 2018 02:58
Show Gist options
  • Save SwiftArchitect/bf0d29459aeefc8552ee60d3ffa4810a to your computer and use it in GitHub Desktop.
Save SwiftArchitect/bf0d29459aeefc8552ee60d3ffa4810a to your computer and use it in GitHub Desktop.
Swift Xcode implementation of InterviewCake 'Temperature Tracker' with just ahead of time, making all methods perform in O(1). Mean is calculated when requested, still performing in O(1). ⚠️ Spoiler alert: this is an actual answer to the https://www.interviewcake.com/question/swift/temperature-tracker question, so no peeking!
//
// TemperatureTracker.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 TemperatureTracker
{
var low: Int?
var high: Int?
var mean: Double? {
return ((recordsCount > 0)
? Double(cumulativeTemperatures) / Double(recordsCount)
: nil)
}
var mode: Int?
private var occurences = [Int](repeatElement(0, count: 111))
private var cumulativeTemperatures = 0
private var recordsCount = 0
private var largestOccurencesCount = 0
func insert(value: Int) {
assert( value >= 0 && value <= 110)
if( value >= 0 && value <= 110) {
// Min & Max
low = min(low ?? Int.max, value)
high = max(high ?? Int.min, value)
// Mean
cumulativeTemperatures += value
recordsCount += 1
// Mode
occurences[value] += 1
if occurences[value] > largestOccurencesCount {
largestOccurencesCount = occurences[value]
mode = value
}
}
}
func getMin() -> Int? {
return low
}
func getMax() -> Int? {
return high
}
func getMean() -> Double? {
return mean
}
func getMode() -> Int? {
return mode
}
}
//
// TemperatureTrackerTests.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 TemperatureTrackerTests: XCTestCase {
func testTemperatureTracker() {
let temps = [2,4,7,5,9,6,3,6]
let tracker = TemperatureTracker()
temps.forEach { (temp) in
tracker.insert(value: temp)
}
let mean = (2.0 + 4.0 + 7.0 + 5.0 + 9.0 + 6.0 + 3.0 + 6.0) / 8.0
XCTAssertEqual(9, tracker.getMax())
XCTAssertEqual(2, tracker.getMin())
XCTAssertEqual(6, tracker.getMode())
XCTAssertEqual(mean, tracker.getMean())
}
func testPerformanceExample() {
self.measure {
let tracker = TemperatureTracker()
for _ in 0 ... 16384 {
tracker.insert(value: Int(arc4random_uniform(111)))
}
}
}
}
@SwiftArchitect
Copy link
Author

SwiftArchitect commented Feb 25, 2018

Every function performs in O(1).

mean is calculated just-in-time, but the necessary nominator and denominator are accumulated at very little cost ahead-of-time. Running 2^14 iterations in the Unit Test only shows a marginal improvement, well within the margin of errors.

As noted, the properties get aren't necessary, and only provided because they are part of the solution's API.

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