When preparing for a technical interview, mastery of code language syntax provides a great start, but understanding computer science is also essential. Here are the top 20 standard computer science interview questions with hints to help you prepare for the next step in your development career. Need help? Consider registering for a free class in my computer science lab.

How to Use

icloud_cloud.png

The following questions are based on my own work, refined in collaboration with senior developers in my program. While questions can be solved through basic collection types (e.g., Array, Dictionary, and Set), difficult questions are easier solved through advanced data structures, algorithms and ways of thinking.

As is common in software development, questions are solved in different ways. The trick is knowing which approach provides the best tradeoff in consideration of time and space complexity.


Top 20 CS Interview Questions

 

Counting Sort Algorithm

orange-checkmark.png

Given the following array, produce a list that provides the number of times each selected value has occurred in the series.

let sequence = [0, 1, 3, 1, 2, 4, 3, 2] 
/*
Results: 
0 seen 1 time
1 seen 2 times
3 seen 2 times
4 seen 1 times
2 seen 2 times
*/

As part of your solution, what will be your input and output parameters (e.g. function signature)? Will a recursive solution provide increased benefits relative to space or time? Can the question be solved using an in-place design? Why or why not?


KTh Largest Element

orange-checkmark.png

How would you find the 4th largest element?

let sequence: Array<Int> = [8, 806, 2, 4, 56]

This question is quite well known but does teach a variety of techniques. One approach would be to sort the array to determine which values come in order. However, note the question doesn’t state that the values require sorting. Since actual sorting is a traditionally expensive operation, could be the data be transformed into a more efficient data structure?


Multiple Inheritance

orange-checkmark.png

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?


Fibonacci Revised

orange-checkmark.png

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?


Binary Tree Traversal

orange-checkmark.png

If asked in a technical interview, what technique or algorithm would you use to print the following values in sequential order?

binary-tree.png

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.


Longest Subsquence

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:

orange-checkmark.png
//input
let sequence: Array<Int> = [9, 4, 10, 2, 7, 3, 11, 1]

//output = 4
the values 1, 2, 3, 4 are the longest sequence of consecutive elements

As shown, 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?


Power Of Two

orange-checkmark.png

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 = 1

Input: 16
Output: true
Explanation: 24 = 16

Input: 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?


Count of Occurences

orange-checkmark.png

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 dog
output:
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?


Reverse The Vowels

orange-checkmark.png

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?


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?

orange-checkmark.png
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 obvious challenge is knowing 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?


Best Auction Bids

orange-checkmark.png

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 that correctly sorted data should always be presented in ascending or descending order. However, there are many times when the goal is find the largest or smallest value for a given dataset.


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.

orange-checkmark.png
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?


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.

orange-checkmark.png
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 data. As a result, these specialized solutions are often at the core of search engine requests, dictionaries and many types of games.


Removing Extra Space

orange-checkmark.png

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 you solution make use of fast enumeration? Why or why not?


Sorting Colors

orange-checkmark.png

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 augment to sort the color sequence?


Find Common Characters

orange-checkmark.png

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?


Deck of Cards

orange-checkmark.png

In a technical interview, you've been asked to provide a high-level design for the creation of a regular deck of playing cards. Describe the major functions of the service as well as any additional supporting components.

 
queen_of_diamonds2.png
 

How will you design the concept of a card? How will define card values? Will you differentiate between numbered and face cards? Can Swift generics be used to streamline the creation process?


Recreate Swift Dictionary

orange-checkmark.png

If asked in a technical interview, could you recreate the main functionally used for a native Swift Dictionary? Popular in iOS development, Dictionaries are particular collection types that associate values to a key.

 let vehicles = Glossary<String, String>()
        
 //add new values
 vehicles.updateValue("Porshe", forKey: "Car")
        
 //check existing value
 if let results = vehicles.valueForKey("Car") {
    print("car is: \(results)")
 }

In this example, we’ve designed a custom Dictionary with the type name Glossary. As shown, we can use the custom Glossary to associate and retrieve a value for a specified key (Car). 

What structure(s) will you use to store dictionary keys and values? How will you associate a key to a value? Will your custom type support Generics? How will this work? What actions occur when attempting to delete dictionary records?


Design A Call Center in Swift

If asked in a technical interview, could you design the main features of a Call Center in Swift? The design of a call center is a relatively straightforward model based on managing one or many requests through a centralized dispatching system.

As calls get initiated, they wait to be answered using a standard queuing system, easily modeled in the real world. They are based on a first-in, first-out design.

orange-checkmark.png

Design A Parking Lot in Swift

orange-checkmark.png

How would you design a virtual parking lot system in Swift?

What kind of structure will represent multiple parking spaces at your lot? How will you determine if your entire parking lot is at (or nearing) capacity? What will be the process for making a parking reservation?

If reserved parking is at capacity, will customers be able to park in a regular spot? Will your lot have pricing discounts based on when someone enters the garage or makes a reservation? (date or time)


Recreate Siri Knowledge Graph In Swift

orange-checkmark.png

In a technical interview, you are asked to design the essential components needed to recreate the very popular Siri virtual assistant present on iOS and Mac OS devices. While the inner workings of Siri are assumed to be complex (much beyond the scope of a technical interview), some fundamental elements of Siri could be replicated through well-known data structures, algorithms as well as the Swift SDK:

Siri is a knowledge graph, which presents some slight differences between itself and traditional graph-based models. Other questions include:

  • What is a knowledge graph?

  • How would you represent the data structures needed to represent a graph in Swift?

  • How is Siri able to retrieve the answer to your question through spoken language.

  • Could this process be replicated through existing libraries present in the iOS SDK?



Congratulations for making it through the list! Be sure also check out my list of popular Swift interview questions and check out my latest book.