DEV Community

Discussion on: Day 2: Fixed point

Collapse
 
nickholmesde profile image
Nick Holmes

As a constructive critique of your solution:

A)

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.

Collapse
 
apomalyn profile image
Xavier Chretien

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 😃