Skip to content

Commit 8541031

Browse files
authored
Write-up for LeetCode problem 2727. Is Object Empty (#400)
1 parent 0064229 commit 8541031

16 files changed

+534
-5
lines changed

workspaces/javascript-leetcode-month/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ This directory contains the resources for the month of JavaScript on LeetCode, a
44

55
## Problem Order
66

7-
1. [2703. Return Length of Arguments Passed](https://leetcode.com/problems/return-length-of-arguments-passed/)
8-
2. [2727. Is Object Empty](https://leetcode.com/problems/is-object-empty/)
7+
1. [2703. Return Length of Arguments Passed](problems/2703-return-length-of-arguments-passed/)
8+
2. [2727. Is Object Empty](problems/2727-is-object-empty/)
99
3. [2677. Chunk Array](https://leetcode.com/problems/chunk-array/)
1010
4. [2635. Apply Transform Over Each Element in Array](https://leetcode.com/problems/apply-transform-over-each-element-in-array/)
1111
5. [2634. Filter Elements from Array](https://leetcode.com/problems/filter-elements-from-array/)

workspaces/javascript-leetcode-month/problems/2703-return-length-of-arguments-passed/solution.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# 2703. Return Length of Arguments Passed
22

3-
[View Problem on LeetCode](https://leetcode.com/problems/return-length-of-arguments-passed/)
3+
[View this Write-up on LeetCode](https://leetcode.com/problems/return-length-of-arguments-passed/solutions/5722508/content/) | [View Problem on LeetCode](https://leetcode.com/problems/return-length-of-arguments-passed/)
44

55
> [!WARNING]
6-
> This page includes spoilers. For a spoiler-free introduction to the problem, see the [README](README.md) file.
6+
> This page includes spoilers. For a spoiler-free introduction to the problem, see [the README file](README.md).
77
88
## Summary
99

@@ -114,7 +114,10 @@ We can also define new types, for example:
114114
type StringOrNumber = string | number;
115115
```
116116

117-
LeetCode's TypeScript template for this problem is unnecessarily complicated. It declares our rest parameter as an array of JSON values, and it defines a recursive type to express this! While this is interesting to illustrate the power of TypeScript, for this problem we don't care what arguments we get, we just care how many we get. Therefore I think it's sufficient to declare the arguments as being of type [`unknown`](https://www.typescriptlang.org/docs/handbook/2/functions.html#unknown). This is the safe way to say that we don't know the type. TypeScript will require us to test the type of the value before doing anything with it. In many LeetCode problems you will see `any`, which is the _unsafe_ way of saying we don't know the type. Unlike `unknown`, `any` allows us to do _anything_ with the value. It bypasses the typechecker for that section of code, and if we're not careful it can even leak to code that interacts with that section of code. It's best to avoid `any` as much as possible.
117+
LeetCode's TypeScript template for this problem is unnecessarily complicated. It declares our rest parameter as an array of JSON values, and it defines a recursive type to express this! While this is interesting to illustrate the power of TypeScript, for this problem we don't care what arguments we get, we just care how many we get. Therefore I think it's sufficient to declare the arguments as being of type [`unknown`](https://www.typescriptlang.org/docs/handbook/2/functions.html#unknown). This is the safe way to say that we don't know the type. TypeScript will require us to test the type of the value before doing anything with it.
118+
119+
> [!WARNING]
120+
> In many LeetCode problems you will see `any`, which is the _unsafe_ way of saying we don't know the type. Unlike `unknown`, `any` allows us to do _anything_ with the value. It bypasses the typechecker for that section of code, and if we're not careful it can even leak to code that interacts with that section of code. It's best to avoid `any` as much as possible.
118121
119122
We can additionally mark array arguments as [`readonly`](https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-func.html#readonly-and-const) when we don't intend to mutate them, as you'll see below.
120123

@@ -312,3 +315,6 @@ const argumentsLength = function (..._args: readonly unknown[]): number {
312315
console.log(enclosing("a", "b", "c")); // prints 3
313316
console.log(enclosing("a", "b")); // prints 2
314317
```
318+
319+
> [!TIP]
320+
> Thanks for reading! If you enjoyed this write-up, feel free to [up-vote it on LeetCode](https://leetcode.com/problems/return-length-of-arguments-passed/solutions/5722508/)! 🙏
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 2727. Is Object Empty
2+
3+
[View Problem on LeetCode](https://leetcode.com/problems/is-object-empty/)
4+
5+
Checking the length of an array is [straightforward](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length), but less so for an arbitrary object. However, perhaps there's a way to turn the object into an array of key-value pairs? Looking up how to do so will reveal one possible solution strategy.
6+
7+
The problem statement also mentions [`JSON.parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse), a function that parses a string into JSON data. What's the opposite of `JSON.parse`? If we could find a function that turns data into strings, it would probably be fairly clear from the resulting string if the original object was empty.
8+
9+
Both of the above strategies have one flaw, however. For a large object, turning it into an array or into a string will have linear time and space complexities. If you're an advanced JavaScript user, try to think of a solution that has constant complexities.
10+
11+
It will also probably be helpful to be able to check if a value is an array or an object. [The `typeof` operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) seems tempting, but unfortunately it won't work, because an array is still an object, so `typeof` on an array returns `"object"`. However, there is another function that does what we need. Search for it.
12+
13+
Once you've worked on the problem, check out [the full write-up and solution](solution.md)!

0 commit comments

Comments
 (0)