Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feedback #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

thuyanduong
Copy link
Member

No description provided.

Copy link
Member Author

@thuyanduong thuyanduong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yasirah-tech This submission is very good! I'm going to ask you to make minor changes because I know you can be even better! Take a look at the feedback below. Update your two short responses and try refactoring your one coding problem.

When you are done, push up your changes and resubmit the URL to Canvas!

@@ -10,9 +10,15 @@ Use Test Driven Development to guide you. Run `npm install` to download dependen
### Short Response Questions

**1. What is a Hash Map and how does it work?** Be sure to mention the terms "hash key" and "hash function" in your explanation.

> A `Hash Map` is a data structure that maps key-value pairs (similar to an object), and stores these pairs within an `Array()`. Unlike `Hash Tables` (which also have a key-value pair functionality) where the `keys` can only be an integer or a string, `Hash Map`'s can be of any data type. `Hash Map`s can be implemented by creating a `hash()` function that excepts a `key` and a `value` as input and the `key` is used as the `hash key` for accessing the index associated with the `key` that is stored at it's specified `index` when it is added to the `Hash Table`. `Hash Maps`' perform lookup, insertion, and deletion operations efficiently and therefore usually have a constant run time of O(1)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good! One thing I would clarify though. A hash function accepts a value and returns a key which is used as the hash key. A hash function does not accept a value and key like you stated. Update your answer.


**2. What are the benefits of using a Hash Map rather than storing all of our data in an Array?**

> > The benefit of using A `Hash Map` rather than storing all of your data in an `array` is the difference in runtime (time complexity) and the limit to the different data types that can be present in the `array`. An `array` can only consist of items of the same data type, where `Hash Map`'s can store any `key`-`value` pairs. A `Hash Map` can be as complex to where you can assign a `key` to be a string `""` and its value to be an object `{}` with its own set of `key`-`value` pairs.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, a very good explanation! To be clear though, arrays can only consist of items of the same data type is true in some programming languages. In JavaScript, arrays can contain a variety of different data types. Update your answer.

sum += nums[i];
}

return 2 * uniqueElementSum - sum;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very clever use of math to solve the problem in an efficient manner!

Comment on lines 23 to +40
const canConstruct = (ransomNote, magazine) => {

};

if (ransomNote.length > magazine.length) return false;
let charMap = [];
for (let i = 0; i < ransomNote.length; i++) {
if (magazine.indexOf(ransomNote[i]) == -1)
return false;
let char = ransomNote.codePointAt(i);
charMap[char] = charMap[char] - 1 || -1;
}

for (let i = 0; i < magazine.length; i++) {
let char = magazine.codePointAt(i);
charMap[char] = charMap[char] + 1 || 1;
}
for (const charCount of charMap) if (charCount < 0) return false;
return true

}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this works, but I'm going to push you to implement a more efficient algorithm! indexOf has a linear runtime, and because it's nested inside a for loop, your current algorithm is quadratic runtime O(n^2). Read this article on how to use a hashmap to implement a truly linear algorithm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants