DEV Community

Discussion on: Can you solve this interview problem?

Collapse
 
brunnerh profile image
brunnerh

If nothing is known about the URL beforehand, this seems underspecified. There are multiple valid URLs that can be constructed from the fragments.

function permutate(arr) {
    if (arr.length == 1) return [arr];

    return arr.map((item, i) => {
        var rest = [...arr];
        rest.splice(i, 1);
        return permutate(rest).map(x => [item, ...x])
    }).flat();
}

permutate(JSON.parse(url))
  .map(x => atob(x.join('')))
  .map(x => { try { return new URL(x).toString() } catch { return null } })
  .filter(Boolean);
Enter fullscreen mode Exit fullscreen mode

Yields:

https://dev.xn--tocommen%7C1414%20-oqa05ipa4v8fnd624dn60pha/1je1m
https://dev.tocomment/1je1m%C3%B2%C3%B3%076%C2%87Wf%C3%B3
https://dev.to/0shuvo0/comment/1je1m
https://dev.to/0shuvo0-%0B%C3%8CZ%C2%99L[%7D%C2%8D%C2%BD%C2%B5%C2%B5%C2%95%C2%B9
Enter fullscreen mode Exit fullscreen mode
Collapse
 
0shuvo0 profile image
Shuvo

You can take those multiple URLs as a challenge as well
Like sending https request to see the status

Collapse
 
brunnerh profile image
brunnerh

Those are additional assumption about the URL, though:

  • It uses a common web protocol like HTTP/HTTPS
  • There actually is a reachable server that would reply to those requests