Skip to content

Instantly share code, notes, and snippets.

@ConnerAiken
Created January 31, 2017 06:12
Show Gist options
  • Save ConnerAiken/25471ca3c85123221a3401dc59772d3c to your computer and use it in GitHub Desktop.
Save ConnerAiken/25471ca3c85123221a3401dc59772d3c to your computer and use it in GitHub Desktop.
An interview question with amazon
var str = '/var/www/./../html/.././webkit';
var str2 = './var/www/./html/testdir/../assets/.';
function normalizeUnix(string) {
var arr = string.split('/');
if(arr[0] == '')
arr.splice(0, 1);
for(var i = 0; i < arr.length; i++) {
if(arr[i] == '.') {
arr.splice(i, 1);
i--;
}else if(arr[i] == '..') {
i--;
arr.splice(i, 2);
i--;
}
}
return arr;
}
console.log(normalizeUnix(str));
console.log(normalizeUnix(str2));
@ConnerAiken
Copy link
Author

I was asked this question during a phone interview with amazon for a web development position.

I let my nerves get the best of me..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment