feat(interview-prep): Transfering Rosetta problems from Curriculum (#34822)
This commit is contained in:
committed by
Kristofer Koishigawa
parent
3b5b0e22a7
commit
8584637254
@ -0,0 +1,71 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e03
|
||||
title: Cumulative standard deviation
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Write a function that takes an array of numbers as parameter and returns the <a href="https://en.wikipedia.org/wiki/Standard Deviation">standard deviation</a> of the series.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>standardDeviation</code> should be a function.
|
||||
testString: assert(typeof standardDeviation == 'function', '<code>standardDeviation</code> should be a function.');
|
||||
- text: <code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return a number.
|
||||
testString: assert(typeof standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]) == 'number', '<code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return a number.');
|
||||
- text: <code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return <code>2</code>.
|
||||
testString: assert.equal(standardDeviation([2, 4, 4, 4, 5, 5, 7, 9]), 2, '<code>standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])</code> should return <code>2</code>.');
|
||||
- text: <code>standardDeviation([600, 470, 170, 430, 300])</code> should return <code>147.323</code>.
|
||||
testString: assert.equal(standardDeviation([600, 470, 170, 430, 300]), 147.323, '<code>standardDeviation([600, 470, 170, 430, 300])</code> should return <code>147.323</code>.');
|
||||
- text: <code>standardDeviation([75, 83, 96, 100, 121, 125])</code> should return <code>18.239</code>.
|
||||
testString: assert.equal(standardDeviation([75, 83, 96, 100, 121, 125]), 18.239, '<code>standardDeviation([75, 83, 96, 100, 121, 125])</code> should return <code>18.239</code>.');
|
||||
- text: <code>standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82])</code> should return <code>16.87</code>.
|
||||
testString: assert.equal(standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82]), 16.87, '<code>standardDeviation([23, 37, 45, 49, 56, 63, 63, 70, 72, 82])</code> should return <code>16.87</code>.');
|
||||
- text: <code>standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314])</code> should return <code>22.631</code>.
|
||||
testString: assert.equal(standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314]), 22.631, '<code>standardDeviation([271, 354, 296, 301, 333, 326, 285, 298, 327, 316, 287, 314])</code> should return <code>22.631</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function standardDeviation (arr) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function standardDeviation (arr) {
|
||||
var sum = 0,
|
||||
sum_sq = 0,
|
||||
n = arr.length;
|
||||
arr.forEach(function(e) {
|
||||
sum += e;
|
||||
sum_sq += e * e;
|
||||
})
|
||||
|
||||
var std_dev=Math.sqrt((sum_sq / n) - Math.pow(sum / n, 2))
|
||||
return Math.round(std_dev*1000)/1000;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,92 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e05
|
||||
title: CUSIP
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
A <b>CUSIP</b> is a nine-character alphanumeric code that identifies a North American financial security for the purposes of facilitating clearing and settlement of trades. The CUSIP was adopted as an American National Standard under Accredited Standards X9.6.
|
||||
Write a function that takes a string as a parameter and checks if the string is valid CUSIP.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>isCusip</code> should be a function.
|
||||
testString: assert(typeof isCusip == 'function', '<code>isCusip</code> should be a function.');
|
||||
- text: <code>isCusip("037833100")</code> should return a boolean.
|
||||
testString: assert(typeof isCusip("037833100") == 'boolean', '<code>isCusip("037833100")</code> should return a boolean.');
|
||||
- text: <code>isCusip("037833100")</code> should return <code>true</code>.
|
||||
testString: assert.equal(isCusip("037833100"), true, '<code>isCusip("037833100")</code> should return <code>true</code>.');
|
||||
- text: <code>isCusip("17275R102")</code> should return <code>true</code>.
|
||||
testString: assert.equal(isCusip("17275R102"), true, '<code>isCusip("17275R102")</code> should return <code>true</code>.');
|
||||
- text: <code>isCusip("38259P50a")</code> should return <code>false</code>.
|
||||
testString: assert.equal(isCusip("38259P50a"), false, '<code>isCusip("38259P50a")</code> should return <code>false</code>.');
|
||||
- text: <code>isCusip("38259P508")</code> should return <code>true</code>.
|
||||
testString: assert.equal(isCusip("38259P508"), true, '<code>isCusip("38259P508")</code> should return <code>true</code>.');
|
||||
- text: <code>isCusip("38259P50#")</code> should return <code>false</code>.
|
||||
testString: assert.equal(isCusip("38259P50#"), false, '<code>isCusip("38259P50#")</code> should return <code>false</code>.');
|
||||
- text: <code>isCusip("68389X105")</code> should return <code>true</code>.
|
||||
testString: assert.equal(isCusip("68389X105"), true, '<code>isCusip("68389X105")</code> should return <code>true</code>.');
|
||||
- text: <code>isCusip("68389X106")</code> should return <code>false</code>.
|
||||
testString: assert.equal(isCusip("68389X106"), false, '<code>isCusip("68389X106")</code> should return <code>false</code>.');
|
||||
- text: <code>isCusip("5949181")</code> should return <code>false</code>.
|
||||
testString: assert.equal(isCusip("5949181"), false, '<code>isCusip("5949181")</code> should return <code>false</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function isCusip (s) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function isCusip (s) {
|
||||
if (s.length != 9) return false;
|
||||
var sum = 0;
|
||||
var ASCII = x => x.charCodeAt(0);
|
||||
for (var i = 0; i < 7; i++) {
|
||||
var c = s.charCodeAt(i);
|
||||
|
||||
var v;
|
||||
if (c >= ASCII('0') && c <= ASCII('9')) {
|
||||
v = c - 48;
|
||||
} else if (c >= ASCII('A') && c <= ASCII('Z')) {
|
||||
v = c - 64; // lower case letters apparently invalid
|
||||
} else if (c == ASCII('*')) {
|
||||
v = 36;
|
||||
} else if (c == ASCII('@')) {
|
||||
v = 37;
|
||||
} else if (c == ASCII('#')) {
|
||||
v = 38;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
if (i % 2 == 1) v *= 2; // check if odd as using 0-based indexing
|
||||
sum += Math.floor(v / 10) + v % 10;
|
||||
}
|
||||
return s.charCodeAt(8) - 48 == (10 - (sum % 10)) % 10;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,113 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e06
|
||||
title: Cut a rectangle
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
A given rectangle is made from <i>m</i> × <i>n</i> squares. If <i>m</i> and <i>n</i> are not both odd, then it is possible to cut a path through the rectangle along the square edges such that the rectangle splits into two connected pieces with the same shape (after rotating one of the pieces by 180°). All such paths for 2 × 2 and 4 × 3 rectangles are shown below.
|
||||
<a href="http://rosettacode.org/wiki/file:rect-cut.svg">file:rect-cut.svg</a>
|
||||
Write a function that calculates the number of different ways to cut an <i>m</i> × <i>n</i> rectangle.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>cutRectangle</code> should be a function.
|
||||
testString: assert(typeof cutRectangle == 'function', '<code>cutRectangle</code> should be a function.');
|
||||
- text: <code>cutRectangle(2, 2)</code> should return a number.
|
||||
testString: assert(typeof cutRectangle(2, 2) == 'number', '<code>cutRectangle(2, 2)</code> should return a number.');
|
||||
- text: <code>cutRectangle(2, 2)</code> should return <code>2</code>.
|
||||
testString: assert.equal(cutRectangle(2, 2), 2, '<code>cutRectangle(2, 2)</code> should return <code>2</code>.');
|
||||
- text: <code>cutRectangle(4, 3)</code> should return <code>9</code>.
|
||||
testString: assert.equal(cutRectangle(4, 3), 9, '<code>cutRectangle(4, 3)</code> should return <code>9</code>.');
|
||||
- text: <code>cutRectangle(4, 4)</code> should return <code>22</code>.
|
||||
testString: assert.equal(cutRectangle(4, 4), 22, '<code>cutRectangle(4, 4)</code> should return <code>22</code>.');
|
||||
- text: <code>cutRectangle(8, 3)</code> should return <code>53</code>.
|
||||
testString: assert.equal(cutRectangle(8, 3), 53, '<code>cutRectangle(8, 3)</code> should return <code>53</code>.');
|
||||
- text: <code>cutRectangle(7, 4)</code> should return <code>151</code>.
|
||||
testString: assert.equal(cutRectangle(7, 4), 151, '<code>cutRectangle(7, 4)</code> should return <code>151</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function cutRectangle (w, h) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function cutRectangle (w, h) {
|
||||
if (w % 2 == 1 && h % 2 == 1)
|
||||
return;
|
||||
|
||||
var dirs = [[0, -1], [-1, 0], [0, 1], [1, 0]];
|
||||
|
||||
var grid = new Array(h); for (var i = 0; i < grid.length; i++) grid[i]=new Array(w);
|
||||
var stack = [];
|
||||
|
||||
var half = Math.floor((w * h) / 2);
|
||||
var bits = Math.pow(2, half) - 1;
|
||||
var result=0;
|
||||
for (; bits > 0; bits -= 2) {
|
||||
|
||||
for (var i = 0; i < half; i++) {
|
||||
var r = Math.floor(i / w);
|
||||
var c = i % w;
|
||||
grid[r][c] = (bits & (1 << i)) != 0 ? 1 : 0;
|
||||
grid[h - r - 1][w - c - 1] = 1 - grid[r][c];
|
||||
}
|
||||
|
||||
stack.push(0);
|
||||
grid[0][0] = 2;
|
||||
var count = 1;
|
||||
while (stack.length!=0) {
|
||||
|
||||
var pos = stack.pop();
|
||||
var r = Math.floor(pos / w);
|
||||
var c = pos % w;
|
||||
|
||||
for (var dir of dirs) {
|
||||
var nextR = r + dir[0];
|
||||
var nextC = c + dir[1];
|
||||
|
||||
if (nextR >= 0 && nextR < h && nextC >= 0 && nextC < w) {
|
||||
|
||||
if (grid[nextR][nextC] == 1) {
|
||||
stack.push(nextR * w + nextC);
|
||||
grid[nextR][nextC] = 2;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count == half) {
|
||||
result++;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
@ -0,0 +1,65 @@
|
||||
---
|
||||
id: 5a23c84252665b21eecc7e1e
|
||||
title: Dot product
|
||||
challengeType: 5
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Create a function, to compute the <b><a href="https://en.wikipedia.org/wiki/Dot product">dot product</a></b>, also known as the <b>scalar product</b> of two vectors.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>dotProduct</code> should be a function.
|
||||
testString: assert(typeof dotProduct == 'function', '<code>dotProduct</code> should be a function.');
|
||||
- text: <code>dotProduct([1, 3, -5], [4, -2, -1])</code> should return a number.
|
||||
testString: assert(typeof dotProduct([1, 3, -5], [4, -2, -1]) == 'number', '<code>dotProduct([1, 3, -5], [4, -2, -1])</code> should return a number.');
|
||||
- text: <code>dotProduct([1, 3, -5], [4, -2, -1])</code> should return <code>3</code>.
|
||||
testString: assert.equal(dotProduct([1, 3, -5], [4, -2, -1]), 3, '<code>dotProduct([1, 3, -5], [4, -2, -1])</code> should return <code>3</code>.');
|
||||
- text: <code>dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])</code> should return <code>130</code>.
|
||||
testString: assert.equal(dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]), 130, '<code>dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])</code> should return <code>130</code>.');
|
||||
- text: <code>dotProduct([5, 4, 3, 2], [7, 8, 9, 6])</code> should return <code>106</code>.
|
||||
testString: assert.equal(dotProduct([5, 4, 3, 2], [7, 8, 9, 6]), 106, '<code>dotProduct([5, 4, 3, 2], [7, 8, 9, 6])</code> should return <code>106</code>.');
|
||||
- text: <code>dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6])</code> should return <code>-36</code>.
|
||||
testString: assert.equal(dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6]), -36, '<code>dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6])</code> should return <code>-36</code>.');
|
||||
- text: <code>dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110])</code> should return <code>10392</code>.
|
||||
testString: assert.equal(dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110]), 10392, '<code>dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110])</code> should return <code>10392</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
function dotProduct (ary1, ary2) {
|
||||
// Good luck!
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
function dotProduct (ary1, ary2) {
|
||||
var dotprod = 0;
|
||||
for (var i = 0; i < ary1.length; i++)
|
||||
dotprod += ary1[i] * ary2[i];
|
||||
return dotprod;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
Reference in New Issue
Block a user