Skip to content

Instantly share code, notes, and snippets.


Farhan Farooqui farhanf

View GitHub Profile

I am Swift developer and don't know JavaScript. But I attempted to answer this question.

I think the bug is in the definition of the lambda function. It uses x + i for every function that it stores. This will mean that i is a constant value for each function and will return the same result regardless of its position in the function array that is created.

I think the authors intention was to have the function return a result thats dynamic to what position is in the array of functions that is created. I am not sure how to fix this error in JS.

If this is not satisfactory I would request a function in swift or objective-c you would like me to debug.

View answer3.swift
// Run in playgrounds
var index = 0
func nextfib(_ num: Int){
let result = fib(index)
if result > num {

I attemped two solutions because the instructions didn't specify if I could use built-ins or not.

The first solution is non-linear time. It is big O of n squared as the function goes through the values of the first array for every value of the second array until it finds a value that is not in the first array.

The second solution is in linear time because it uses hashtable behind the scenes thanks to swift built in Set(). Assuming it is a good hashing function this will be big O of n where n is the length of the the second array.

I prefer the second function because it is more performant and easier to read. It can be used for any object as long as it is conforms to hashable protocol.

View answer1.swift
func isSubsetOf(arr1: [String], arr2: [String]) -> Bool {
var result = false
arr2.forEach { (str) in
conditional: if (arr1.contains(str)) {
result = true
} else {
result = false
break conditional