Skip to content

Instantly share code, notes, and snippets.

View AdrianFerreyra's full-sized avatar

Adrián Ferreyra AdrianFerreyra

View GitHub Profile
@AdrianFerreyra
AdrianFerreyra / longestPathBinaryTree.swift
Created November 6, 2017 03:20
longest path on binary tree (DFS)
/*
Given a tree of nodes that looks like this:
A
/ \
B G
/ \ \
C D H
/ \
E F
@AdrianFerreyra
AdrianFerreyra / sumNestedArray.swift
Created November 6, 2017 03:04
Given an array that contains numbers and/or other nested arrays, write an algorithm to come up with a sum of these elements, multiplied by the depth (or how many arrays deep) you are.
/*
Given an array that contains numbers and/or other nested arrays, write an algorithm to come up with a sum of these elements, multiplied by the depth (or how many arrays deep) you are.
For example, what would you do with an input array that looks like:
[ 2, 3, [ 9, [ 1, 2 ]], 4]
*/
import Foundation
@AdrianFerreyra
AdrianFerreyra / binaryStringsSum.swift
Created November 3, 2017 13:32
Sum of two binaries, represented through strings.
import Foundation
func sum(_ lhs: String,_ rhs: String, carry: Int = 0) -> String {
guard let lhsLast = lhs.last else {
if rhs.count > 0 {
return rhs
} else {
return carry == 1 ? "1" : ""
@AdrianFerreyra
AdrianFerreyra / resetZeros.swift
Created November 2, 2017 03:02
Given an array of numbers, reset the array to put all the non-zero numbers in front of all the zeros in the array, then return the count of non-zero numbers. e.g., for an input array of [3,0,2,0,0,1,0,4], you’ll end up with a return value of 4 and an array of [3,2,1,4,0,0,0,0]
import Foundation
/*Given an array of numbers, reset the array to put all the non-zero numbers in front of all the zeros in the array, then return the count of non-zero numbers.
e.g., for an input array of [3,0,2,0,0,1,0,4], you’ll end up with a return value of 4 and an array of [3,2,1,4,0,0,0,0]
*/
//idea
//get first zero
//return func(array.removeZero) + [0]
@AdrianFerreyra
AdrianFerreyra / findCommonSuperviews.swift
Created November 1, 2017 22:19
How to detect a common superview (IOS Developer Interview Question)
func commonSuperviews(between lhs: UIView, and rhs: UIView) -> [UIView] {
func getSuperviews(for view: UIView) -> [UIView] {
guard let superview = view.superview else {
return []
}
return [superview] + getSuperviews(for: superview)
@AdrianFerreyra
AdrianFerreyra / equivalentStrings.swift
Created November 1, 2017 22:07
Resolution of a Facebook iOS Code Challenge.
import Foundation
/*
A telephone keypad has letters associated with each number (e.g. 2 = abc, 3 = def).
Given a passphrase of "fb1" (e.g. one that you might use to log into a bank account),
come up with an algorithm that would assemble an array that contains all the different
possible letter combinations that, when typed into a telephone dial pad, would be equivalent
to the original passphrase. That is, "fb1" equals "321" numerically; matching equivalent
combinations include: "da1", "db1", "dc1", "ea1", "eb1", "ec1", "fa1" and "fc1".
*/
@AdrianFerreyra
AdrianFerreyra / recursivePermutations.swift
Created November 1, 2017 19:52
Getting permutations of a Set of Strings recursively.
import Foundation
func permutations(_ set: Set<String>) -> Set<String> {
guard set.count != 0 else {
return Set<String>([""])
}
return set.reduce(Set<String>()) { accumulator, element in
@AdrianFerreyra
AdrianFerreyra / linkedListSwapElements.swift
Last active October 31, 2017 18:54
Return the head node of the singly linked list with each pair of nodes swapped. If there is a last odd node leave it in place. Example: Input: 1 -> 2 -> 3 -> 4 -> 5 Output: 2 -> 1 -> 4 -> 3 -> 5
enum LinkedList {
case empty
indirect case node(T, LinkedList)
func swapTwoFirst() -> LinkedList {
switch self {
case .node(let value, .node(let nextValue, let next)):
return .node(nextValue, .node(value, next.swapTwoFirst()))
@AdrianFerreyra
AdrianFerreyra / recursiveInsertionSort.swift
Created October 31, 2017 12:41
A brief recursive implementation of InsertionSort on generic Comparable element's arrays in Swift 4.
import Foundation
//InsertionSort
///Inserts a value into a sorted array
func insert<T: Comparable>(value: T,into array: [T]) -> [T] {
guard let last = array.last else {
return [value]
@AdrianFerreyra
AdrianFerreyra / In-SituMergeSort.swift
Created October 31, 2017 03:25
In-Situ implementation of MergeSort
import Foundation
//Merge Sort
func merge<T: Comparable>(_ lhs: [T],_ rhs: [T]) -> [T] {
switch (lhs,rhs) {
case (let lhs, let rhs) where lhs.count == 0:
return rhs
case (let lhs, let rhs) where rhs.count == 0: