iOS Swift Development: SEAN ALLEN INTERVIEW QUESTIONS

iOS Swift Development: SEAN ALLEN INTERVIEW QUESTIONS

memorize.aimemorize.ai (lvl 286)
Section 1

Preview this deck

Swift Error Handling - Do, Try, Catch - iOS Interview Question Series https://www.youtube.com/watch?v=Lrc-MX8WgNc

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (15)

Section 1

(15 cards)

Swift Error Handling - Do, Try, Catch - iOS Interview Question Series https://www.youtube.com/watch?v=Lrc-MX8WgNc

Front

Back

Binary Search - Swift Tutorial - iOS Interview Coding Challenge https://www.youtube.com/watch?v=CkYDljd_7M8

Front

Essentially keeps chopping array in two, to find the number you are looking for in a sorted array. [1,2,3,4,5,6,7,8,9,10] If number is 9. We look at the halfway number and if it is smaller than the number. Remove all lower number values in the array. [6,7,8,9,10] Now Repeat [8,9,10] [9, 10] 9 How to code this up: let numbers = [1,4,6,7,10,12,18,19,23,24,34,44,58,65] func binarySearch(array: [Int], key: Int) -> Bool { if array.count == 0 {return false} let minIndex = 0 let maxIndex = array.count - 1 let midIndex = maxIndex/2 let midValue = array[midIndex] if key > midValue { let slice = Array(array[midIndex + 1...maxIndex]) return binarySearch(array: slice, key: key) } if key < midValue { let slice = Array(array[midIndex...midIndex - 1]) return binarySearch(array: slice, key: key) } if key == midValue { print("\(key) found in the array") return true } return false } binarySearch(array: numbers, key: 23)

Back

Swift Observer & Notification Pattern Tutorial - iOS Communication Patterns Part 2 https://www.youtube.com/watch?v=srqiDnLEocA

Front

Back

Swift Optionals Tutorial - Unwrapping - Guard, If Let, Chaining, Force https://www.youtube.com/watch?v=ZL8BFK8bVjk

Front

What is an optional? var optionalNumber: Int? Allow you to write flexible more safe code. It could be nil or a number. Program won't crash if it is nil. In order to get to an optionals value, you have to unwrap it. Here are ways to unwrap it: var optionalNumber: Int? optionalNumber = 23 //IF LET if let number = optionalNumber { print("Value is \(number)") } else { print("no value") //GUARD - used with functions, provides early exit should something be nil func tripleNumber(number: Int?) { guard let notNillNumber = number else { print("exiting function") return } print("tripled number is \(notNillNumber *3)") } tripleNumber(number: optionalNumber) //FORCE UNWRAPPING - dangerous/last resort/1000% sure that value will be there let forcedNumber = optionalNumber! //OPTIONAL CHANGING - only using this on an object that has properties struct Device { var type: String var price: Float var color: String } var myPhone: Device? myPhone = Device(type: "Phone", price: 690.0, color: "Space Grey") let devicePrice = myPhone?.price if let devicePrice = devicePrice { print("My total price = \(devicePrice + 8.99)") }

Back

Swift Gesture Recognizer Tutorial - Pan https://www.youtube.com/watch?v=rnJxpuPyLNA

Front

Have user interaction enabled on the image for gesture recognizer to work

Back

iOS Interview Questions and Answers 2017 - Swift - Series Overview https://www.youtube.com/watch?v=56ZO6Gg68tw

Front

