If I had been the one interviewing and you solved it only for numeric input, my next question would have been, "How would you solve this problem for non-numeric input?" because I wouldn't be looking for mathematical cleverness. Instead, how about this?
List<String> options = new ArrayList<>(Arrays.asList("bob", "fred"));
options.remove("bob");
System.out.println(options.get(0));
That can obviously be easily adapted to any type, including Object
, so long as the equality of the objects works out correctly, and as a bonus, it can be expressed much more concisely in other languages, such as Groovy:
println((["bob", "fred"] - "bob").first())
The output, in either case, is obviously "fred". If I were the one interviewing, this is the answer I'd be looking for.