Skip to content

Commit 5d65055

Browse files
authored
Merge pull request #103 from pertrai1/gfe-12242025
GFE Deep Clone Problem
2 parents d6c9a54 + 06940e3 commit 5d65055

File tree

7 files changed

+70
-17
lines changed

7 files changed

+70
-17
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A comprehensive collection of coding challenges from multiple platforms for lear
77
| Platform | Focus Area | Problems Solved |
88
| -------------------------------- | ---------------------------- | --------------- |
99
| [LeetCode](#-leetcode) | Data Structures & Algorithms | 166 |
10-
| [GreatFrontEnd](#-greatfrontend) | Frontend Engineering | 7 |
10+
| [GreatFrontEnd](#-greatfrontend) | Frontend Engineering | 8 |
1111

1212
## 🎯 Platforms
1313

@@ -43,7 +43,7 @@ A comprehensive collection of coding challenges from multiple platforms for lear
4343

4444
**Progress**:
4545

46-
- GFE 75: 4/75 problems
46+
- GFE 75: 5/75 problems
4747
- Blind 75: 3/75 problems
4848

4949
**Quick Links**:

greatfrontend/README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ Solutions to GreatFrontEnd coding challenges, focusing on frontend engineering,
88
- **[GFE 75/](./gfe-75/)** - GreatFrontEnd's curated GFE 75 collection
99
- **javascript/** - JavaScript-specific challenges (coming soon)
1010

11-
## 📊 Progress
12-
13-
- **Blind 75**: 3/75 completed
14-
- **GFE 75**: 4/75 completed
15-
16-
Both collections are organized by category for easier pattern recognition and learning.
17-
1811
## 🔗 Resources
1912

2013
- [GreatFrontEnd Platform](https://www.greatfrontend.com/)

greatfrontend/gfe-75/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,34 @@ The GFE 75 is GreatFrontEnd's curated collection of 75 essential frontend coding
44

55
## 📊 Progress Tracker
66

7-
### Status: 4 / 75 problems completed
7+
### Status: 5 / 75 problems completed
88

99
## ✅ Completed Problems
1010

11-
### JavaScript Utilities (2/20)
11+
### JavaScript Utilities
1212

1313
- [Debounce](./javascript-utilities/debounce/) - Medium
1414
- [Flatten](./javascript-utilities/flatten/) - Medium
1515
- [Throttle](./javascript-utilities/throttle/) - Medium
16+
- [Deep Clone](./javascript-utilities/deep-clone/) - Medium
1617

17-
### DOM Manipulation (0/15)
18+
### DOM Manipulation
1819

1920
No problems completed yet
2021

21-
### Async Programming (1/12)
22+
### Async Programming
2223

2324
- [Promise.all](./async-programming/promise-all/) - Medium
2425

25-
### Components (0/10)
26+
### Components
2627

2728
No problems completed yet
2829

29-
### Data Structures (0/10)
30+
### Data Structures
3031

3132
No problems completed yet
3233

33-
### Algorithms (0/8)
34+
### Algorithms
3435

3536
No problems completed yet
3637

greatfrontend/gfe-75/javascript-utilities/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ Essential JavaScript utility functions that are commonly asked in frontend inter
99
- [Debounce](./debounce/) - Medium
1010
- [Flatten](./flatten/) - Medium
1111
- [Throttle](./throttle/) - Medium
12+
- [Deep Clone](./deep-clone/) - Medium
1213

1314
### To Do 📝
1415

1516
- **Memoize** - Medium - Cache function results
1617
- **Curry** - Medium - Transform multi-argument function
1718
- **Partial** - Medium - Pre-fill function arguments
18-
- **Deep Clone** - Medium - Clone nested objects/arrays
1919
- **Deep Equal** - Medium - Compare nested objects
2020
- **Get** - Easy - Access nested object properties safely
2121
- **Set** - Medium - Set nested object properties
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Deep Clone
2+
3+
The term "deep clone" is not formally defined in JavaScript's language specification, but is generally well understood in the community. A deep clone makes a copy of JavaScript value, leading to a completely new value that has no references pointing back to the properties in the original object (if it's an object). Any changes made to the deep-copied object will not affect the original object.
4+
5+
Implement a deepClone function that performs a deep clone operation on JavaScript objects. You can assume the input only contains JSON-serializable values (`null`, `boolean`, `number`, `string`, `Array`, `Object`) and will not contain any other objects like `Date`, `Regex`, `Map` or `Set`.
6+
7+
## Examples
8+
9+
```JavaScript
10+
const obj1 = { user: { role: 'admin' } };
11+
const clonedObj1 = deepClone(obj1);
12+
13+
clonedObj1.user.role = 'guest'; // Change the cloned user's role to 'guest'.
14+
clonedObj1.user.role; // 'guest'
15+
obj1.user.role; // Should still be 'admin'.
16+
17+
const obj2 = { foo: [{ bar: 'baz' }] };
18+
const clonedObj2 = deepClone(obj2);
19+
20+
obj2.foo[0].bar = 'bax'; // Modify the original object.
21+
obj2.foo[0].bar; // 'bax'
22+
clonedObj2.foo[0].bar; // Should still be 'baz'.
23+
```
24+
25+
GFE Url: [https://www.greatfrontend.com/interviews/study/gfe75/questions/javascript/deep-clone](https://www.greatfrontend.com/interviews/study/gfe75/questions/javascript/deep-clone)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { deepClone } from './deep-clone';
3+
4+
describe('deepClone', () => {
5+
it('primitive values', () => {
6+
expect(deepClone('foo')).toEqual('foo');
7+
expect(deepClone(123)).toEqual(123);
8+
});
9+
10+
it('objects', () => {
11+
const obj = { role: 'foo' };
12+
const clonedObj = deepClone(obj);
13+
clonedObj.role = 'bar';
14+
expect(obj).toEqual({ role: 'foo' });
15+
});
16+
17+
it('nested objects', () => {
18+
const obj = { user: { role: 'admin', id: '123' } };
19+
const clonedObj = deepClone(obj);
20+
clonedObj.user.role = 'bar';
21+
expect(obj).toEqual({ user: { role: 'admin', id: '123' } });
22+
});
23+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function deepClone<T>(value: T): T {
2+
if (typeof value !== 'object' || value === null) {
3+
return value;
4+
}
5+
if (Array.isArray(value)) {
6+
return value.map((val) => deepClone(val)) as T;
7+
}
8+
return Object.fromEntries(
9+
Object.entries(value).map(([key, val]) => [key, deepClone(val)])
10+
) as T;
11+
}

0 commit comments

Comments
 (0)