FIRST QUESTION: Automatic Reference Counting Keeps track of strong references to an object and when that count gets to zero it will automatically deallocate it from memory for you. Retain Cycles/Memory Leaks If you have a retain cycle or two objects have a strong reference to each other its kind of a never=ending loop and that will never get to zero Trick is to make one of those references a weak reference "weak var" Apple Automatic Reference Counting Doc: https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html Follow up questions to memory in closure, weak self unknown self come into play SECOND QUESTION: Communication Patterns between views: Delegates vs Observers and Notifications Delegates is a one to one communication. Like one view communicating with another view Observers Notifications you can have one observer and then have ten different areas that notify that observer throughout your code, so it's a one to many communication pattern. When to use: Downsides to Observer Notification pattern is you can have 10 to 20 notifications spread out all throughout your code only pointing to one observer. In a bigger code base there can be timing issues and hard to keep track of. What is the lifecycle overview? viewdidLoad - gets called the very first time the view is loaded into memory and only gets called once viewWillAppear - gets called every time the view appears, an example of this is say you're navigation controller a master detail view and you keep going back and forth between those two views, viewDidLoad will not get called but viewWillAppear does. If you need an animation to occur every time you arrive upon page, use viewWillAppear viewWillLayoutSubviews/viewDidSubLayoutSubviews - these two are where the view is actually laying out all the sub views, the constraints, the sizing, and everything viewDidAppear- which means the view is completely loaded End of LifeCycle viewWillDisappear - you can do stuff right before the view dissapears viewDidDisappear - to let you know the view is gone THIRD QUESTION: What is your favorite Apple framework to work with? Do you like core location, do you like UIKit, like MapKit and if you do, why you like those API? FOURTH QUESTION: Classes vs Structs? When would you use one versus the other? Explain that classes are a reference type, that if you change a property on that class you are actually going to change the reference whereas a struct is a value type that essentially creates a copy of that objects are not overwriting other properties If you subclass, you are inheriting the superclass methods, variables, properties, etc, which is some bloat that you don't necessarily need. Structs are more lightweight and clean FIFTH QUESTION: Filter, map, and reduce on collections Filter - Filter an array, you want to filter out all the even or odd numbers in an array Map - is like you want to transform to every object in the array. If you want to multiply an array of numbers by multiplying by 2 to each index Reduce - summing up the numbers up in an array SIXTH QUESTION: Testing - do you have experience with testing? Learn to build testing suites in projects SEVENTH QUESTION: 3RD PARTY LIBRARIES: What experience you have with 3rd party libraries like cocoa pods or Carthage or Swift package manager. So get experience using this, and the pros and cons of using them and knowing how to use them. At the end of the day, that's what a dependency is, you are depending on somebody's code in your project. So in a language like swift you got to hope the manager of that library is keeping up to date with swift. CODING EXCERCISES: 1. Gesture recognizers - the tap gesture recognizer, the pane gesture recognizer, the pinch and zoom. For example: grabbing a file and dragging it to a trash bin, being able to manipulate objects on the screen using a gesture recognizer, look for tutorials that teach how to use gesture recognizers in a UI 2. Networking - Authenticate on a server, they should give all the API information, the endpoints needed, but essentially you will have to build a URL the proper headers with authorization and make sure the body of the request was complete with the username password etc and had to authenticate on the server and return a token. This is very fundamental 3. Merge Sort/ Shuffle an Array 4. Debugging. They will put code in front of you have to debug it just by looking at it. You have to find the errors yourself. The usual suspects. look for optionals being forced and wrapped inappropriately, look for race conditions in the network calls, look for retained cycles, memory leaks, and look for UI not being updated on the main thread 5. Take a large unit and break it down into small units. Take 100,000 seconds and break that down into days hours minutes seconds. Use the modulu operator 6. Know how to iterate through for loops / while loops 7. Know data structures and algorithms TAKE HOME PROJECTS!: 1. Create a mini version of their app, networking/building a tableView to building the UI to a design spec, persistence, animation 2. Build a basic login flow but it was a 100 percent programmatic, no storyboards, no nothing for auto layout

Back

Swift - Retain Cycle, Automatic Reference Counting, Memory Leak - iOS Interview Questions https://www.youtube.com/watch?v=VcoZJ88d-vM

Front

