Last active
August 29, 2015 14:04
-
-
Save johnazariah/d848093d2ffbeeb9bbaa to your computer and use it in GitHub Desktop.
Interview Question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Building = { | |
left : int | |
right : int | |
height : int | |
} | |
let buildings : Building[] = [| (* ... *) |] | |
let getHeight building = building.height | |
let getWidth building = building.right - building.left | |
let getArea building = getHeight building * getWidth building | |
type Op = { | |
map : Building -> int | |
reduce : int -> int -> int | |
zero : int | |
} | |
let maxHeight = { | |
map = getHeight | |
reduce = (fun l r -> if l < r then l else r) | |
zero = System.Int32.MinValue | |
} | |
let minWidth = { | |
map = getWidth | |
reduce = (fun l r -> if l > r then l else r) | |
zero = System.Int32.MaxValue | |
} | |
let totalArea = { | |
map = getArea | |
reduce = (+) | |
zero = 0 | |
} | |
let fs = [| maxHeight; minWidth; totalArea |] | |
let limits = | |
buildings | |
|> Seq.map | |
(fun b -> fs |> Array.map (fun f -> f.map b)) | |
|> Seq.fold | |
(fun result curr -> fs |> Array.mapi (fun i f -> f.reduce result.[i] curr.[i])) | |
(fs |> Array.map (fun f -> f.zero)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment