Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9221961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:42:06+00:00 2026-06-18T03:42:06+00:00

I’m currently writing an interview question for a java expert profile . Here it

  • 0

I’m currently writing an interview question for a java expert profile. Here it is:


Considering this code :

listing 1

package com.example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Searching {
    public static void main(String[] args) {
        int input = Integer.valueOf(args[0]);
        String[] strings = {"1", "2", "4", "8", "16", "32", "64", "128"};
        List<Integer> integers = new ArrayList<Integer>();
        for (String s : strings) {
            integers.add(Integer.valueOf(s));
        }
        System.out.println("index of "+input+" is:"+Collections.binarySearch(integers, input, cmp));
    }

    static Comparator<Integer> cmp = new Comparator<Integer>() {
        public int compare(Integer i, Integer j) {
            return i < j ? -1 : (i == j ? 0 : 1);
        }
    };
}

This code is then compiled with this cmd line

listing 2

javac com/example/Searching.java

and run with this command line

listing 3

java com/example/Searching 128

QUESTION A:

Executing listing 3 produce:

index of 128 is:-8

Can you explain this output ?

QUESTION B:

Considering this

 java com/example/Searching 32

output is

index of 32 is:5

Can you explain this output ?

QUESTION C:

Assuming you have a JRE 1.6, a shell, and a text editor. What would you change to listing 1 and/or listing 2 and/or listing 3 to produce this output:

index of 128 is:7

remark: the less you change, the better it is.


My questions are :

  • what would be your answer to those questions ?
  • how to improve it ?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:42:07+00:00Added an answer on June 18, 2026 at 3:42 am

    As an interview question, I would make the problem simpler. I have found that in interviews it can be much harder to solve these sort of problems without allot of hints. After a couple of questions they can’t answer, interviewees can give up which isn’t always productive.


    Can you explain this output ?

    There is a bug in the code with i == j which impact A & B differently. In one case the sort assumes the value is less than 128 and in the second it matches 32 because this is cached.

    If you try something like -XX:+AggressiveOpts` or another option to increase the Integer cache size it would match in each case.

    What would you change to listing 1

    Change i == j ? 0 : -1 to i > j ? -1 : 0

    Of course using Integer.compare() would some the problem 😉

    how to improve it

    Depending on the purpose of the program, I would use

    int count = 0;
    for(int n = Integer.parseInt(args[0]); n != 0; n >>>= 1)
      count++;
    System.out.println(count);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.