I am listing down some of the repetitive interview questions I have been facing these days. I don't know how I was evaluated by answering these questions. But somehow these are the messages from the gods.
My Answer:
NSManagedObject is the representation of Core Data Entity. Which holds attributes and relationships.
Expected Answer
A managed object has an associated entity description (
NSEntityDescription
) that provides metadata about the object, including the name of the entity that the object represents and the names of its attributes and relationships. A managed object also has an associated managed object context that tracks changes to the object graph.
You can’t use instances of direct subclasses of
NSObject
, or any other class that doesn’t inherit fromNSManagedObject
, with a managed object context. You may create custom subclasses ofNSManagedObject
, although this isn’t always necessary. If you don’t need custom logic, you can create a complete object graph withNSManagedObject
instances.
If you instantiate a managed object directly, you must call the designated initializer
init(entity:insertInto:)
.
Read the documentation here
6. How to make three different network calls and how to make forth network call upon completion of the first three calls?
While answering I asked the interviewer: Should the first three network calls to be serial or concurrent, Interviewer replied "anything is fine" I started answering while coding it. We can use Tasks/ TaskGroup/ DispatchGroup Given the example with Tasks & DispatchGroup
Approach 1
func download(file name: String) async throws -> String {
try await Task.sleep(nanoseconds: 100_000)
print(name)
return name
}
func test() {
Task {
async let task1 = download(file: "File 1")
async let task2 = download(file: "File 2")
async let task3 = download(file: "File 3")
_ = try? await [task1, task2, task3]
_ = try? await download(file: "File 4")
}
}
test()
Approach 2
let group = DispatchGroup()
group.enter()
serviceCall(num: 10) { data in
print(data)
group.leave()
}
group.enter()
serviceCall(num: 11) { data in
print(data)
group.leave()
}
group.enter()
serviceCall(num: 12) { data in
print(data)
group.leave()
}
group.notify(queue: DispatchQueue.main) {
serviceCall(num: 13) { data in
print(data)
group.leave()
}
}
func serviceCall(num: Int, success: @escaping((String) -> Void)) {
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(2), execute: {
success("Call \(num)")
})
}
I didn't answer properly
One best way is Write the service class in testable manner We need to create MockProtocol conforming to URLProtocol Impletement startLoading method im MockProtocol to verify the URL and trigger custom requestHandler
Please refer this example
9. When two buttons are placed in a view one overlaps the other, how to prevent the touch of the button underneath?
My lazy answer:
Extensions are used
- in
protocol
s to give default implementation to the methods in them.- in
struct
orclass
to write convenience initializers.
I thought my interviewer would accept that I know them and I used them.
My lazy answer:
atomic and nonatomic are used to mark the properties in Objective-C classes. atomic is the static and nonatomic is the dynamic. atomic being thread safe.
I thought my interviewer would accept that I know them and I used them.
My answer:
weak and unowned are used to mark a reference typed stored property in a class to avoid strong referencing. If class A holds a reference of class B and vice versa, in such cases both the objects would never be deallocated even though the scope is closed as they are holding strong references each other. To avoid the memory leakage, we can mark the reference variables as either weak or unowned. weak is used in cases of the property is optional and unowned can be used in both non-optional and optional variable.
weak and unowned should be carefully chosen. If you are certain that both the references can be deallocated at the same time.
I don't know the ideal answer.
My answer:
@escaping
make the closure to hold it's refernce in the memory so that an asynchronous call still call the closure even after the enclosed method completes its return.
Example I gave that the network calls with completion handler would require an
@escaping
closure.
May be the interviewer was expecting hear the differences of
@escaping
andnonescaping
closures. He might have expected me to say that the closures in the function parameters are nonescaping by default. May be he was wanting to know that I know that there used to be@escaping
closures by default until Swift 3. Now it is reversed.
I don't know, may be he is looking for Swiftclopedia.
let name = "Jagan Kumar Mudila"
print(String(name.reversed()))