StartUp Interview Question
Country: United States
Interview Type: Written Test
Something in these lines:
var arr = ["aab", "bac", "aaa", "cda"];
function wordChaining(arr){
var index = 1;
var arr = arr.sort();
console.log(arr);
var sortedArr = [arr[0]];
var lastchar;
while(index < arr.length){
console.log("arr",arr[index].substring(0,1), arr, arr[index]);
console.log("sorted",sortedArr[index-1].substring(arr[index-1].length-1), sortedArr, sortedArr[index-1])
if(arr[index].substring(0,1)==sortedArr[index-1].substring(sortedArr[index-1].length-1)){
sortedArr.push(arr[index]);
}
index++
}
return (sortedArr.length == arr.length) ? sortedArr : false;
}
console.log(wordChaining(arr))
This is assuming that all characters are lowercase and ascii.
Create an int array of 26. Create a count int var. Loop through original string array, checking only the first and last letters. Add +1 to the int idex that corresponds with the ascii character code for both first and last letters. Before adding + 1 to character index if current int var is +1 to count, if current value is 1, -1. After looping through original string if count is greater than 0....the strings cannot make a circle. It 0, then they can.
can you clarify the question a bit more?
- aka November 25, 2015