├── CONTRIBUTORS.md
├── README.md
├── css
├── challenges
│ └── 01-odd-even.css
└── solutions
│ └── 01-odd-even-solution.css
├── javascript
├── challenges
│ ├── 01-hello-world.js
│ ├── 02-fetcher.js
│ ├── 03-stateful.js
│ ├── 04-sets.js
│ ├── 05-what-for.js
│ ├── 06-unreachable-condition.js
│ ├── 07-string-reverse.js
│ ├── 08-battlestar-part-1.js
│ ├── 09-battlestar-part-2.js
│ └── 10-battlestar-part-3.js
├── helpers
│ └── sleep.js
└── solutions
│ ├── 04-sets-solution.js
│ ├── 05-what-for-solution.js
│ ├── 06-unreachable-condition-solution.js
│ └── 07-string-reverse-solition.js
├── package.json
└── ruby
├── challenges
├── 01-average.rb
├── 02-duplicates.rb
├── 03-nums-even.rb
└── 04-only-vowels.rb
└── solutions
├── 01-average-solution.rb
├── 03-nums-even-solution.rb
└── 04-only-vowels-solution.rb
/CONTRIBUTORS.md:
--------------------------------------------------------------------------------
1 | | Name (optional) | @handle | challenges contributed to |
2 | | ------------------- | ---------- | ----------------------------------- |
3 | | Francesco Agnoletto | @kornil | js01, js02, js03, js04, js06, css01 |
4 | | Roxana Oanes | @roxyoanes | rb01, rb02, rb03, rb04 |
5 | | | | |
6 | | | | |
7 | | | | |
8 | | | | |
9 | | | | |
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Mixed Programming Challenges
2 |
3 | Whiteboard test challenge prep.
4 | Some of the projects are not meant to be run, as the code provided abstracts some parts for increased readability.
5 |
6 | Challenges are divided in folders per language. Pick your language folder and try the exercises!
7 |
8 | ## Rules
9 | 1) You are not allowed to run the test code in any editor or environment. These challenges are meant to be solved with "pen and paper".
10 | 2) You are only allowed to modify the code inside `/challenges`.
11 | 4) Other than the solution, you have to provide an explanation.
12 | 5) You are allowed to google questions you might have if absolutely necessary. Ex: googling what `Array.map()` does is *ok*, googling an online editor to check the code is *not ok*.
13 | 6) Might be obvious but do not read the solutions inside `/solutions` if you want to practice the challenges.
14 |
15 | ### Language specific help
16 |
17 | #### JavaScript
18 | - Code is meant to run in node8
19 |
20 | ## Contribute
21 | Contributing is easy, simply fork the project, clone it locally, add your challenge into the `/challenges` folder and provide a solution inside `/solutions` (can be a simple comment or more). Naming for the challenge must follow this format:
22 | * `xx` being a number (just for sorting purposes)
23 | * `challenge-name` be creative!
24 | * (ONLY FOR SOLUTIONS) `-solution` quite self explanatory
25 |
26 | Remember to add your name on `CONTRIBUTORS.md`!
27 |
--------------------------------------------------------------------------------
/css/challenges/01-odd-even.css:
--------------------------------------------------------------------------------
1 | //
2 | //
hello
3 | //
hello
4 | //
hello
5 | //
hello
6 | //
hello
7 | //
8 | //
9 | // make all even "list-item" background-color: blue
10 |
11 | .list > .list-item {
12 | background-color: red;
13 | }
14 |
--------------------------------------------------------------------------------
/css/solutions/01-odd-even-solution.css:
--------------------------------------------------------------------------------
1 | .list > .list-item {
2 | background-color: red;
3 | }
4 |
5 | .list > .list-item:nth-of-type(even) {
6 | background-color: blue;
7 | }
--------------------------------------------------------------------------------
/javascript/challenges/01-hello-world.js:
--------------------------------------------------------------------------------
1 | const sleep = require("../helpers/sleep");
2 |
3 | // output should be displayed after the timer
4 |
5 | const func = (() => {
6 | sleep(5000);
7 | console.log("hello");
8 | })();
9 |
--------------------------------------------------------------------------------
/javascript/challenges/02-fetcher.js:
--------------------------------------------------------------------------------
1 | const sleep = require("../helpers/sleep");
2 |
3 | // sometimes it does not work
4 |
5 | const main = async() => {
6 | const taskRunner = new TaskRunner();
7 | const first = taskRunner.run(() => fetch(/* random api */));
8 | await sleep(100);
9 | const second = taskRunner.run(() => fetch(/* random api */));
10 | await sleep(100);
11 | const third = taskRunner.run(() => fetch(/* random api */));
12 | await Promise.all([first, second, third])
13 | }
14 |
15 | class TaskRunner {
16 | constructor() {
17 | this.pending = [];
18 | this.running = false;
19 | }
20 | run(exec) {
21 | const task = { exec };
22 | const promise = new Promise((resolve, reject) => {
23 | Object.assign(task, { resolve, reject });
24 | });
25 | this.pending.push(task)
26 | if (!this.running) {
27 | this.running = true;
28 | this.shift();
29 | }
30 | return promise;
31 | }
32 | shift() {
33 | const task = this.pending.shift();
34 | if (task) {
35 | task.exec()
36 | .then(task.resolve, task.reject)
37 | .finally(() => this.shift())
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/javascript/challenges/03-stateful.js:
--------------------------------------------------------------------------------
1 | // SyntaxError unexpected identifier. Should return increased value.
2 |
3 | function counter() {
4 | let i = 0;
5 | while (true) {
6 | yield i++
7 | }
8 | }
9 |
10 | const gen = counter();
11 |
12 | gen.next().value;
13 | gen.next().value;
14 | console.log(gen.next().value);
15 |
--------------------------------------------------------------------------------
/javascript/challenges/04-sets.js:
--------------------------------------------------------------------------------
1 |
2 | // explain the below function.
3 |
4 | const mysteryFunc = (...string) => [...new Set([...string].join(' ').split(' '))].join(' ');
5 |
--------------------------------------------------------------------------------
/javascript/challenges/05-what-for.js:
--------------------------------------------------------------------------------
1 | // What is the output of the code below?
2 | // How would you correct the error?
3 | // Give reasons for your solution and how it compares against potential alternatives
4 |
5 | // Expected Output
6 | // -> Hello
7 | // -> world
8 | // -> !
9 |
10 | let contents = ['Hello', 'world', '!'];
11 |
12 | for (let i in contents) {
13 | console.log(i);
14 | }
15 |
--------------------------------------------------------------------------------
/javascript/challenges/06-unreachable-condition.js:
--------------------------------------------------------------------------------
1 | // both params are random numbers
2 | // it never returns "hello"
3 | //
4 | // func(0,1) => "world"
5 | // func(1,2) => "world"
6 |
7 | const func = (num1, num2) => (num1 && num2 === false ? "hello" : "world");
8 |
--------------------------------------------------------------------------------
/javascript/challenges/07-string-reverse.js:
--------------------------------------------------------------------------------
1 | // It does not work
2 | // should output the inverted string
3 | // revertString("hello") => "olleh"
4 |
5 | const revertString = (str) => str.reverse();
6 |
--------------------------------------------------------------------------------
/javascript/challenges/08-battlestar-part-1.js:
--------------------------------------------------------------------------------
1 | // Battlestar-ship Radar
2 |
3 | // There's a ship in the vast ocean of space! Currently, the fashion of the day is to have spherical spaceships - due to limited data point resolution, these are represented as squares.
4 |
5 | // Your task is to process low granularity radar data to return the coordinates of the ship.
6 |
7 | // Given a matrix of 0s (empty) and 1s (present), write a function that returns the position.
8 |
9 |
10 | // Input - Empty as 0, Present as 1
11 | let scan = [
12 | [0, 0, 0, 0],
13 | [0, 1, 1, 0],
14 | [0, 1, 1, 0],
15 | [0, 0, 0, 0]
16 | ];
17 |
18 | // Output - anything that clearly describes the location of the ship
19 |
20 | // { x: 1, y: 1, size: 2 }
21 |
22 | // * or *
23 | // { x1: 1, y1: 1, x2: 2, y2: 2 }
24 | // or whatever format you prefer
25 |
26 |
27 |
28 |
29 | let test = [
30 | [0, 0, 0, 0],
31 | [0, 0, 0, 0],
32 | [0, 0, 1, 1],
33 | [0, 0, 1, 1]
34 | ];
35 |
36 | // { x: 2, y: 2, size: 2 }
37 |
--------------------------------------------------------------------------------
/javascript/challenges/09-battlestar-part-2.js:
--------------------------------------------------------------------------------
1 | // What! Could it be? The world is changing, and in fact many ship designers have realized that they could simplify logistics using rectangular ships. Given this breakthrough, can your radar processor hold up?
2 |
3 | // Input - Empty as 0, Present as 1
4 | let scan2 = [
5 | [0, 0, 0, 0],
6 | [0, 1, 1, 0],
7 | [0, 1, 1, 0],
8 | [0, 1, 1, 0]
9 | ];
10 |
11 | // Output - anything that clearly describes the location of the ship
12 |
13 | // { x: 2, y: 1, width: 2, height: 3 }
14 |
15 | // or whatever format you prefer
16 |
17 |
18 |
19 |
20 | let test2 = [
21 | [0, 0, 0, 0],
22 | [1, 1, 1, 1],
23 | [1, 1, 1, 1],
24 | [0, 0, 0, 0]
25 | ];
26 | // { x: 0, y: 1, width: 4, height: 2 }
--------------------------------------------------------------------------------
/javascript/challenges/10-battlestar-part-3.js:
--------------------------------------------------------------------------------
1 | // Technological advances have made ships increasingly affordable. Now tons of people are flying around and by God we need to track them. Make any changes that are necessary to return the position of multiple, rectangular ships.
2 |
3 | // Input - Empty as 0, Present as 1
4 | let scan3 = [
5 | [1, 0, 0, 0],
6 | [1, 0, 1, 1],
7 | [0, 0, 1, 1],
8 | [0, 0, 1, 1]
9 | ];
10 |
11 | // Output - anything that clearly describes the location of the ship
12 |
13 | // [
14 | // { x: 0, y: 0, width: 1, height: 2 }
15 | // { x: 2, y: 1, width: 2, height: 3 }
16 | // ]
17 |
18 | // or whatever format you prefer
19 |
20 |
21 |
22 |
23 | let test3 = [
24 | [1, 1, 1, 0],
25 | [1, 1, 1, 0],
26 | [0, 0, 0, 0],
27 | [1, 1, 1, 1]
28 | ];
29 | // [
30 | // { x: 0, y: 0, width: 3, height: 2 }
31 | // { x: 0, y: 3, width: 4, height: 1 }
32 | // ]
--------------------------------------------------------------------------------
/javascript/helpers/sleep.js:
--------------------------------------------------------------------------------
1 | module.exports = function(ms) {
2 | return new Promise(resolve => setTimeout(resolve, ms));
3 | };
4 |
--------------------------------------------------------------------------------
/javascript/solutions/04-sets-solution.js:
--------------------------------------------------------------------------------
1 | // first create an array with all inputted strings
2 | // split all the string in array by space
3 | // remove all duplicates and turn it into a single string
4 | // input: "bob" "alice" "john" "bob bob dan"
5 | // output: "bob alice john dan"
6 | const deduplicateString = (...string) =>
7 | [...new Set(
8 | [...string]
9 | .join(" ")
10 | .split(" ")
11 | )
12 | ].join(" ");
13 |
--------------------------------------------------------------------------------
/javascript/solutions/05-what-for-solution.js:
--------------------------------------------------------------------------------
1 | let contents = ['Hello', 'world', '!'];
2 |
3 | // Original code question
4 | for (let i in contents) {
5 | console.log(i);
6 | }
7 | // Output -> 0, 1, 2
8 |
9 | // ------------------------
10 | // Possible solutions
11 | // Solution No.1
12 | for (let i in contents) {
13 | console.log(contents[i]);
14 | }
15 | // While you do arrive at the correct output in this case, there are some potential pitfalls of using 'for in' on an array.
16 | // When 'for in' is used on an array elements within the array can move around unexpectedly.
17 | // If you add in an array element at a specific position, and other elements between
18 | // are undefined the returned data is sometimes not what you would expect.
19 | // eg. contents[5] = 'unicorn'
20 | // Expected -> Hello, world, !, undefined,undefined, unicorn
21 | // Result -> Hello, world, !, unicorn
22 |
23 | // Solution No.2
24 | contents.forEach(i => console.log(i));
25 | // Very compact ES6 solution, and in the majority of cases it works very well.
26 | // Do keep an eye on the size of the potential array that it will be working with
27 | // as forEach is not the most performant 'for' loop.
28 |
29 | // Solution No.3 - BEST SOLUTION
30 | for (let i of contents) {
31 | console.log(i);
32 | }
33 | // Unlike 'for in' which work best with Object, 'for of' works brilliantly for Arrays.
34 | // Doesn't suffer from dropping elements like 'for in' does, and is also very performant.
35 | // eg. contents[5] = 'unicorn'
36 | // Expected -> Hello, world, !, undefined,undefined, unicorn
37 | // Result -> Hello, world, !, undefined, undefined, unicorn
38 |
--------------------------------------------------------------------------------
/javascript/solutions/06-unreachable-condition-solution.js:
--------------------------------------------------------------------------------
1 | // both params are random numbers
2 | // it never returns "hello"
3 | //
4 | // func(0,1) => "world"
5 | // func(1,2) => "world"
6 |
7 | /* Explanation: num1 && num2 === false is always false, so removing this condition
8 | we have a true or false result depending on the two numbers */
9 | const func = (num1, num2) => (num1 && num2 ? "hello" : "world");
10 |
--------------------------------------------------------------------------------
/javascript/solutions/07-string-reverse-solition.js:
--------------------------------------------------------------------------------
1 | // .reverse() is an Array built in method, String does not have it.
2 | // to use it, we have to turn our string in an array, reverse and then join it again.
3 |
4 | const revertString = (str) => str.split("").reverse().join("");
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "code-challenges",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "author": "",
10 | "license": "ISC"
11 | }
12 |
--------------------------------------------------------------------------------
/ruby/challenges/01-average.rb:
--------------------------------------------------------------------------------
1 | # Sometimes it doesn't work
2 |
3 | def average(num1, num2)
4 | (num1 + num2) / 2
5 | end
--------------------------------------------------------------------------------
/ruby/challenges/02-duplicates.rb:
--------------------------------------------------------------------------------
1 | words = ["cat", "dog", "mouse", "cat", "donkey"]
2 |
3 | find_duplicates(words)
4 | # ["cat"]
--------------------------------------------------------------------------------
/ruby/challenges/03-nums-even.rb:
--------------------------------------------------------------------------------
1 | # print even numbers in a new array
2 | # it should be a one line solution
3 |
4 | nums = [1,2,31,42,56,67,72,81,88,103]
5 |
6 |
--------------------------------------------------------------------------------
/ruby/challenges/04-only-vowels.rb:
--------------------------------------------------------------------------------
1 | # Write a method, `only_vowels?(str)`, that accepts a string as an arg.
2 | # The method should return a new string containing only vowels.
3 |
4 | def only_vowels?(str)
5 |
6 | end
--------------------------------------------------------------------------------
/ruby/solutions/01-average-solution.rb:
--------------------------------------------------------------------------------
1 | # With the current setup the result provided by an odd number would have not been precise, see
2 | # average(10, 5) => 7
3 | # to have a more precise result we need to allow decimal numbers.
4 |
5 | def average(num1, num2)
6 | (num1 + num2) / 2.0
7 | end
--------------------------------------------------------------------------------
/ruby/solutions/03-nums-even-solution.rb:
--------------------------------------------------------------------------------
1 | print nums.select{ |ele| ele % 2 == 0 }
2 |
--------------------------------------------------------------------------------
/ruby/solutions/04-only-vowels-solution.rb:
--------------------------------------------------------------------------------
1 | # Create a string of vowels
2 | # Create an empty string that will store later the result
3 | # Split string and loop through each character
4 | # Check if character is a vowel, and if it is, shovel it in the empty string
5 |
6 |
7 | def only_vowels?(str)
8 | vowels = "aeiou"
9 | new_str = ""
10 | words = str.split("")
11 | words.each do |char|
12 | if vowels.include?(char)
13 | new_str << char
14 | end
15 | end
16 | new_str
17 | end
--------------------------------------------------------------------------------