Time flies while you're having fun, but then one day your bio says something about being a 30+ year veteran in software engineering. Still, I've not seen it all, let alone done it all (yet).
if(array[1] < array[0]) throw IllegalArgumentException("Array not sorted")
You are claiming the array is not sorted if the first two elements are not sorted. What about the array [1, 3, 2] or [1, 1, 2]? Also, what if the array is of length 0 or 1?
You weren't asked to validate that the array was sorted, so you could simply remove this line.
B)
return pointFixedFound.toList()
You are asked to return either an integer or a false, but you are returning a list. Although your list contains the answer, if this was an interview question, I would mark you down on this - demonstrating the ability to extract all detail out of a provided specification is important.
C)
Finally, given that this is coding challenge, and that we are told that input list is sorted and distinct, I think a "more optimal" solution is evident;
as soon as you find a cell value equal to the index, just return the value - no need to go to the end of the list.
as soon as you find a cell value greater than the index, just return false - there can be no fixed points from here on.
A) About the first line, you are right I could simply remove that line.
B) In an interview, I would probably follow the problem asked without any modification. In that case, I choose to return every fixed point on the given array but you are right I should just respect the problem.
C) I will update the post with your proposition
Once again thank you I really appreciate your comment 😃
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
As a constructive critique of your solution:
A)
You are claiming the array is not sorted if the first two elements are not sorted. What about the array [1, 3, 2] or [1, 1, 2]? Also, what if the array is of length 0 or 1?
You weren't asked to validate that the array was sorted, so you could simply remove this line.
B)
You are asked to return either an integer or a false, but you are returning a list. Although your list contains the answer, if this was an interview question, I would mark you down on this - demonstrating the ability to extract all detail out of a provided specification is important.
C)
Finally, given that this is coding challenge, and that we are told that input list is sorted and distinct, I think a "more optimal" solution is evident;
as soon as you find a cell value equal to the index, just return the value - no need to go to the end of the list.
as soon as you find a cell value greater than the index, just return false - there can be no fixed points from here on.
First, thank you for your critique! 😃
A) About the first line, you are right I could simply remove that line.
B) In an interview, I would probably follow the problem asked without any modification. In that case, I choose to return every fixed point on the given array but you are right I should just respect the problem.
C) I will update the post with your proposition
Once again thank you I really appreciate your comment 😃