Skip to content

Instantly share code, notes, and snippets.

@NorrinRadd
Created July 14, 2014 20:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NorrinRadd/3a461fe7bc714757b4b6 to your computer and use it in GitHub Desktop.
Save NorrinRadd/3a461fe7bc714757b4b6 to your computer and use it in GitHub Desktop.
crazy interview questions
Q: What’s wrong with this code?
NSLock* arrayLock = GetArrayLock();
NSMutableArray* myArray = GetSharedArray();
id anObject;
[arrayLock lock];
anObject = [myArray objectAtIndex:0];
[arrayLock unlock];
[anObject doSomething];
A: Another thread might remove that object from the array after the lock is released but before doSomething is called. Fix by retaining inside the unlock, not moving doSomething inside the lock, because it could block.
Q: You have a stream of bytes in char array buf[]:
00 00 00 0A 68 65 6C 6C 6F 77 6F 72 6C 64
You know the bytes follow a protocol with this spec:
<1 byte> message type (0 == hello, 1 == goodbye)
<3 bytes> message length
<N bytes> message content (UTF-8 characters)
Determine if this is a hello message, and, if it is, print the content as a string to stdout.
A:
uint8_t message_type = buf[0];
if( message_type == 0 ){
uint32_t message_length = 0;
message_length |= buf[1] << 16;
message_length |= buf[2] << 8;
message_length |= buf[3];
char * message_content = malloc( message_length+1 );
memcpy( message_content, buf+4, message_length );
message_content[message_length] = '\0';
fprintf(stdout, "%s\n", message_content );
free(message_content);
}
Q: Write a program that prints the numbers from 1 to 100. But for multiples of three print "Multiple of 3" instead of the number and for the multiples of five print "Multiple of 5". For numbers which are multiples of both three and five print "Multiple of 3 and 5".
Q: Explain retain counts.
A: Retain counts are the way in which memory is managed in Objective-C. When you create an object, it has a retain count of 1. When you send an object a retain message, its retain count is incremented by 1. When you send an object a release message, its retain count is decremented by 1. When you send an object a autorelease message, its retain count is decremented by 1 at some stage in the future. If an object’s retain count is reduced to 0, it is deallocated.
Q: Can you explain what happens when you call autorelease on an object?
A: When you send an object a autorelease message, its retain count is decremented by 1 at some stage in the future. The object is added to an autorelease pool on the current thread. The main thread loop creates an autorelease pool at the beginning of the function, and release it at the end. This establishes a pool for the lifetime of the task. However, this also means that any autoreleased objects created during the lifetime of the task are not disposed of until the task completes. This may lead to the task’s memory footprint increasing unnecessarily. You can also consider creating pools with a narrower scope or use NSOperationQueue with it’s own autorelease pool. (Also important – You only release or autorelease objects you own.)
Q: After releasing an object in Obj-C, why should you set it to nil?
A: Calling a method on a nil object won't cause a crash.
Q: If you had to test if an NSDictionary contained a key, would you be better off getting an NSArray of allKeys from the dictionary and testing if the array contains the key, or asking the dictionary if it has an object for the key, and testing if it's nil?
A: The latter, because the dictionary lookup will be much faster.
[[dict allKeys] containsObject:key];
vs
([dict objectForKey:key] != nil)
Q: What's fast enumeration?
A: Fast enumeration is a language feature that allows you to enumerate over the contents of a collection. (Your code will also run faster because the internal implementation reduces message send overhead and increases pipelining potential.)
Q: How can you use fast enumeration to loop through the elements of a mutable array when you want to add or remove elements?
A: Make a copy of the array to loop through.
Q: What's the difference between copying an array using the "arrayWithArray" NSArray class method versus just using the existing NSArray instance's "copy" method?
A: Convenience methods like arrayWithArray have memory management handled by the system, copy has to be released by the user.
Q: Regarding languages, what is different about working with I/O Kit than writing a standard kernel extension?
A: I/O Kit uses C++
Q: What C++ features aren't supported in I/O Kit?
A: Exceptions, multiple inheritance, templates, runtime type information
Q: Why is debugging a kernel extension difficult? Why does it require two Macs?
A: Once the kext crashes, the Mac kernel panics and the interface goes down. But if you have another Mac connected to the panicking machine, it can run a remote GDB session to access the backtrace.
-----
Questions below from Daniel Pasco: http://blackpixel.com/blog/2013/04/interview-questions-for-ios-and-mac-developers-1.html
1. How do you handle asynchronous networking?
2. Have you ever worked with multi-threaded Core Data?
* How was it?
* What approach did you use?
3. Have you ever worked with Core Animation?
* Can you tell me about that?
* What kind of animations? (grace notes for an app versus canned table view insertion or bulk affine transforms)
4. Have you ever worked with Core Graphics? (answer for Mac devs should be a resounding "HELL YES I DID")
* What kinds of things did you do?
5. Have you ever filed any verified issues against Apple frameworks on radar?
* Can you describe them to me?
* Better still, can you point them out on open radar?
6. Compare and contrast NSNotification vs KVO.
* When would you use one or the other? What are the implications of using them?
7. Have you ever worked with NSOperationQueue?
* What did you use it for?
8. Please tell me about your Core Text experience.
9. How would you use NSURLConnection on a background thread?
10. What kinds of issues should someone be aware of when working with blocks?
11. Have you ever built and custom frameworks or libraries?
* What's your preferred method of working on an application in parallel with a library?
------
An elementary AVC (H.264) video stream is packaged as a series of network abstraction layer units (NAL units). Typically each NAL unit begins with a variable length start code consisting of at least two bytes of zeroes followed by 0x01. The start code must be at least 3 bytes in length (0x00 0x00 0x01), but may be longer. For instance:
00 00 01 DE AD BE EF 01 23 00 01 89 00 00 00 00 00 01 DA DB AD ...
[------------][---------------------------][-----------------][--------------]
START CODE(3) NAL UNIT (9) START CODE (6) NAL UNIT (20)
Let's suppose you have an H.264 decoder that wants the stream packaged differently for performance reasons. Instead of variable length start codes, it requires a 32-bit big endian length preceding each NAL unit, aka the "box" format. For instance:
00 00 00 09 DE AD BE EF 01 23 00 01 89 00 00 00 14 DA DB AD ...
[------------][---------------------------][-----------][-------------]
BE LEN (4) NAL UNIT (9) BE LEN (4) NAL UNIT (20)
Your task is to write a function in which you convert an elementary H.264 video stream with variable length start codes into the "box" format.
———
Q: A Sultan has a 1000 bottles of wine. He needs to use them in 30 days time for a royal banquet. He knows that his enemies have poisoned exactly one bottle with a type of poison that takes effect in 29 days. He decides to use his soldiers to test which bottle is poisoned. Is there a strategy that minimizes the number of soldiers needed for the task?
Probability Theory: The Logic of Science
A: The naive approach is to have one soldier per bottle. Every soldier gets a drop from each bottle and they wait for 29 days. The number of the soldier who gets affected on the 29th day shows which bottle is poisoned. However, this strategy is quite expensive in terms of the number of soldiers needed for the Sultan. A far more efficient strategy is the following:
1. Label each bottle with a number.
2. Maintain a ledger which maps a number to each of the patterns 0000000000 -> 1, 0000000001 -> 2, 0000000010 -> 3, 0000000100 -> 4 and so on till you reach 1111111111 -> 1000. Note there are 10 places that can hold either 0s or 1s.
3.Assign each soldier to each of the 10 places.
4. For each bottle number, give a drop of the wine to each soldier with a number 1.
What happens? On the 29th day, a certain combination of soldiers will be affected by the poison. Knowing that combination, the Sultan can trace back and find the exact bottle that contained the poison by looking up the ledger. This way the number of soldiers needed is minimized. Note, the problem solution can be extended to any number of bottles. By using the binary number encoding method the number of combinations needed can be ascertained by simply doing log2N where N is the number of bottles.
http://bayesianthink.blogspot.com/2013/11/the-sultans-wine-bottles.html#.Updy3RVx05k
--------
Q: Does Objective-C support multiple inheritance?
A: No
Q: How can you achieve the things you'd do with multiple inheritance in Objective-C?
A: You can have a class implement multiple protocols. You can add multiple categories to a class. You can use composition (nest instances of multiple classes as instance variables and wrap calls to them). You can also use method swizzling to substitute a method from another class.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment