Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
When cloest is in the exact middle of two numbers

Alex

Jan 21, 2024

[50,30,70,10,40,60,90]

closest: 55

Output 60

Expected 50

In mathematics, when a number is exactly in the middle of two others, as 55 is between 50 and 60, it's customary to round up. Therefore, 55 is considered closer to 60. The test case and provided solution is wrong.

4

0

Comments
Comments
R
ray.laiwh a year ago

To cater for this, I manually calculate the rounded value as a workaround. Here is my solution:

class Solution { private: int closest_val_; double smallest_diff_; public: void Search(TreeNode *root, int target) { if (!root) return;...
C
Crandon Riordana year ago

I found the test cases to be quite annoying as well.

Design Gurus
Design Gurus6 months ago

To clarify: the problem mentions that "If there are multiple answers, print the smallest."