Skip to content

Commit ca46e80

Browse files
committed
Add challenges for 2023: day 1
1 parent 04d11a2 commit ca46e80

File tree

5 files changed

+137
-1
lines changed

5 files changed

+137
-1
lines changed

โ€Ž2023/challenge-01/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Challenge #1: ๐ŸŽ First Repeated Gift!
2+
3+
## ๐Ÿ”ข Instructions
4+
5+
In Santa's toy factory at the North Pole, each toy has a unique identification number.
6+
7+
However, due to a glitch in the toy-making machine, some numbers have been assigned to more than one toy.
8+
9+
Your task is to find the first identification number that has been repeated, where the second occurrence has the smallest index!
10+
11+
In simpler terms, if there are multiple repeated numbers, return the number whose second occurrence appears first in the list. If no numbers are repeated, return -1.
12+
13+
### Example:
14+
15+
```javascript
16+
const giftIds = [2, 1, 3, 5, 3, 2];
17+
const firstRepeatedId = findFirstRepeated(giftIds);
18+
console.log(firstRepeatedId); // 3
19+
// Although both 2 and 3 are repeated,
20+
// 3 appears first for the second time
21+
22+
const giftIds2 = [1, 2, 3, 4];
23+
const firstRepeatedId2 = findFirstRepeated(giftIds2);
24+
console.log(firstRepeatedId2); // -1
25+
// It's -1 as no number is repeated
26+
27+
const giftIds3 = [5, 1, 5, 1];
28+
const firstRepeatedId3 = findFirstRepeated(giftIds3);
29+
console.log(firstRepeatedId3); // 5
30+
```
31+
32+
Be cautious! The elves say that this is a Google technical test.

โ€Ž2023/challenge-01/solution.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function findFirstRepeated(gifts) {
2+
const seen = new Set();
3+
4+
return gifts.find((gift) => seen.has(gift) || !seen.add(gift)) || -1;
5+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { findFirstRepeated } from "./solution";
2+
3+
describe("Challenge #1: ๐ŸŽ First Repeated Gift!", () => {
4+
const testCases = [
5+
createTestCase(
6+
[2, 1, 3, 5, 3, 2],
7+
3,
8+
"should return the first repeated gift ID with the smallest second occurrence index"
9+
),
10+
createTestCase(
11+
[1, 2, 3, 4],
12+
-1,
13+
"should return -1 when no numbers are repeated"
14+
),
15+
createTestCase(
16+
[5, 1, 5, 1],
17+
5,
18+
"should return the first repeated gift ID when there are repeated numbers"
19+
),
20+
];
21+
22+
it.each(testCases)("#$# $description", ({ input, expected, description }) => {
23+
expect(findFirstRepeated(input)).toEqual(expected);
24+
});
25+
});
26+
27+
function createTestCase(input, expected, description) {
28+
return { input, expected, description };
29+
}

โ€Ž2023/challenge-04/README.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Box inside a box and another...
2+
3+
## ๐Ÿ”ข Instructions
4+
5+
Santa Claus needs to review his gift boxes to make sure he can pack them all in his sleigh. He has a series of **boxes of different sizes, characterized by their length, width, and height**.
6+
7+
Your task is to **write a function** that, given a list of boxes with their sizes, determines whether it is possible to pack **all the boxes in one so that each box contains another** (which in turn contains another, and so on).
8+
9+
Each box represents its measures with an object. For example: `{l: 2, w: 3, h: 2}`. This means that the box has a length of 2, a width of 3 and a height of 2.
10+
11+
A box fits into another box if all the sides of the first are smaller than the sides of the second. **The elves have told us that the boxes cannot be rotated**, so you cannot put a box of 2x3x2 in a box of 3x2x2. Let's see some examples:
12+
13+
```javascript
14+
fitsInOneBox([
15+
{ l: 1, w: 1, h: 1 },
16+
{ l: 2, w: 2, h: 2 }
17+
]); // true
18+
```
19+
20+
In the previous example, the smallest box fits into the largest box. Therefore, it is possible to pack all the boxes in one. Now let's see a case that does not:
21+
22+
```javascript
23+
const boxes = [
24+
{ l: 1, w: 1, h: 1 },
25+
{ l: 2, w: 2, h: 2 },
26+
{ l: 3, w: 1, h: 3 }
27+
];
28+
29+
fitsInOneBox(boxes); // false
30+
```
31+
32+
In the previous example, the smallest box fits into the middle box, but the middle box does not fit into the largest box. Therefore, it is not possible to pack all the boxes in one.
33+
34+
Note that the boxes may not come in order:
35+
36+
```javascript
37+
const boxes = [
38+
{ l: 1, w: 1, h: 1 },
39+
{ l: 3, w: 3, h: 3 },
40+
{ l: 2, w: 2, h: 2 }
41+
];
42+
43+
fitsInOneBox(boxes); // true
44+
```
45+
46+
In the previous example, the first box fits into the third, and the third into the second. Therefore, it is possible to pack all the boxes in one.
47+
48+
**Things to keep in mind**:
49+
50+
- The boxes cannot be rotated because the elves have told us that the machine is not ready.
51+
- The boxes may come in any order.
52+
- The boxes are not always squares, they could be rectangles.

โ€ŽREADME.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ This repository contains the solutions to the challenges proposed by [@midudev](
4343
| 24 | [Comparando รกrboles de Navidad](2021/24-arboles) | ๐ŸŸ  | [Show](2021/24-arboles/24-arboles.md) |
4444
| 25 | [El รบltimo juego y hasta el aรฑo que viene ๐Ÿ‘‹](2021/25-juego) | ๐ŸŸ  | [Show](2021/25-juego/25-juego.md) |
4545

46+
[^1]: **Difficulty**: ๐ŸŸข Easy ๐ŸŸ  Medium ๐Ÿ”ด Hard ๐ŸŸฃ Very Hard
4647
</details>
4748

4849
</br>
4950

5051
## ๐Ÿค– 2022
5152

52-
<details open>
53+
<details hide>
5354

5455
<summary>Show / Hide</summary>
5556

@@ -83,4 +84,21 @@ This repository contains the solutions to the challenges proposed by [@midudev](
8384

8485
[^1]: **Difficulty**: ๐ŸŸข Easy ๐ŸŸ  Medium ๐Ÿ”ด Hard ๐ŸŸฃ Very Hard
8586

87+
</details>
88+
89+
</br>
90+
91+
## ๐ŸŽ 2023
92+
93+
<details open>
94+
95+
<summary>Show / Hide</summary>
96+
97+
### ๐ŸŽฎ๏ธ Challenges
98+
| # | Challenge | Difficulty[^1] | Solution |
99+
| :-: | :-----------------------------------------------------------: | :------------: | :-----------------------------------: |
100+
| 01 | [First repeat gift!](2023/challenge-01) | ๐ŸŸข | [Show](2023/challenge-01/solution.js) |
101+
102+
[^1]: **Difficulty**: ๐ŸŸข Easy ๐ŸŸ  Medium ๐Ÿ”ด Hard ๐ŸŸฃ Very Hard
103+
86104
</details>

0 commit comments

Comments
ย (0)