Array Wrapper - Leetcode 2695 - JavaScript 30-Day Challenge
Skills:
Systems Design Basics70%
Key Takeaways
The video demonstrates how to solve the Array Wrapper problem on LeetCode by creating a class that overloads operators and functions in JavaScript, specifically using the reduce method to sum an array of numbers and converting an array to a string with custom formatting.
Full Transcript
hey everyone welcome back and let's write some more neat code today so they'll solve the problem array wrapper we are creating a class array wrapper even though they made it a function and basically the premise of the problem is that we want to overload a couple operators for this class and it's easier to understand when you take a look at the example so here you can see that we're creating two instances of the array wrapper class passing in an array of numbers each time that's always going to be the input for this class that's why it's called array wrapper and I guess we're guaranteed that it's going to be numbers as well like that's the type of this array what's happening is kind of interesting here if you've never seen this before we're literally taking the plus operation between these two objects and somehow we're getting a number as the result so that's pretty weird if you've never seen it before but this is actually pretty common in other languages like C plus plus which is the first language I learned I think you can also do it in Python but I'm not super familiar with doing this type of stuff in Python but one language you definitely can't do it in is Java because Java is a pretty rigid language it doesn't give you that flexibility but C plus probably gives you too much flexibility at least for certain cases in this case we're not technically overloading the plus operation we're not giving it a different meaning what we're actually doing is overloading the value of method on this object and we're doing that by adding to the Prototype so what JavaScript is going to do before executing this statement it's going to get the value of this and the value of that and then add them together now normally for just a regular number like five plus five the value is the same thing itself and I think it's probably similar when you're dealing with strings as well so what we really want to do is more this array convert it into a single number within this function and in our case they told us in the problem description that this number should evaluate to the sum of this array this number should evaluate to the sum of this array so that itself is pretty simple before I get into the second thing that we want to implement because it's completely different let's actually finish implementing this portion so in our class the first thing we want to do is given that array of numbers just set it to a member variable and we can do that like this so that's what's making this a class even though it's a function that's why we can call the new keyword here that will initialize this I would much prefer though to write this with class syntax because I think it makes it more obvious but oh well and now to get the value of that array remember we do have access to this dot nums within this method even though this is not directly defined inside of the array wrapper and for that particular array of numbers we want to get the sum now you can't just call like a math function like sum that would be too easy JavaScript gives us a ton of flexibility but I guess it doesn't want to give us like a utility function like that so the easiest way to probably get the sum of this array is by calling reduce on it if you remember how that works it's a good a chance to review that when you call reduce we pass send two things we pass in a function a callback so I'm just going to Define like an anonymous function here and the second parameter is the accumulator in our case we want to accumulate the sum of these numbers and the initial value for the accumulator can be set to zero so that's the second parameter here now for our function itself one of the parameters is going to be a number from this array I'm going to call it n and the second parameter is going to be the accumulator which yes the initial value is zero but I'm going to call it a for short and what we want to return after iterating through each number is basically the sum of that number plus the accumulator so far so this will pretty much get the entire sum of these numbers stored in the accumulator and it will also return that from the reduce method so we can go ahead and just return at this point and that will be the sum and so now when we try to execute this line of code down here it will work as expected but the next part is going to be a bit more into interesting well I would actually say it's a bit more straightforward when you convert a string to an object we can also overload that method as well I think this actually is available in Java so this will not be super complicated what they told us though we want to do is for that array we want to print it in this format you might think well then from here can we literally just say convert string for this number array and then return that well we can't because when you convert an array like this to a string we actually get one two we get that without the outer bracket so what we want to do to this is just add the brackets on the outside and the easiest way to do that is using back ticks or you could also add a character at the beginning and a character at the end but I'm just going to use back text so we'll have an Open Bracket we want to say this string nums is not a string it's actually a variable so we can do that with a dollar sign curly braces and then close this off with the closing bracket so it's really as simple as that now we could also take these two strings and concatenate them together but that's obviously not a part of the problem but you can imagine how overloading these types of operators could be helpful but to be honest most of the time you probably don't want to do this because it can get kind of complex now let's run the code to make sure that it can work of course there's a bug we don't have a reference to nums we need to use our current context which is using the this keyword so now hopefully this works now as you can see yes it does and it's pretty efficient if you found this helpful please like And subscribe if you're preparing for coding interviews check out neatcode.io it has a ton of free resources to help you prepare thanks for watching and I'll see you soon
Original Description
Solving Day 28 of the Leetcode 30-day javascript challenge. Today we implement an array wrapper, but more specifically we overload some operators / functions of our class.
🚀 https://neetcode.io/ - A better way to prepare for Coding Interviews
🥷 Discord: https://discord.gg/ddjKRXPqtk
🐦 Twitter: https://twitter.com/neetcode1
🐮 Support the channel: https://www.patreon.com/NEETcode
⭐ BLIND-75 PLAYLIST: https://www.youtube.com/watch?v=KLlXCFG5TnA&list=PLot-Xpze53ldVwtstag2TL4HQhAnC8ATf
💡 DYNAMIC PROGRAMMING PLAYLIST: https://www.youtube.com/watch?v=73r3KWiEvyk&list=PLot-Xpze53lcvx_tjrr_m2lgD2NsRHlNO&index=1
Problem Link: https://leetcode.com/problems/array-wrapper/
0:00 - Read the problem
0:40 - Read examples
2:00 - Coding Solution
leetcode 2695
#neetcode #leetcode #python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeetCodeIO · NeetCodeIO · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Leetcode 149 - Maximum Points on a Line - Python
NeetCodeIO
Design Linked List - Leetcode 707 - Python
NeetCodeIO
Minimum Time to Collect All Apples in a Tree - Leetcode 1443 - Python
NeetCodeIO
Design Browser History - Leetcode 1472 - Python
NeetCodeIO
Number of Good Paths - Leetcode 2421 - Python
NeetCodeIO
Flip String to Monotone Increasing - Leetcode 926 - Python
NeetCodeIO
Maximum Sum Circular Subarray - Leetcode 918 - Python
NeetCodeIO
Find Closest Node to Given Two Nodes - Leetcode 2359 - Python
NeetCodeIO
Concatenated Words - Leetcode 472 - Python
NeetCodeIO
Data Stream as Disjoint Intervals - Leetcode 352 - Python
NeetCodeIO
LFU Cache - Leetcode 460 - Python
NeetCodeIO
N-th Tribonacci Number - Leetcode 1137
NeetCodeIO
Best Team with no Conflicts - Leetcode 1626 - Python
NeetCodeIO
Greatest Common Divisor of Strings - Leetcode 1071 - Python
NeetCodeIO
Shortest Path in a Binary Matrix - Leetcode 1091 - Python
NeetCodeIO
Insert into a Binary Search Tree - Leetcode 701 - Python
NeetCodeIO
Delete Node in a BST - Leetcode 450 - Python
NeetCodeIO
Shuffle the Array (Constant Space) - Leetcode 1470 - Python
NeetCodeIO
Fruits into Basket - Leetcode 904 - Python
NeetCodeIO
Number of Subarrays of size K and Average Greater than or Equal to Threshold - Leetcode 1343 Python
NeetCodeIO
Naming a Company - Leetcode 2306 - Python
NeetCodeIO
As Far from Land as Possible - Leetcode 1162 - Python
NeetCodeIO
Shortest Path with Alternating Colors - Leetcode 1129 - Python
NeetCodeIO
Minimum Fuel Cost to Report to the Capital - Leetcode 2477 - Python
NeetCodeIO
Count Odd Numbers in an Interval Range - Leetcode 1523 - Python
NeetCodeIO
Contains Duplicate II - Leetcode 219 - Python
NeetCodeIO
Path with Maximum Probability - Leetcode 1514 - Python
NeetCodeIO
Add to Array-Form of Integer - Leetcode 989 - Python
NeetCodeIO
Unique Paths II - Leetcode 63 - Python
NeetCodeIO
Minimum Distance between BST Nodes - Leetcode 783 - Python
NeetCodeIO
Design Hashmap - Leetcode 706 - Python
NeetCodeIO
Range Sum Query Immutable - Leetcode 303 - Python
NeetCodeIO
Binary Tree Zigzag Level Order Traversal - Leetcode 103 - Python
NeetCodeIO
Middle of the Linked List - Leetcode 876 - Python
NeetCodeIO
Course Schedule IV - Leetcode 1462 - Python
NeetCodeIO
Single Element in a Sorted Array - Leetcode 540 - Python
NeetCodeIO
Capacity to Ship Packages - Leetcode 1011 - Python
NeetCodeIO
IPO - Leetcode 502 - Python
NeetCodeIO
Minimize Deviation in Array - Leetcode 1675 - Python
NeetCodeIO
Longest Turbulent Array - Leetcode 978 - Python
NeetCodeIO
Last Stone Weight II - Leetcode 1049 - Python
NeetCodeIO
Construct Quad Tree - Leetcode 427 - Python
NeetCodeIO
Find Duplicate Subtrees - Leetcode 652 - Python
NeetCodeIO
Sort an Array - Leetcode 912 - Python
NeetCodeIO
Ones and Zeroes - Leetcode 474 - Python
NeetCodeIO
Remove Duplicates from Sorted Array II - Leetcode 80 - Python
NeetCodeIO
Maximum Twin Sum of a Linked List - Leetcode 2130 - Python
NeetCodeIO
Concatenation of Array - Leetcode 1929 - Python
NeetCodeIO
Symmetric Tree - Leetcode 101 - Python
NeetCodeIO
Check Completeness of a Binary Tree - Leetcode 958 - Python
NeetCodeIO
Construct Binary Tree from Inorder and Postorder Traversal - Leetcode 106 - Python
NeetCodeIO
Find Peak Element - Leetcode 162 - Python
NeetCodeIO
Accounts Merge - Leetcode 721 - Python
NeetCodeIO
Binary Tree Preorder Traversal (Iterative) - Leetcode 144 - Python
NeetCodeIO
Binary Tree Postorder Traversal (Iterative) - Leetcode 145 - Python
NeetCodeIO
Number of Zero-Filled Subarrays - Leetcode 2348 - Python
NeetCodeIO
Minimum Score of a Path Between Two Cities - Leetcode 2492 - Python
NeetCodeIO
Sqrt(x) - Leetcode 69 - Python
NeetCodeIO
Successful Pairs of Spells and Potions - Leetcode 2300 - Python
NeetCodeIO
Optimal Partition of String - Leetcode 2405 - Python
NeetCodeIO
More on: Systems Design Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Bloom Filters, Explained Properly
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Medium · Python
Chapters (3)
Read the problem
0:40
Read examples
2:00
Coding Solution
🎓
Tutor Explanation
DeepCamp AI