For each object, it keeps a count of how many strong references are pointing to that object. For example, say you have a person class called "Sean" There's also a camera, phone, and a MacBook class all having a strong reference pointing towards Sean. So automatic reference counting is going to be 4. Now if you try to make Sean = nil, to get rid of Sean from memory, automatic referencing won't allow it, Sean won't be released from memory because there are 3 other objects pointing back. To fix that, you need to make those strong references weak references. Example: using a Person and MacBook class class Person { let name: String var macbook: MacBook? init(name: String, macbook: MacBook?) { self.name = name self.macbook = macbook } deinit { print("\(name) is being deinitialized") } } class MackBook { let name: String var owner: Person? init(name: String, owner: Person?) { self.name = name self.owner - owner } deinit { print("MacBook named \(name) is being deinitialized") } } var sean: Person? var Matilda: MacBook? func createObjects() { sean = Person(name: "Sean", macbook: nil) matilda = MacBook(name: "Matilda", owner: nil) //to activate de-init method, set to nil //Sean = nil //Matilda = nil } //creating strong references func assignProps() { Sean?.macbook = Matilda matilda?.owner = Sean //Now if you make Sean = nil, you will get an error. There is still one reference left out of the two //The fix for this is to make one of the variables weak //in the MacBook class you set: weak var owner: Person? }

Back

Swift Delegate Protocol Pattern Tutorial - iOS Communication Patterns Part 1 https://www.youtube.com/watch?v=DBWu6TnhLeY

Front

Back

Filter, Map, Reduce - Swift - iOS Interview Questions https://www.youtube.com/watch?v=OujkoTKEVj8

Front

var myiMacPro = Device(type: "iMac Pro", price: 4999.00, color: "space grey") var myiPhone6Plus = Device(type: "iPhone", price: 799.00, color: "white") var myiPhone7 = Device(type: "iPhone", price: 699.00, color: "Black") let myDevices = [myiMacPro, myiPhone6Plus, myiPhone7] //FILTER let iPhones = myDevices.filter({return $0.type == "iPhone"}) //MAP let canadianPrices: [Float] = myDevices.map({return $0.price * 1.2}) //REDUCE let totalCanadianPrice: Float = canadianPrices.reduce(0.0, +) or let totalCanadianPrice: Float = canadianPrices.reduce(20000, -) No longer needing to use for loops

Back

Merge Sort - Swift Tutorial - iOS interview Coding Challenge https://www.youtube.com/watch?v=DfO089qWEE8

Front

[7,3,1,8,5,4,2,6] [7][3][1][8][5][4][2][6] [7, 3] | [1, 8] | [5, 4] | [2, 6] [3,7] [1,8] | [4,5] [2,6] Compare left to left, than compare right to right [1, 3, 7, 8] | [2, 4, 5, 6] [1,2,3,4,5,6,7,8] //split arrays func mergeSort(array: [Int]) -> [Int] { guard array.count > 1 else { return array } let leftArray = Array(array[0..<array.count/2]) let rightArray = Array(array.count/2..<array.count]) return merge(left: mergeSort(array: leftArray), right: mergeSort(array: rightArray)) } //Merge Function func merge(left: [Int], right: [Int]) -> [Int] { var mergedArray: [Int] = [] var left = left var right = right while left.count > - && right.count > 0 { if left.first! < right.first! { mergedArray.append(left.removeFirst()) } else { mergedArray.append(right.removeFirst()) } } return mergedArray + left + right } var numbers: [Int] = [] for _ in 0..<50 { let randomInt = Int(arc4random_uniform(UInt32(1000))) numbers.append(randomInt) } print(numbers) print() print(mergeSort(arrays: numbers))

Back

Two-Sum Problem - Swift Tutorial - iOS Interview Coding Challenge https://www.youtube.com/watch?v=cUmGngurKXo

Front

