I was issued the following coding challenge from a company to write an encoding and decoding function:
Encoding: This function needs to accept a signed integer in the 14-bit range [-8192..+8191] and return a 4 character string. The encoding process is as follows:
Add 8192 to the raw value, so its range is translated to [0..16383]. Pack that value into two bytes such that the most significant bit of each is cleared.
Unencoded intermediate value (as a 16-bit integer):
00HHHHHH HLLLLLLLEncoded value:
0HHHHHHH 0LLLLLLL- Format the two bytes as a single 4-character hexadecimal string and return it.
Decode:
Your decoding function should accept two bytes on input, both in the range [0x00..0x7F] and recombine them to return the corresponding integer between [-8192..+8191]
Here is my solution to both functions:
public static String encode(int num){
if(num < -8192 || num > 8191) {
throw new IllegalArgumentException("This function only takes ints within the 14-bit range (-8192 to 8191)");
} else {
//step 1
//System.out.println("Unencoded Decimal Value: "+ num);
num += 8192;
//System.out.println("Intermediate Decimal: " + num);
//step 2
String numString = Integer.toHexString(num);
//System.out.println("Intermediate Hex: " + numString);
//step 3
String theBinary = Integer.toBinaryString(Integer.parseInt(numString,16));
while(theBinary.length() < 14){
String zero = "0";
zero += theBinary;
theBinary = zero;
}
//Build the updated Binary
String newBinary = "";
for(int i=theBinary.length() -1; i>=0; i--){
newBinary += theBinary.charAt(i);
if(i== 7 || i == 0){
newBinary += "0";
}
}
newBinary = new StringBuilder(newBinary).reverse().toString();
StringBuilder sb = new StringBuilder();
sb.append(Integer.toHexString(Integer.parseInt(newBinary,2)));
while(sb.length() < 4) {
sb.insert(0, '0'); // pad with leading zero if needed
}
String hex = sb.toString();
System.out.println("The Encoded Hex: " + hex);
System.out.println();
return hex;
}
}
public static int decode(byte byte1, byte byte2){
if(byte1 < 0x00 || byte2 < 0x00 || byte1 > 0x7F || byte2 > 0x7F){
throw new IllegalArgumentException("This function only takes bytes between 0x00 and 0x7F");
}
//Append Zeros
String pt1 = Integer.toBinaryString(byte1);
while(pt1.length() < 7){
String zero = "0";
zero += pt1;
pt1 = zero;
}
//Append Zeros
String pt2 = Integer.toBinaryString(byte2);
while(pt2.length() < 7){
String zero = "0";
zero += pt2;
pt2 = zero;
}
String fullBinary = pt1 + pt2;
//System.out.println("Full Binary: "+fullBinary);
int decimalValue = Integer.parseInt(fullBinary, 2);
//System.out.println("Decimal Value: "+decimalValue);
int originalValue = decimalValue - 8192;
System.out.println("Original Value: " + originalValue);
//System.out.println();
return originalValue;
}
I recieved that this solution "did not demonstrate the level of expertise required". The only requirements issued in the challenge were to write the functions and to return the specified value (which it does) and to do it quickly (which it does). My main question is where could I have improved this code to to "demonstrate more expertise". I'm just looking for an entry level programming position, but I keep getting responses like this that are vague even when I write code that completes the task issued.