|
| 1 | +## JavaScript Interview Questions |
| 2 | + |
| 3 | +<!-- *******************************--> |
| 4 | +<details> |
| 5 | +<summary>Reverse a string using promises [Company:Harman]</summary><br> |
| 6 | +<blockquote> |
| 7 | + |
| 8 | +Solution: |
| 9 | +``` |
| 10 | +async function reverseStr(str){ |
| 11 | + try { |
| 12 | + let arr = str.split(''); |
| 13 | + arr.reverse() |
| 14 | + return await arr.join(''); |
| 15 | + } catch (error) { |
| 16 | + return error.message |
| 17 | + } |
| 18 | + |
| 19 | +} |
| 20 | +console.log(reverseStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ").then((result)=>{ |
| 21 | + console.log(result) |
| 22 | +})); |
| 23 | +``` |
| 24 | +</blockquote><br> |
| 25 | +</details> |
| 26 | + |
| 27 | +<!-- *******************************--> |
| 28 | +<details> |
| 29 | +<summary>Design an algorithm to pick a random integer in the range [0, n - 1] that is not in the blacklist</summary><br> |
| 30 | +<blockquote> |
| 31 | + |
| 32 | +Problem:- |
| 33 | +``` |
| 34 | +You are given an integer n and an array of unique integers blacklist. Design an algorithm to pick a random integer in the range [0, n - 1] that is not in the blacklist. Any integer that is in the mentioned range and not in blacklist should be equally likely to be returned. |
| 35 | +Example:- |
| 36 | +
|
| 37 | +Explanation |
| 38 | +Solution solution = new Solution(7, [2, 3, 5]); |
| 39 | +solution.pick(); // return 0, any integer from [0,1,4,6] should be ok. Note that for every call of pick, |
| 40 | + // 0, 1, 4, and 6 must be equally likely to be returned (i.e., with probability 1/4). |
| 41 | +solution.pick(); // return 4 |
| 42 | +solution.pick(); // return 1 |
| 43 | +solution.pick(); // return 6 |
| 44 | +solution.pick(); // return 1 |
| 45 | +solution.pick(); // return 0 |
| 46 | +solution.pick(); // return 4 |
| 47 | +
|
| 48 | +Input |
| 49 | +["Solution", "pick", "pick", "pick", "pick", "pick", "pick", "pick"] |
| 50 | +[[7, [2, 3, 5]], [], [], [], [], [], [], []] |
| 51 | +Output |
| 52 | +[null, 0, 4, 1, 6, 1, 0, 4] |
| 53 | +
|
| 54 | +
|
| 55 | +Constraints: |
| 56 | +1 <= n <= 10^9 |
| 57 | +0 <= blacklist.length <= min(10^5, n - 1) |
| 58 | +0 <= blacklist[i] < n |
| 59 | +``` |
| 60 | + |
| 61 | +Solution: |
| 62 | +``` |
| 63 | +
|
| 64 | +``` |
| 65 | +</blockquote><br> |
| 66 | +</details> |
| 67 | + |
| 68 | +<!-- *******************************--> |
| 69 | +<details> |
| 70 | +<summary>Question</summary><br> |
| 71 | +<blockquote> |
| 72 | + |
| 73 | +Solution: |
| 74 | +``` |
| 75 | +
|
| 76 | +``` |
| 77 | +</blockquote><br> |
| 78 | +</details> |
| 79 | + |
| 80 | +<!-- *******************************--> |
| 81 | +<details> |
| 82 | +<summary>Question</summary><br> |
| 83 | +<blockquote> |
| 84 | + |
| 85 | +Solution: |
| 86 | +``` |
| 87 | +
|
| 88 | +``` |
| 89 | +</blockquote><br> |
| 90 | +</details> |
0 commit comments