3 ways to solving the problem: 1. Brute Force - Nested For-Loop compare all (n2) func bruteForceTwoSum(array: [Int], sum: Int) -> Bool { for i in 0..<array.count { for j in 0..<array.count where j !=1 { if array[j] + array[I] == sum { print("True: \(array[I]) + \(array[j])") return true } else { print("False: \(array[I]) + \(array[j])") } } } return false } bruteForceTwoSum(array: numbers, sum: 23) 2. Binary Search for compliment (because it's sorted) - n log n func twoSumBinarySearch(array: [Int], sum: Int) -> Bool { if array.count == 0 { return false } for I in 0..<array.count { let compliment = sum - array[I] tempArray.remove(at: i) let hasCompliment = binarySearch(array: tempArray, key: compliment) print("num: \(array[I]) - \(hasCompliment)") if hasCompliment == true { print("true - num: \(array[I]), compliment: \(compliment)") return true } } return false } twoSumBinarySearch(array: numbers2, sum: 26) 3. Move pointer from either end - linear func twoSumPointers(array: [Int], sum: Int) -> Bool { var lowIndex = 0 var highIndex = array.count - 1 while lowIndex < highIndex { let sumOfItems = array[lowIndex] + array[highIndex] if sumOfItems == sum { print("Sum of \(array[lowIndex]) and \(array[highIndex])) = \(sum)") return true }. else if sumOfItems < sum { lowIndex += 1 } else if sumOfItems > sum { highIndex -= 1 } } print("Pointers have crossed") return false } twoSumPointers(array:numbers3, sum: 30)

Back

Swift - Bounds vs. Frame - iOS Interview Question https://www.youtube.com/watch?v=Nfzy1qgxSAg

Front

1. What is the difference between bounds and frames? Let's say using a UIView, the frame is comprised of its position relative to its super view (the white background) and the UIView's height and width. Hold option to see dimensions The bounds are its position relative to its own coordinates system and not it's parents view. So in a UIView the top left border is (0,0) The frame and bounds width and height are the same when parallel. But if you were to rotate the UIView 55 degrees, then the frame's position and width and height changes, but the bound dimensions of width and height stay the same

Back

iOS Concurrency and Threading - iOS Interview Question - Swift https://www.youtube.com/watch?v=iTcq6L-PaDQ

Front

What is concurrency? Concurrency is doing multiple tasks at the same time (more Cores you have the tasks you can do at the same time) Think of threads as highway lanes, and car is each task being executed Think of the highway fast lane as the "Main Thread", and you want to keep the "Main Thread" speedy and clean, not filled with traffic To keep track of all the threads, apple has created Gran Central Dispatch & NSOperation Queues. Essentially an API built on top of these threadings What is a queue? Like lining up for a movie, the first in the line, is the first to go in. "First In First Out" Two types of Queue: Serial: Task in Order, 1 by 1 Concurrent: Tasks come in 1 at a time but don't have to wait for the task ahead of it to finish. Also tasks can finish faster than other tasks, order of completion is unpredictable In Serial Queue Pros: -Predictable -Prevents race conditions Concurrent Queue Pros: -Faster -Unpredictable Order For a given app, you are given 1 Main Queue and 4 Concurrent Queue How to switch between queues: DispatchQueue.main.async { self.tableView.reloadData() }

Back

Most Common Element in Array - iOS Interview Coding Challenge White Board Question https://www.youtube.com/watch?v=0eWv1mAewx4

Front

In an array of colors, which color appears the most? Create a function that constructs a dictionary that returns an array of string func getMostCommonColor (array: [String]) -> [Strings] { var topColors: [String] = [] var colorDictionary: [String: Int] = [:] for color in array { if let count = colorDictionary[color] { colorDictionary[color] = count + 1 } else { colorDictionary[color] = 1 } } let highestValue = colorDictionary.values.max() for (color, count) in colorDictionary { if colorDictionary[color] == highestValue { topColors.append(color) } return topColors }

Back

Swift Classes and Structures Explained https://www.youtube.com/watch?v=ObIxxHy8yY8

Front

Classes are references types - passed by reference Structs are value types - passed by copy

Back