# (SE-3) 2023 iOS Interview Questions
### Warm up questions:
1. What are the various ways to unwrap an optional in swift ***(Steve)***
:::spoiler Reveal Answer
* Guard statement: safe.
* if let block
* Forced unwrapping: using “!” operator, unsafe.
* Optional binding: safe.
* Nil coalescing operator: safe.
* Optional chaining: safe.
* Implicitly unwrapped variable declaration: unsafe in many cases.
:::
\
2. What is a GUARD statement and what are a couple examples of where you would use them? ***(Komran)*** [Skip whenever they mention guard] [Agree]
:::spoiler Reveal Answer
Definition - A guard statement is used to transfer program control out of a scope if one or more conditions aren’t met.
**Examples:**
1. Safe Unwrapping optionals
2. If condition fails you can exit the program
:::
\
3. Define Strong, weak and unowned. What causes memory leaks? (Bill)
:::spoiler Reveal Answer
Strong- default reference, obj won't be deallocated as long as reference exists. Memory leaks can be caused if two objects strongly reference each other
Weak- Does not increase reference count, becomes nil if deallocated
Unowned - assumes reference obj will never be nil, if deallocated, will cause runtime crash (typically safe for relational types)
:::
\
4. What is a Protocol in swift? (Komran)
:::spoiler Reveal Answer
A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.
In addition to specifying requirements that conforming types must implement, you can extend a protocol to implement some of these requirements or to implement additional functionality that conforming types can take advantage of.
Positive signal:
- extensions
- protocol oriented programming
- associated types
- practical examples
:::
\
5. Explain extensions? [Skip if they talk extensions in #4] (Stephen)
:::spoiler Reveal Answer
Positive signal:
- protocols
- code organization
- generics for conditional extensions
- default implementations
:::
\
6. ACL with example, internal/private/public when to use what? (Bill)
:::spoiler Reveal Answer
- private: limited to what it is enclosed in, fileprivate limits to same file
- internal: same module
- public: cross module
:::
### SwiftUI
1. Does the order of swiftui modifiers matter? Why? (Stephen)
:::spoiler Reveal Answer
Yes, each modifier updates the view from the state of the last modifier
:::
\
2. What is the @Viewbuilder parameter attribute, when do we use it? (Komran)
:::spoiler Reveal Answer
Lets you build a view from multiple expressions ex. 'some View' is using ViewBuilder
:::
\
3. List vs scrollview difference? when to use what? (Bill)
:::spoiler Reveal Answer
Lists are more for structured data and are optimized for dealing with large data sets. There is some default UI/UX behavior associated with it (row styling, separators). Scrollview is more free-form with arbitrary content; is not lazy loaded so is less performant. Best to use the former for things like a list of store data, for the latter, customized layouts mixining text/images is an unstructured way
:::
\
4. When should you use LazyVStack and lazyHStack? (Stephen)
:::spoiler Reveal Answer
- Large collection of views you don't want to render right away that are not visible
:::
\
5. When would you use GeometryReader? (Komran)
:::spoiler Reveal Answer
GeometryReader allows us to read the size and location of a view, which means we can create proportional layouts or create adaptive modifiers that change their values as a view moves around the screen.
:::
\
~~6. What is UIViewRepresentable? Difference between UIViewRepresentable and UIViewController?~~
\
7. What are the different property wrappers you have used? (Bill)
* **Notes**: Give `@State` as an example to start answering process from candidates.
* What are the other property wrapper's available?
* Binding property wrapper's? how it works?
* Can we create our own property wrapper?
:::spoiler Reveal Answer
Some of the more relevant ones:
@State / @StateObject - simple/complex owner of data
@Binding - correlates with @State, to use in dependant view
@ObservedObject - observe an ObservableObject
@Published - in the ObservableObject, marks props that will trigger an update when using @ObservedObject
@Environment - env values, read-only
@EnvironmentObject - globally accessible, read-write
@AppStorage - userdefaults interaction
@MainActor - force to run on main thread
Yes, you can create your own property wrapper, set up a struct and define how the property is stored and fetched. May want to do this to cut down repetition and improve readability
:::
\
8. **How does observable object anounces changes?** (Bill) [Skip if covered in #7] Bonus Question
\
9. Explain Combine framework? How can you subscribe to event using the combine framework in swiftUI? (Stephen)
* Async programming how are you using and managing?
* Note: ask for comfort level
* combine/GCD/async-await, rate for this framework out of 10.
* Would you prefer Swift concurrency model or Grand Central Dispatch and why?
:::spoiler Reveal Answer
* Completion handlers (run a function after completion of data fetch)
* DispatchQueue - manage execution of tasks on different threads (MainQueue - typically UI tasks, GlobalQueue - typically background tasks) - also have serial and concurrent queues (FIFO vs as-ready)
* DispatchGroups - helps you track when a collection of tasks are completed (enter/leave/notify)
* Bonus- Why use async await- easier to read so that you don't have to juggle completion handlers, reads synchronously
:::
Kom Extra:
- @MainActor
- Task
- withCheckedContinuation
- PreferenceKey
- `StateObject` vs `ObservableObject`
Steve Extra:
- Can you explain value vs reference types (with an example of each) and describe when you might use which?
- If you needed to set up deeplink navigation in an app, how might you handle that, what would you utilize?
- How would you mock dependencies in unit tests? (If this wasn't already covered in protocols)
## Swift
* What is Enum associated value and how would you access it? ***(Alex)***
* What are Generics in Swift? ***(Alex)***
## Misc
* How would you describe Runloop and when to use it? Bonus ***(Alex)***
## Algo
* Which is faster: for unique sorted items a search an Array or an Set? /5 🏏 X ***(Bill)*** ✅
* Higher order fucntions and When to use them - __/5 ***(Bill)***
## Design & Architecture
* Design patterns? ***(Bill)***
## Testing
* How do you test logic using or against data? ***(Bill)***
Kom Ideas:
- Debugging process