Skip to content

Commit

Permalink
feat(book/questions): add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
amejiarosario committed Aug 31, 2020
1 parent 29b8dc8 commit c1a8f8e
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 9 deletions.
19 changes: 19 additions & 0 deletions book/content/part02/array.asc
Expand Up @@ -284,6 +284,15 @@ To sum up, the time complexity of an array is:

*AR-1*) _Given an array of integers, find the maximum sum of consecutive elements (subarray)._

Examples:

[source, javascript]
----
maxSubArray([1, -3, 10, -5]); // 10 (taking only 10)
maxSubArray([-3, 4,-1, 2, 1, -5]); // 6 (sum [4,-1, 2, 1])
maxSubArray([-2, 1, -3, 4, -1, 3, 1]); // 7 (sum [4,-1, 3, 1])
----

_Seen in interviews at: Amazon, Apple, Google, Microsoft, Facebook_
// end::array-q-max-subarray[]

Expand All @@ -301,6 +310,16 @@ _Solution: <<array-q-max-subarray>>_

*AR-2*) _You are given an array of integers. Each value represents the closing value of the stock on that day. You are only given one chance to buy and then sell. What's the maximum profit you can obtain? (Note: you have to buy first and then sell)_

Examples:

[source, javascript]
----
maxProfit([1, 2, 3]) // 2 (buying at 1 and selling at 3)
maxProfit([3, 2, 1]) // 2 (no buys)
maxProfit([5, 10, 5, 10]) // 5 (buying at 5 and selling at 10)
----

_Seen in interviews at: Amazon, Facebook, Bloomberg_
// end::array-q-buy-sell-stock[]

Expand Down
17 changes: 16 additions & 1 deletion book/content/part02/linked-list.asc
Expand Up @@ -294,6 +294,13 @@ For the next two linear data structures <<part02-linear-data-structures#stack>>

*LL-1*) _Merge two sorted lists into one (and keep them sorted)_

Examples:

----
mergeTwoLists(2->3->4, 1->2); // 1->2->2->3->4
mergeTwoLists(2->3->4,null); // 2->3->4
----

_Seen in interviews at: Amazon, Adobe, Microsoft, Google_
// end::linkedlist-q-merge-lists[]

Expand All @@ -312,7 +319,15 @@ _Solution: <<linkedlist-q-merge-lists>>_
// tag::linkedlist-q-linkedlist-same-data[]
===== Check if two strings lists are the same

*LL-2*) _Given two linked lists with strings, check if are the same_
*LL-2*) _Given two linked lists with strings, check if the data is equivalent._

Examples:

----
hasSameData(he->ll->o, hel->lo); // true
hasSameData(hello, hel->lo); // true
hasSameData(he->ll->o, h->i); // false
----

_Seen in interviews at: Facebook_
// end::linkedlist-q-linkedlist-same-data[]
Expand Down
15 changes: 14 additions & 1 deletion book/content/part02/queue.asc
Expand Up @@ -120,7 +120,20 @@ _Solution: <<queue-q-recent-counter>>_
// tag::queue-q-design-snake-game[]
===== Design Snake Game

*QU-2*) _Design the `move` function for the snake game. The move function returns an integer representing the current score. If the snake goes out of the given height and width or hit itself return `-1` for a game over._
*QU-2*) _Design the `move` function for the snake game. The move function returns an integer representing the current score. If the snake goes out of the given height and width or hit itself the game is over and return `-1`._

Example:

[source, javascript]
----
const snakeGame = new SnakeGame(4, 2, [[1, 2], [0, 1]]);
expect(snakeGame.move('R')).toEqual(0); // 0
expect(snakeGame.move('D')).toEqual(0); // 0
expect(snakeGame.move('R')).toEqual(1); // 1 (ate food1)
expect(snakeGame.move('U')).toEqual(1); // 1
expect(snakeGame.move('L')).toEqual(2); // 2 (ate food2)
expect(snakeGame.move('U')).toEqual(-1); // -1 (hit wall)
----

