Last active
November 14, 2022 13:18
-
-
Save jvillste/e826b7e683402521e6f1f41d51e7845a to your computer and use it in GitHub Desktop.
interview
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
(defn load-employees-from-db [] | |
[{:name "Mary" :salary 6000} | |
{:name "John" :salary 4000}]) | |
(let [sum (atom 0)] | |
(doseq [e (load-employees-from-db)] | |
(reset! sum (+ @sum (:salary e)))) | |
(/ @sum | |
(count (load-employees-from-db)))) |
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
(first (map prn (vec (range 1024)))) |
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
(defn sum-tree [root] | |
(if (nil? root) | |
nil | |
(let [a-sum-tree (sum-tree (:left root)) | |
b-sum-tree (sum-tree (:right root))] | |
(cond-> {:value (+ (:value root) | |
(or (:value a-sum-tree) | |
0) | |
(or (:value b-sum-tree) | |
0))} | |
(some? a-sum-tree) | |
(assoc :left a-sum-tree) | |
(some? b-sum-tree) | |
(assoc :right b-sum-tree))))) | |
(deftest test-sum-tree | |
(is (= {:value 16, | |
:left {:value 3 | |
:left {:value 1}} | |
:right {:value 12 | |
:left {:value 4} | |
:right {:value 5}}} | |
(sum-tree {:value 1 | |
:left {:value 2 | |
:left {:value 1}} | |
:right {:value 3 | |
:left {:value 4} | |
:right {:value 5}}})))) |
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
1 16 | |
/ \ / \ | |
2 3 ==> 3 12 | |
/ / \ / / \ | |
1 4 5 1 4 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment