DEV Community

loading...
Cover image for Reintroducing Code Review with an Interview Question Asked at Amazon
Coderbyte

Reintroducing Code Review with an Interview Question Asked at Amazon

cindyytong profile image Cindy Tong Updated on ・3 min read

Reintroducing Code Review

Hi there! I’m Cindy from Coderbyte, an online developer interview prep and learning platform. In celebration of Coderbyte reaching 500,000 developers, we’re relaunching our weekly code challenge series: Code Review. Every Thursday, we’ll launch a new coding challenge used in a real interview for free to Dev.to. Readers can submit their solutions in the comments section of each article for the opportunity to have their code reviewed by Codebyte and our readers. Let’s jump directly to this week’s challenge.

The Challenge

This week we’re focusing on an algorithms based question asked during an interview at Amazon. In this challenge, we’re asked to write a function ArrayAddition in Javascript that takes in an array of numbers stored in a variable named arr.

The function should return the string true if any combination of numbers in the array (excluding the largest number) can be added up to equal the largest number in the array.

Otherwise, the function should return the string false.

Examples:

  • If arr contains [ 4, 6, 23, 10, 1, 3 ] the output should return true because 23 is the largest number and 4 + 6 + 10 + 3 = 23.
  • If arr contains [ 5, 7, 16, 1, 2 ], the output should return false because none of the numbers add up to the largest number of 16.
  • If arr contains [ 3, 5, -1, 8, 12 ], the output should return true because 12 is the largest number and 5 + 8 - 1 = 12.

Some assumptions we can make:

  1. The arr will not be empty.
  2. The arr will not contain all the same elements but can contain some duplicate elements. For instance, the array cannot be [ 2, 2 ], however, it can be [ 2, 2, 4 ]
  3. The array may contain negative numbers.

Tips for Solving the Problem

Some things to consider when approaching this problem:

  1. Are there any edge cases we have not accounted for?
  2. How would you approach whiteboarding an approach before coding?
  3. What would a recursive solution look like and how would it compare to an iterative approach? If you need a refresher on recursion, check out our Master Recursion Youtube series.
  4. What is the time and space complexity of your solution? Check out our video guide on Big O for reference.

Once you have a solution that passes the test scenarios above, please share with us in the comments for a chance to be selected as this week’s winner.

More Resources

At Coderbyte, you can sign up for our free 10-day interview prep email course, access our challenge library of over one million solutions, learn through interview kits and starter courses, and practice with mock interviews. We also have more insights on career and interview prep on Medium as well as additional free video tutorials on Youtube.

About Me

I’ve worked in tech for over five years. My journey to engineering has been non-traditional. In my former lives, I’ve worked as a real estate broker, product manager, growth lead and UX designer. I’ve also taught full-stack development at App Academy, a coding bootcamp where I also learned how to code. Currently, outside of Coderbyte I work as a backend engineer at Knotch where we build a content intelligence platform used by Fortune 100 companies. My passion for making engineering careers accessible is what drew me to join the Coderbyte team. Feel free to reach out to me at cindy.tong@coderbyte.com if you have ideas on how we can improve this series.

Til Next Week

We’ll see you next Thursday where we will discuss various approaches to this problem and highlight some reader-submitted solutions.

Credits: Photo by ThisisEngineering RAEng on Unsplash

Discussion (3)

pic
Editor guide
Collapse
dbenchi profile image
David BENCHI

Hello,
I did not tried to optimize my solution (cause I really do not have time). But I really liked the idea of this challenge so I gave it a hit.

Here is my solution

Collapse
cindyytong profile image
Cindy Tong Author

@dbenchi
Hi David! Nice work here. Love that you went beyond the ask and even added the frontend for this. Some things to consider for the future:

  1. What happens if we call arrayAddition([1,1])?
  2. What if we wanted to eliminate complexity a bit and minimize the number of new arrays we're creating. How could this be solved without using flat() or filter()?

We'll be posting our approach to this problem tomorrow. Looking forward to seeing more solutions/thoughts from you throughout our series.

Collapse
dbenchi profile image
David BENCHI • Edited
  1. fixed
  2. we could do so by determining in advance the different combination we have. SO we may apply this combination on the original input directly.

Now I follow you on dev.to..... So I will try to answer new challenges if I have time.
Thanks a lot