Created
June 17, 2020 15:24
-
-
Save mrogach2350/cc683def12cec3c0fb49d622a15e2cab to your computer and use it in GitHub Desktop.
Amazon Rd. 1 Interview questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Non-coding questions | |
// 1. Describe what happens in the Browser after a user enters a URL and hits enter. | |
// 2. Describe what is a XSS (Cross-site scripting) attack and how can we prevent them? | |
// Coding question 1 | |
// Given an array of strings, return an array with all duplicates removed | |
// ex: ['a', 'a', 'a', 'b', 'a', 'c', 'c'] => ['a', 'b', 'c'] | |
//Solution #1: | |
const dedupeArray = (array = []) => { | |
const result = []; | |
array.forEach((letter) => { | |
if (!result.includes(letter)) { | |
result.push(letter); | |
} | |
}) | |
return result | |
} | |
//Solution #2: | |
const dedupeArray = (array = []) => { | |
const counter = {}; | |
array.forEach((letter) => { | |
if (!counter[letter])) { | |
counter[letter] = true; | |
} | |
}) | |
return Object.keys(counter) | |
} | |
// Coding question 2 | |
// Given a string, return a new string with the unique letters in order of most frequent to least frequent. | |
// (handle Spaces and punction as you would any other character) | |
const frequencyCounter = (string) => { | |
const counter = {} | |
string.split('').forEach((char) => { | |
if (char in counter) { | |
counter[char] ++; | |
} else { | |
counter[char] = 1; | |
} | |
}) | |
// Grab array of entries, sort by value, and then map back keys in sorted order | |
return Object.entries(counter) | |
.sort((a, b) => b[1] - a[1]) | |
.map(entry => entry[0]) | |
.join('') | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment