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.
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
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
[ 4, 6, 23, 10, 1, 3 ]the output should return true because
23is the largest number and
4 + 6 + 10 + 3 = 23.
[ 5, 7, 16, 1, 2 ], the output should return false because none of the numbers add up to the largest number of
[ 3, 5, -1, 8, 12 ], the output should return true because
12is the largest number and
5 + 8 - 1 = 12.
Some assumptions we can make:
arrwill not be empty.
arrwill 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 ]
- The array may contain negative numbers.
Some things to consider when approaching this problem:
- Are there any edge cases we have not accounted for?
- How would you approach whiteboarding an approach before coding?
- 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.
- 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.
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.
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 email@example.com if you have ideas on how we can improve this series.
We’ll see you next Thursday where we will discuss various approaches to this problem and highlight some reader-submitted solutions.