2

I got a task where I need to write a encoder with these requirements:

  • Input: an integer varying from 1 up to 8 digits (i.e. 12345678, 2352, 76543)
  • Output: a fixed size 6 digits code that could contain any alphanumeric and symbols (a-z, A-Z, 0-9, !@#$%*()|-_=+^/?)
  • The operation must be reversible, so given a code it should decode back to the original integer
  • There's no need for the output code to be cryptographicaly safe

I have no experience with crypto(?) or encoding algorithms so I have no clue how to get this job done. The libs I found online doesn't allow to set the output size since it's based on input size.

Any help is strongly appreciated, I'm lost here

2

2 Answers 2

2

I got excited when I saw this, so I made an answer that at least does what the question requires :D

The encoding works through base conversion logic and conversion of those numbers to text. I used an array of length 6 to store the data on encoding to ensure that it will always be 6 chars(and not less)

The decoding works through just adding it back respecting the powers so it adds back in full value

I hope it's an answer you're looking for because I love this question :D

//a homemade answer :D
//first time I saw a question like that, I hope I get stuff like this in college ngl
function encode(num){
  let alphabet=[]
  for(let i=48;i<127;i++){
    alphabet.push(String.fromCharCode(i))
  }
  //alphabet is filled with encoding text :D
  let arr=[0,0,0,0,0,0] //for fixed 6 digits
  let pow=0; let pow1=1
  let length=alphabet.length
  while(num>0){
    let subtraction=(num%Math.pow(length,pow1))
    num-=subtraction
    arr[pow]=subtraction/Math.pow(length,pow)
    pow++; pow1++
  }
  return arr.reverse()
  .map(a=>String.fromCharCode(a+48)) //offset of 48 for nice chars
  .join('') //like a base conversion basically
}

function decode(string){
  let num=0; let length=127-48 //length really is the number of base(79 in this case)
  string=string.split('').reverse().join('')
  for(let i=0;i<string.length;i++){
    let int=string.charCodeAt(i)-48 //-48 remembering the offset
    num+=int*Math.pow(length,i)
  }
  return num
}



var int=12348291
console.log('encoded num:',encode(int))
console.log('decoded string:',decode(encode(int)))
console.log('they are reversible?',decode(encode(int))==int)

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

4 Comments

I'm glad you loved my question. Your enthusiasm is motivating. Thanks for your help
@TheBombSquad +1 for your excitement towards coding... anyways can you write the same code for alpha-numeric input ... and the encoded / output string can be of any length ( but it must be fixed for any input ) and must output must be reversible
@daksh you can.. however that is not the question here.. ask your question and send a link maybe (or maybe someone would get to you with a method before)
1

Here's a simple approach:

  • convert the input to a string and extend to 9 digits by adding zeros to the left as necessary
  • encode each group of 3 digits using base32 encoding, which gives you max two letters/digits per group. You can use .toString(32) while testing, but I guess the interviewer expects you to create your own function
  • pad each group to the length 2 and concatenate

Example:

 1234567 -> 001234567 -> 001 234 567 -> 1 7a hn -> 017ahn

Good luck!

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.