2

Yesterday I went for an interview and they asked me to create a method which takes an integer value and displays the number with its digits in descending order. I used string manipulation and solved it but they asked me to do it using binary number technique. I still don't know how to approach this problem.

2 Answers 2

1

"Binary number technique"? It's a bullshit question, one where the correct answer is to walk out from the interview because it's a bullshit company.

Anyway, the best answer I can think of is

public static int solveBullshitTaskInASmartWay(int n) {
    // get characters and sort them
    char[] chars = Integer.toString(n).toCharArray();
    Arrays.sort(chars);
    // comparators don't work in Java for primitives,
    // so you either have to flip the array yourself
    // or make an array of Integer or Character
    // so that Arrays.sort(T[] a, Comparator<? super T> c)
    // can be applied
    for (int i = 0, j = chars.length - 1; i < j; i++, j--) {                                                                                   
        char t = chars[i]; chars[i] = chars[j]; chars[j] = t;
    }
    // reconstruct the number
    return Integer.parseInt(new String(chars));
}   

There is no numeric way to sort a number's digits, if you're expecting a nifty mathematical answer you will be waiting for a while.

EDIT: I need to add this - "digit" is solely a property of display of numbers. It is not a property of a number. Mathematically, the number 0b1000 is the same as 0x8 or 0o10, or 008.00000, or 8e0 (or even trinary 22, if anyone used trinary; alas, no conventional notation for that in programming). It is only the string representations of numbers that have digits. Solving this problem without use of characters or strings is not only pretty hard, it is stupid.

EDIT2: It is probably obvious, but I should make it clear that I have no beef with the OP, it is the interviewer I that I am entirely laying the blame on.

Sign up to request clarification or add additional context in comments.

7 Comments

I was wondering the same.. using binary math here makes no sense... I just thought that the interviewer wanted to know whether the OP knew binary arithmatic.. :P
Maybe they wanted him to say "but that wouldn't make sense" ? I agree that this is not a good question for an interview, though.
He said I will give you a hint and started telling me how to convert a decimal number into binary. what the hell I was supposed to do ? comapany was Software innovations from Norway having office in Bangalore.
That especially is worthy of a WTF, since there is no way to deal with decimal digits if you have a binary representation of the number (unless you're doing BCD, but that too would be a WTF).
Quite possibly what the interviewer wanted was for the OP to write code that would extract the digits as values from 0 to 9 rather than depending on toString. Or perhaps he wanted the OP to sort the hexadecimal digits. I wouldn't condemn this as a "bullshit question" unless based on the OP's question. I suspect it's not word-for-word, and I also wouldn't be surprised if part of the purpose was to give a vague problem description so that the interviewee would be forced to ask for clarification.
|
1

The is a simple (but not efficient) way of doing it without conversion to string. You can perform insertion sort on digits by extracting them from number using modulo and division, comparing them, and swapping if needed. There will be at most 9*8 comparsions need. Here is code in C++

int sortDigits(int number)
{
    for(int j = 0; j < 9; ++j) //because number can have 9+1 digits (we don't need 10 because digits are sorted in pairs)
    {
        int mul = 1;
        for(int i = 0; i < 8; ++i) //because with i == 7 mul * 10 is biggest number fitting in int (will extract last digit)
        {
            if (mul * 10 > number) break; //by doing that we ensure there will be no zeroes added to number
            int digitRight = number / mul % 10;
            int digitLeft = number / (mul * 10) % 10;
            if(digitRight > digitLeft) //swapping digits
            {
                /*
                 number -= digitLeft * mul * 10;
                 number += digitLeft * mul;

                 number -= digitRight * mul;
                 number += digitRight * mul * 10;
                */
                number -= digitLeft * mul * 9;
                number += digitRight * mul * 9;
            }
            mul *= 10;
        }
    }
    return number;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.