• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Array BinarySearch

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dears,

I was practicing a mock test in Enthuware and i came across this question.



Select correct statements.

Options:

a.This is not a valid Comparator implementation.
b.Arrays.binarySearch(sa, "cc", new MyStringComparator()); will return -2.
c.Arrays.binarySearch(sa, "c", new MyStringComparator()); will return 0.
d.Arrays.binarySearch(sa, "c", new MyStringComparator()); will return -1.
e.Arrays.binarySearch(sa, "c", new MyStringComparator()); will throw an exception.

My answer was b and d. But the correct answer is b and c.

I am not able to understand why c is the correct answer. As per my understanding the binarysearch would search for the position where the correct element would exist if its not present and will  calculate based on(-(index-1)
I am not sure if  i am missing something. Can you please help me understand the same.

Thanks in Advance!!
 
Bartender
Posts: 4667
183
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to your Comparator, "c" and "d" are considered the same, since their lengths are 1. So it finds "c" at location 0.
 
Priyanka Leo
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case if we search the single/ three/ Four charachter will it return the existing elements position. Is that what youy mean to say?
 
Priyanka Leo
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i also tried with the below Program:

String[] os = new String[]{"Mac", "Linux", "Windows"};
Arrays.sort(os);
System.out.println(Arrays.binarySearch(os, "Max"));

and i got the output as -3 am i missing something here ??
Can you please clariify i am really confused
 
Saloon Keeper
Posts: 13369
295
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we first sort the three elements, we end up with the following sequence: ["Linux", "Mac", "Windows"].

At what index would we put "Max" if we inserted it into this sequence? After "Mac", so at index 2.

Apply the formula to the insertion index: (-2 - 1) == -3.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic