-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3974bcf
commit c69a096
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Given an array of SKUs return the number of duplicate SKUs. | ||
// i.e. fn([1234, 5678, 1234, 7777, 0000, 0000]) => 2 | ||
// Since 1234 and 0000 are duplicates, the answer is 2 | ||
// Can be solved in O(n), but there is an obvious O(n^2) solution | ||
|
||
// Given an array of SKUs, return the top k SKUs and their frequency. | ||
// i.e. fn(3, [12, | ||
// Can be done in O(n) | ||
// Write a function to flatten an array of arrays nested N levels deep. | ||
// fn([12, [34, 56], [78, [[9,10]],11]) = > [1,2,3,4,5,6,7,8,9,10] | ||
// Can be done in O(n) | ||
|
||
// Write a function to determine the maximum discount for a time period, given the following rules: | ||
// Day 1 – Day 5 => 10% | ||
// Day 2 – Day 7 => 5% | ||
// Day 4 – Day 6 => 20% | ||
// i.e. fn(4 (day)) => 10 + 5 + 20 = 35% | ||
|
||
// Given a list of products and their corresponding bundle prices. The customer wants to purchase N quantity of products from Best Buy. Provide a function which suggests the customer N quantity of products with the minimum price. | ||
// i.e | ||
// Quantity of Products = [10, 20, 30, 15, 25] | ||
// Price of product bundle = [100, 200, 120, 130, 165] | ||
// Customer wants to purchase 500 quantity of products – What is the minimum price? | ||
// This is an example of a “ Greedy Knapsack problem.” | ||
// Resource: https://en.wikipedia.org/wiki/Knapsack_problem |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Determine if a string of length S is a palindrome if you remove at most a single characters from that string. | ||
|
||
//Best Case: O(1) & Worst Case: O(2N) | ||
const isApproxPalindrome = (str, calledOnce = false) => { | ||
if (str.length === 0) { | ||
return true; | ||
} | ||
let result = isPalindrome(str); | ||
|
||
if (result.isPalindrome) { | ||
return true; | ||
} | ||
|
||
if (calledOnce) { | ||
return false; | ||
} | ||
|
||
const { badIndex } = result; | ||
str = str.slice(0, badIndex) + str.slice(badIndex + 1); | ||
|
||
return isApproxPalindrome(str, true); | ||
}; | ||
|
||
// O(1) Space & O(N) time | ||
const isPalindrome = str => { | ||
const strArr = str.split(""); | ||
const strLength = str.length; | ||
let result = { | ||
isPalindrome: true, | ||
badIndex: null | ||
}; | ||
|
||
strArr.forEach((char, idx) => { | ||
const correspondingChar = strArr[strLength - 1 - idx]; | ||
if (char !== correspondingChar) { | ||
result = { | ||
isPalindrome: false, | ||
badIndex: idx | ||
}; | ||
} | ||
}); | ||
return result; | ||
}; | ||
|
||
const data = ["abca", "aba", "abcd", "acba", ""]; // true, true, false, true, true | ||
|
||
data.forEach(testCase => { | ||
console.log("isApproxPalindrome(testCase)", isApproxPalindrome(testCase)); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* You are assigned to build a function to figure out | ||
* what to display for today's store hours based on current time. | ||
* | ||
* The backend engineer have an API at "/store-hours/44" to provide a list | ||
* of open and close time for this week. | ||
* Ex. { result : [ | ||
* { open:"10-28-2018T12:00", close:"10-28-2018T20:00" }, | ||
* { open:"10-29-2018T13:00", close:"10-29-2018T18:00" }, | ||
* { open:"10-30-2018T10:00", close:"10-30-2018T19:00" }, | ||
* { open:"10-31-2018T11:00", close:"10-31-2018T21:00" }, | ||
* { open:"11-01-2018T12:00", close:"11-01-2018T20:00" }, | ||
* { open:"11-02-2018T12:00", close:"11-02-2018T20:00" }, | ||
* { open:"11-03-2018T12:00", close:"11-03-2018T20:00" }, | ||
* ]} | ||
* | ||
* With those information, you have to figure out if you need to display | ||
* one of those 3. | ||
* - 'Open on 11/03 until 10pm' | ||
* - 'Closed for the rest of day' | ||
* - 'Will open on 11/03 from 12pm to 10pm' | ||
*/ | ||
|
||
function getStoreHoursText() { | ||
// Step 1 : Fetching stored data from API | ||
// Purpose : Check how we make ajax call, and the timing of receiving the data | ||
const hours = fetchStoreHourData(); | ||
// Follow up : If you need to make 2 API calls for OPEN and CLOSE hours | ||
|
||
// Step 2 : Loop thorugh the array with data conversion | ||
// Purpose : Examine knowledge of looping and data manipulcation | ||
const parsedHours = parseHours(hours); | ||
// Follow up : slice() vs substring() | ||
// https://stackoverflow.com/questions/2243824/what-is-the-difference-between-string-slice-and-string-substring | ||
|
||
// Step 3 : Convert the marshalled data into human readable string | ||
// Purpose : Testing String concatenation skill and conditional logic | ||
const output = toHumanString(parsedHours); | ||
//Follow up : what if we need to display Monday instead of 11/03 | ||
|
||
return output; | ||
} | ||
|
||
getStoreHoursText(); |