_Seen in interviews at: Amazon, Bloomberg, Apple_
// end::queue-q-design-snake-game[]
Expand Down
19 changes: 19 additions & 0 deletions book/content/part02/stack.asc
Expand Up @@ -95,6 +95,17 @@ It's not very common to search for values on a stack (other Data Structures are

*ST-1*) _Given an string with 3 types of brakets: `()`, `{}`, and `[]`. Validate they are properly closed and opened._

Examples:

[source, javascript]
----
isParenthesesValid('(){}[]'); // true
isParenthesesValid('('); // false (closing parentheses is missing)
isParenthesesValid('([{}])'); // true
isParenthesesValid('[{]}'); // false (brakets are not closed in the right order)
isParenthesesValid('([{)}]'); // false (closing is out of order)
----

_Seen in interviews at: Amazon, Bloomberg, Facebook, Citadel_
// end::stack-q-valid-parentheses[]

Expand All @@ -116,6 +127,14 @@ _Solution: <<stack-q-valid-parentheses>>_

*ST-2*) _Given an array of integers from 30 to 100 (daily temperatures), return another array that for each day in the input, tells you how many days you would have to wait until a warmer temperature. If no warmer temperature is possible then return `0` for that element._

Examples:

[source, javascript]
----
dailyTemperatures([30, 28, 50, 40, 30]); // [2 (to 50), 1 (to 28), 0, 0, 0]
dailyTemperatures([73, 69, 72, 76, 73, 100]); // [3, 1, 1, 0, 1, 100]
----

_Seen in interviews at: Amazon, Adobe, Cisco_
// end::stack-q-daily-temperatures[]

Expand Down
4 changes: 2 additions & 2 deletions book/interview-questions/daily-temperatures.js
Expand Up @@ -5,8 +5,8 @@
* for each elem from the input.
*
* @examples
* dailyTemperatures([30, 28, 50, 40, 30]); // [2, 1, 0, 0, 0]
* dailyTemperatures([73, 69, 72, 76, 73]); // [3, 1, 1, 0, 0]
* dailyTemperatures([30, 28, 50, 40, 30]); // [2, 1, 0, 0, 0]
* dailyTemperatures([73, 69, 72, 76, 73]); // [3, 1, 1, 0, 0]
*
* @param {number[]} t - Daily temperatures
*/
Expand Down
6 changes: 3 additions & 3 deletions book/interview-questions/design-snake-game.js
Expand Up @@ -14,9 +14,9 @@ const { Queue } = require('../../src/index');
* snakeGame.move('R'); // 0
* snakeGame.move('D'); // 0
* snakeGame.move('R'); // 0
* snakeGame.move('U'); // 1 (ate the food1)
* snakeGame.move('L'); // 2 (ate the food2)
* snakeGame.move('U'); // -1 (hit the upper wall)
* snakeGame.move('U'); // 1
* snakeGame.move('L'); // 2
* snakeGame.move('U'); // -1
*/
class SnakeGame {
// end::description[]
Expand Down
5 changes: 3 additions & 2 deletions book/interview-questions/max-subarray.js
@@ -1,9 +1,10 @@
// tag::description[]
/**
* Find the maximun sum of contiguous elements in an array.
*
* @examples
* maxSubArray([1, -3, 10, -5]); // => 10
* maxSubArray([-3,4,-1,2,1,-5]); // => 6
* maxSubArray([1, -3, 10, -5]); // => 10
* maxSubArray([-3,4,-1,2,1,-5]); // => 6
*
* @param {number[]} a - Array
*/
Expand Down
4 changes: 4 additions & 0 deletions book/interview-questions/max-subarray.spec.js
Expand Up @@ -8,6 +8,10 @@ describe('Max Subarray Sum', () => {
expect(fn([-2, 1, -3, 4, -1, 2, 1, -5, 4])).toEqual(6);
});

it('should work with small arrays', () => {
expect(fn([1, -3, 10, -5])).toEqual(10);
});

it('should work with large arrays', () => {
expect(fn(largeArray)).toEqual(4853);
});
Expand Down

0 comments on commit c1a8f8e

Please sign in to comment.