DEV Community

Cover image for Can you solve this interview problem?
Shuvo
Shuvo

Posted on

Can you solve this interview problem?

So few days ago I applied to a MNC and I had some interesting problems to solve in given amount of time. So let me share one with you, see if you can solve this.

Problem Description

So let's say we have a URL something like this:

let url = "https://dev.to/0shuvo0"
Enter fullscreen mode Exit fullscreen mode

Now they have converted the url to a base64 string.
So the URL have become something like this:

let url = "aHR0cHM6Ly9kZXYudG8vMHNodXZvMA=="
Enter fullscreen mode Exit fullscreen mode

Now what they did was split the sting to multiple parts and convert into an array

let url = ["aHR0cH", "M6Ly9kZX", "YudG8vMHN", "odXZvMA=="]
Enter fullscreen mode Exit fullscreen mode

But of course the madnesses doesn't stop here. Then the shuffled the array so it became something like this:

let url = ["M6Ly9kZX", "aHR0cH", "odXZvMA==", "YudG8vMHN"]
Enter fullscreen mode Exit fullscreen mode

And lastly they have converted that array to a string.

So here is your input

let url = `[ "6Ly9kZXYudG", "9jb21tZW5", "8vMHNodXZvMC", "aHR0cHM", "0LzFqZTFt" ]`
Enter fullscreen mode Exit fullscreen mode

Use the input to find the original URL programmatically you have 45 Minutes to do it.

Useful JavaScript functions that can help you

  • You can convert your array to sting by calling join method on it. Eg.
let urlStr = url.join("")
Enter fullscreen mode Exit fullscreen mode
  • You can use atob function to decode the base64 string.
let decoded = atob(urlStr)
Enter fullscreen mode Exit fullscreen mode

Now go and and see if you can solve this. Best of luck šŸ‘

0shuvo0 image

Was it useful? Support me on Patreon

Patreon Logo

Latest comments (58)

Collapse
 
microtot profile image
MicroTot • Edited

This is the worst implementation here, but it workd

let url = ["aHR0cHM", "6Ly9kZXYudG", "8vMHNodXZvMC",  "9jb21tZW5",  "0LzFqZTFt" ]
var joinedUrl = url.join()
console.log(joinedUrl)

var result = joinedUrl.replace(/,/g, "")
var decoded = atob(result)
console.log(decoded)
Enter fullscreen mode Exit fullscreen mode

Output:

[LOG]: "https://dev.to/0shuvo0/comment/1je1m" 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jefflindholm profile image
Jeff Lindholm

This sounds like some of the 'oldschool' brain teasers (example: 'Why are manhole covers round?' -- which of course I answer 'because manholes are round, any other shape would be silly')
At least this is asking for you to code something vs. just ask if you know trivia.

Collapse
 
0shuvo0 profile image
Shuvo

Glad you liked it

Collapse
 
enygma2002 profile image
Eduard Moraru • Edited

Here's my not-so-fancy recursive solution :)

let url = [ "6Ly9kZXYudG", "9jb21tZW5", "8vMHNodXZvMC", "aHR0cHM", "0LzFqZTFt" ]

function testUrl(testUrl) {
  return/^[\x00-\x7F]*$/.test(testUrl);
}

function test(segments, currentString) {
  //console.log("try(segments: " + segments + " currentString: " + currentString)
  if (segments.length == 0) {
    let decoded = atob(currentString)
    if (decoded.startsWith("https://") && testUrl(decoded)) {
      console.log("Encoded: " + currentString)
      console.log("Decoded: " + decoded);
    }
  } else {
    for (let i=0; i<segments.length; i++) {
      let newString = currentString + segments[i];
      let remainingSegments = segments.slice(0, i).concat(segments.slice(i+1, segments.length))

      test(remainingSegments, newString)
    }
  }
}

test(url, "")
Enter fullscreen mode Exit fullscreen mode

Outputs:

Encoded: aHR0cHM6Ly9kZXYudG8vMHNodXZvMC9jb21tZW50LzFqZTFt
Decoded: https://dev.to/0shuvo0/comment/1je1m
Enter fullscreen mode Exit fullscreen mode
Collapse
 
itr13 profile image
Mikael Klages

Ignore the input and ddos their site with random urls until you find the correct url.

Personally I would decline to solve their challenge and show them source code I've written in the past, though honestly, this specific question sounds like a big red flag.

Collapse
 
documentednerd profile image
Kevin Mack

I fail to see how this interview problem does anything other than play "stump the chump." Realistically interviews are about determining if a person's skills, and personality are a fit for the team, and the work being done. How does this accomplish any of that? I can't think of a single time that this scenario would be applicable to an actual project. And I am of the belief that at any interview, "you are interviewing them, as much as they are interviewing you." This would speak volumes to me as part of a terrible "Know-it-all" / gate keeper culture that I would not want to be a part of.

Collapse
 
kb1hyr profile image
kb1hyr

Convert the strings in each array element back to base64, then iterate through each possible combination until you find one that, converted back into a string, starts with http. This is aided by the fact the last array element will always end in ==.

Collapse
 
choroba profile image
E. Choroba

A Perl solution I was able to write in less than 5 minutes:

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

use MIME::Base64 qw{ decode_base64 };
use List::Util qw{ shuffle };

my $cipher = '[ "6Ly9kZXYudG", "9jb21tZW5", "8vMHNodXZvMC", "aHR0cHM", "0LzFqZTFt" ]';

my @strings = $cipher =~ /"([^"]+)"/g;

my $string = "";
until ($string =~ /^[ -~]+$/) {
    @strings = shuffle(@strings);
    $string = decode_base64(join "", @strings);
}
say $string;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
0shuvo0 profile image
Shuvo

Nice

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
0shuvo0 profile image
Shuvo

Savage

Collapse
 
thacken profile image
Chris Thackrey

This was a fun problem I haven't crossed paths with before, please consider sharing more like this!

Collapse
 
0shuvo0 profile image
Shuvo

Many thanks.

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

Collapse
 
joeattardi profile image
Joe Attardi

What a ridiculous interview question.

Collapse
 
0shuvo0 profile image
Shuvo

and don't even tell them about the problem at all

Collapse
 
ky1e_s profile image
Kyle Stephens

I sincerely hope nobody is being asked questions like this in interviews.

/ends

 
grahamthedev profile image
GrahamTheDev

šŸ¤£ ā€œgreat minds think alikeā€ā€¦or is it ā€œfools seldom differā€, I will go for ā€œgreat mindsā€ as I donā€™t want to drag you down with me šŸ˜œ