15 Essential iOS Technical Code Challenge Questions (with Hints)
After helping many iOS developers prepare for technical interviews I’ve grown a fun list of questions I feel exercise a candidate’s breadth of knowledge when it comes to algorithms, data structures, and coding syntax. Feel free to cruise through the list as most of these are my favorites. What’s nice about programming is that there’s rarely a single right way of doing things. Rather it’s all about one proving their approach through thoughtful analysis, development experience as well as applying tools like Big-O Notation. Let’s begin!
1. Find Common Characters
Given an array of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer:
Input:["cool","lock","cook"]
Output:["c","o"]
Often when solving code challenges, specific words or phrases used provide clues for how best to approach the problem. For this challenge we see the word duplicates highlighted. Can you think of the an algorithm or data structure that addresses the common scenario of duplicates? How could this model be used to help us find common characters?
2. Sorting Colors
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively:
let sequence = [2,0,2,1,1,0] -> [0, 0, 1, 1, 2, 2]
If not done correctly, sorting data can be one of the most expensive aspects of any program or application. While standard production sorting algorithms run in O(n log n) time, the average / worst case runtimes often produce a O(n) squared result. Is there a more efficient technique or algorithm you could implement to sort the color sequence?
3. Removing Extra Space
Given a proposed String, write an in-place algorithm to remove all whitespace. For this challenge, assume you don’t have access to a regular expression engine or SDK library function:
let sequence = "The qu ick fox jum ped over the lazy d o g"
How will your function search through the the data? How will you track the removal of data? Will your solution make use of fast enumeration? Why or why not?
4. Prefix and Suffix Search
In a technical interview, you’ve been asked to design a class WordFilter that supports one function, WordFilter.f(String prefix, String suffix). It will return the word with given prefix and suffix. If no word exists, return -1.
WordFilter.(["apple"])
WordFilter.(["app"])
WordFilter.f("a", "e") // returns 0
WordFilter.f("b", "") // returns -1
While most structures are designed to manage generic data, other models are more efficient when managing textual content. As a result, these specialized solutions are often at the core of search engine requests, dictionaries and many types of games.
5. Find Common Element in Rows
Given a matrix where every row is sorted in increasing order, return the smallest common element in all rows. If there is no common element return -1.
Input: mat = [[1,2,3,4,5,8], [2,4,5,8,10], [3,5,7,8,9,11], [1,3,5,7,8,9]]Output: 5
How will you keep track of elements common to all rows? How many elements are common to all rows? How will you find the smallest value? As shown, there are a number of possibilities to track. Could the use of a specialized data structure be used to streamline your solution?
6. Best Auction Bids
In a technical interview, assume you are building a system to track top auction bids for your local community fundraiser. As new bids are made, your goal will be to track the number of top bids as they are announced:
let bids = [2, 1, 9, 4, 8, 6, 10, 5]
func topBids(bids: [Int]) -> Int {
//code goes here..
}
When organizing data, we sometimes make the assumption correctly sorted data should always be presented in ascending or descending order. However, there are many times when the primary objective is find the largest or smallest value for a given dataset.
7. Pair of Numbers
In a technical interview, you’ve have been given an array of numbers and you need to find a pair of numbers which are equal to the given target value. Numbers can be either positive, negative or both. Can you design an algorithm that works in O(n) — linear time or greater?
let sequence = [8, 10, 2, 9, 7, 5]
let results = pairValues(11) = //returns (9, 2)
What’s interesting about this solution is that it can be solved using different techniques. The main objective is calculating which numbers can be paired to achieve the expected result. How could this work, assuming your algorithm is only permitted to scan the sequence once?
8. Reverse the Vowels
You’ve been asked by your manager to build a new algorithm to reverse vowels in any proposed String. How would you build such a model, assuming you aren’t allowed to use any native String reverse() functions in your code?
e.g. Hello = Holle
e.g. BinarySearch = BanerySairch
When preparing for a technical interview, it’s common to practice code challenges on String manipulation, including being able to reverse sequences. This challenge presents a twist by reversing specific values. How will you track the position of vowels as you iterate through the model? Also, how will you reverse the series of vowels without using native Swift library functions?
9. Count of Occurrences
If asked in a technical interview, could you write a function that lists the number of occurrences for each word in descending order? For the output, assume your solution makes use of a standard Swift Dictionary or Array of tuple values. Also assume your algorithm does not make use of any built-in Swift sorting functions.
input:
this is a test of the emergency broadcast system this is only a test dog dog dogoutput:
dog 3
a 2
is 2
test 2
this 2
broadcast 1
emergency 1
of 1
only 1
the 1
system 1
At first glance, this challenge seems relatively straightforward. The idea being one could easily iterate through a list of words in O(n) — linear time and track the number of word occurrences with a standard dictionary. However, difficulty is introduced when sorting the word list. Without using a built-in function, how would you keep the list sorted as new items are being added?
10. Power of Two
If asked in a technical interview, could you write a function to determine if an integer is a power of two? For example:
Input: 1
Output: true
Explanation: 20 = 1Input: 16
Output: true
Explanation: 24 = 16Input: 218
Output: false
Even though the Swift SDK has built in functionality to accomplish this task, how would you write this algorithm from scratch? Could the input values be interpreted in a different way to achieve our objective?
11. Longest Subsequence
If asked in a technical interview, could you write a function in Swift to find the longest subsequence of array elements in consecutive order? The consecutive numbers can be in any order. For example:
//input
let sequence: Array<Int> = [9, 4, 10, 2, 7, 3, 5, 1]
//output = 4
the values 1, 2, 3, 4 are the longest sequence of consecutive elements
A consecutive sequence doesn’t necessarily require that our elements be in ascending or descending order. However, your solution will still need to be intuitive enough to recognize which values come in order. Could a brute force solution be designed to correctly sort through the array, or could the values be placed into a more efficient data structure?
12. Tree Traversal
If asked in a technical interview, what technique or algorithm would you use to print the following values in sequential order?
output = 1, 2, 3, 4, 5, 6, 7
Often what makes tree-based questions difficult is knowing how to navigate their structure. Just like with working with standard Swift collections like a Dictionary, Array or Set, knowing how to navigate and manipulate content in trees unlocks their capabilities and allows us to apply them to specific situations.
13. Fibonacci Revised
If asked in a technical interview, how would you improve the runtime performance of this code? Why would your version be more effective?
func fib(_ n: Int) -> Int {
guard n > 1 else { return n }
return fib(n-1) + fib(n-2)
}
When learning the basics of recursive algorithms, the Fibonacci sequence is a widely used standard for understanding this process. However, there are a number of ways we can write this algorithm. Even though our function is technically correct, how could this function be improved? What is being implied when we say the algorithm is inefficient?
14. Multiple Inheritance
If asked in a technical interview, could you explain or write code showing why Swift doesn’t support multiple inheritance? What’s the alternative?
Even though the concept of multiple inheritance was once considered a must-have feature in languages such as C++, most modern programming languages like Java and Swift only support single inheritance. As part of your analysis, can you think of why is no longer used?
15. Design a URL Shortener
In a technical interview, you’ve been asked to provide a high-level design for the creation of a url shortener service (eg. bit.ly). Describe the major functions of the service as well as any additional supporting components.
How does a url shortener work? How will you ensure each hashed url is unique? How will end users retrieve hashed urls? Are all urls guaranteed to be unique? Can two or more hashed url’s be resolved to the same url?