Last active
February 8, 2021 20:37
-
-
Save davidseek/6e02b6bea55bed9014397df205745db5 to your computer and use it in GitHub Desktop.
hotelWithMethods.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Again, same class as before. | |
class Hotel { | |
// We now have the returned `roomID` optional. | |
// It's possible, that there are no rooms available anymore, | |
// so we need to make nil a valid option. | |
func checkIn(_ dog: Dog) -> String? { | |
// We want to return a roomID so each user | |
// is able to retrieve their dog using the ID. | |
var roomID: String? | |
// We check for the dog size and we check if | |
// the desired room size is available | |
if dog.size == .small, smallRooms.keys.count < smallRoomsCount { | |
// Only then do we generate a PIN. | |
// To safe memory. Mention that to your interviewer. | |
// That's part of "scalablity". | |
roomID = UUID() | |
// We use the ID to assign a room for the dog. | |
// Of course there are other ways to do this. | |
// But this is an easy way and it makes sense. | |
// No reason to overcomplicate it in an interview. | |
smallRooms[roomID] = dog | |
} | |
// Same for medium as we did for small | |
if dog.size == .medium, mediumRooms.keys.count < mediumRoomsCount { | |
roomID = UUID() | |
mediumRooms[roomID] = dog | |
} | |
// Same for large as we did for small and medium | |
if dog.size == .large, largeRooms.keys.count < largeRoomsCount { | |
roomID = UUID() | |
largeRooms[roomID] = dog | |
} | |
// Lastly we return the ID that can be nil | |
// in case of no availability. | |
return roomID | |
} | |
// For the checkOut we've created a more swifty syntax. | |
// That shows that we care about writing well readable code. | |
// We now require the dogID and the roomID to check out. | |
// That makes more sense. | |
// If the IDs are invalid, then the return is nil. | |
func checkOut(dogWithID dogID: String, outOfRoom roomID: String) -> Dog? { | |
// To not overcomplicate it, we'll just check if | |
// there's a dog in the room, | |
// and then if the dogIDs are matching | |
// | |
// It's possible, that your interviewer asks you | |
// about concerns regarding efficiency of this approach. | |
// Let them know, that the Big O cost is constant | |
// and very efficient, even if checked in 3 dictionaries. | |
if let dog = smallRooms[roomID], dog.uid == dogID { | |
// If so, it's safe to return the dog | |
return dog | |
} | |
// Same then for medium rooms. | |
if let dog = mediumRooms[roomID], dog.uid == dogID { | |
return dog | |
} | |
// And for large rooms. | |
if let dog = largeRooms[roomID], dog.uid == dogID { | |
return dog | |
} | |
// If there's no matches in the IDs, | |
// then we're not gonna return any pups. | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment