diff --git a/book/D-interview-questions-solutions.asc b/book/D-interview-questions-solutions.asc index dd2d8a54..c0ddb13f 100644 --- a/book/D-interview-questions-solutions.asc +++ b/book/D-interview-questions-solutions.asc @@ -90,8 +90,7 @@ Algorithm: [source, javascript] ---- -include::interview-questions/buy-sell-stock.js[tag=description] -include::interview-questions/buy-sell-stock.js[tag=solution] +include::interview-questions/buy-sell-stock.js[tag=description,solution] ---- The runtime is `O(n)` and space complexity of `O(1)`. @@ -126,8 +125,7 @@ Another case to take into consideration is that lists might have different lengt [source, javascript] ---- -include::interview-questions/merge-lists.js[tag=description] -include::interview-questions/merge-lists.js[tag=solution] +include::interview-questions/merge-lists.js[tag=description,solution] ---- Notice that we used a "dummy" node or "sentinel node" to have some starting point for the final list. @@ -172,8 +170,7 @@ A better way to solve this problem is iterating over each character on both list [source, javascript] ---- -include::interview-questions/linkedlist-same-data.js[tag=description] -include::interview-questions/linkedlist-same-data.js[tag=solution] +include::interview-questions/linkedlist-same-data.js[tag=description,solution] ---- The function `findNextPointerIndex` is a helper to navigate each character on a linked list. diff --git a/book/content/part02/array.asc b/book/content/part02/array.asc index e03863e5..fbaff07b 100644 --- a/book/content/part02/array.asc +++ b/book/content/part02/array.asc @@ -276,13 +276,15 @@ To sum up, the time complexity of an array is: |=== //end::table -==== Interview Questions +==== Practice Questions (((Interview Questions, Arrays))) // tag::array-q-max-subarray[] ===== Max Subarray *AR-1*) _Given an array of integers, find the maximum sum of consecutive elements (subarray)._ + +_Seen in interviews at: Amazon, Apple, Google, Microsoft, Facebook_ // end::array-q-max-subarray[] [source, javascript] @@ -298,6 +300,8 @@ _Solution: <>_ ===== Best Time to Buy and Sell an Stock *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)_ + +_Seen in interviews at: Amazon, Facebook, Bloomberg_ // end::array-q-buy-sell-stock[] [source, javascript] diff --git a/book/content/part02/linked-list.asc b/book/content/part02/linked-list.asc index 71b68c82..329db496 100644 --- a/book/content/part02/linked-list.asc +++ b/book/content/part02/linked-list.asc @@ -286,13 +286,15 @@ Use a doubly linked list when: For the next two linear data structures <> and <>, we are going to use a doubly-linked list to implement them. We could use an array as well, but since inserting/deleting from the start performs better with linked-lists, we will use that. -==== Interview Questions +==== Practice Questions (((Interview Questions, Linked Lists))) // tag::linkedlist-q-merge-lists[] ===== Merge Linked Lists into One *LL-1*) _Merge two sorted lists into one (and keep them sorted)_ + +_Seen in interviews at: Amazon, Adobe, Microsoft, Google_ // end::linkedlist-q-merge-lists[] [source, javascript] @@ -311,6 +313,8 @@ _Solution: <>_ ===== Check if two strings lists are the same *LL-2*) _Given two linked lists with strings, check if are the same_ + +_Seen in interviews at: Facebook_ // end::linkedlist-q-linkedlist-same-data[] [source, javascript] diff --git a/book/content/part02/queue.asc b/book/content/part02/queue.asc index 3dd78caf..90e88288 100644 --- a/book/content/part02/queue.asc +++ b/book/content/part02/queue.asc @@ -91,8 +91,21 @@ indexterm:[Runtime, Linear] ===== Recent Counter *QU-1*) _Design a class that counts the most recent requests within a time window._ -// end::queue-q-recent-counter[] + +Example: + +[source, javascript] +---- +const counter = new RecentCounter(10); // The time window is 10 ms. +counter.request(1000); // 1 (first request, it always counts) +counter.request(3000); // 1 (last requests was 2000 ms ago, > 10ms, so doesn't count) +counter.request(3100); // 1 (last requests was 100 ms ago, > 10ms, so doesn't count) +counter.request(3105); // 2 (last requests was 5 ms ago, <= 10ms, so it counts) +---- + _Seen in interviews at: Google, Bloomberg, Yandex_ +// end::queue-q-recent-counter[] + [source, javascript] ---- @@ -108,9 +121,9 @@ _Solution: <>_ ===== 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 game over._ -// end::queue-q-design-snake-game[] _Seen in interviews at: Amazon, Bloomberg, Apple_ +// end::queue-q-design-snake-game[] [source, javascript] ---- diff --git a/book/content/part02/stack.asc b/book/content/part02/stack.asc index ae00254b..f0a1a381 100644 --- a/book/content/part02/stack.asc +++ b/book/content/part02/stack.asc @@ -87,13 +87,15 @@ Implementing the stack with an array and linked list would lead to the same time It's not very common to search for values on a stack (other Data Structures are better suited for this). Stacks are especially useful for implementing <>. -==== Interview Questions +==== Practice Questions (((Interview Questions, Stack))) // tag::stack-q-valid-parentheses[] ===== Validate Parentheses / Braces / Brackets *ST-1*) _Given an string with 3 types of brakets: `()`, `{}`, and `[]`. Validate they are properly closed and opened._ + +_Seen in interviews at: Amazon, Bloomberg, Facebook, Citadel_ // end::stack-q-valid-parentheses[] [source, javascript] @@ -113,6 +115,8 @@ _Solution: <>_ ===== Daily Temperaturs *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._ + +_Seen in interviews at: Amazon, Adobe, Cisco_ // end::stack-q-daily-temperatures[] [source, javascript] diff --git a/book/interview-questions/recent-counter.js b/book/interview-questions/recent-counter.js index 49c685e9..052e27f6 100644 --- a/book/interview-questions/recent-counter.js +++ b/book/interview-questions/recent-counter.js @@ -8,20 +8,13 @@ const { Queue } = require('../../src/index'); * any requests that happened more than 2 seconds before the most recent request * should not count. * - * @example - The time window is 10 ms. - * const counter = new RecentCounter(10); - * counter.request(1000); // 1 (first request, it counts) - * counter.request(3000); // 1 (last requests was 2000 ms ago, > 10ms, so doesn't count) - * counter.request(3100); // 1 (last requests was 100 ms ago, > 10ms, so doesn't count) - * counter.request(3105); // 2 (last requests was 5 ms ago, <= 10ms, so it counts) - * * @example - The time window is 3 sec. (3000 ms) * const counter = new RecentCounter(3000); * counter.request(100); // 1 * counter.request(1000); // 2 * counter.request(3000); // 3 * counter.request(3100); // 4 - * counter.request(3101); // 4 (request at time 100 is out of the 3000 window). + * counter.request(3101); // 4 * */ class RecentCounter {