DEV Community

Discussion on: Can you solve this interview problem?

Collapse
 
rushannotofficial profile image
Rushan S J

Hey, I got the solution. The only thing is that I'm more familiar with python but the same logic applied in other languages too.

import itertools, base64

originalarray = ["6Ly9kZXYudG", "9jb21tZW5", "8vMHNodXZvMC", "aHR0cHM", "0LzFqZTFt"]     # this is the input 

for i in itertools.permutations(originalarray, len(originalarray)):  # getting all possible combinations
    tmpdecodedstring = ""
    for k in i:     # iterate over each strng in the array and add it to the string
        tmpdecodedstring += str(k)
    try:
        print("URL is: " + str(str(base64.b64decode(tmpdecodedstring)).decode()))  # decode the string finally
    except:
        pass # only proper base64 string dont raise an error. If there is a error, that means that the string is incorrect and we try another permutation.
Enter fullscreen mode Exit fullscreen mode
Collapse
 
0shuvo0 profile image
Shuvo

Great