fork download
  1. /**
  2.  * Question:
  3.  * Write a method that takes an array of consecutive (increasing) letters as
  4.  * input and that returns the missing letter in the array.
  5.  *
  6.  * Input Constrains:
  7.  * You will always get a valid array. And it will be always exactly one letter
  8.  * be missing. The length of the array will always be at least 2.
  9.  * The array will always contain letters in only one case.
  10.  */
  11. function findMissingLetter(arr) {
  12. // Base condition
  13. // When array length is less than 2 then the second letter is missing.
  14. if (arr.length <= 2) {
  15. return String.fromCharCode(arr[0].charCodeAt(0) + 1);
  16. }
  17.  
  18. const midPos1 = Math.floor((arr.length-1)/2);
  19. const midPos2 = Math.floor(arr.length/2);
  20. const start = arr[0].charCodeAt(0);
  21. const mid1 = arr[midPos1].charCodeAt(0);
  22. const mid2 = arr[midPos2].charCodeAt(0);
  23. const end = arr[arr.length - 1].charCodeAt(0);
  24.  
  25. if (mid1 - start > end - mid2) {
  26. return findMissingLetter(arr.slice(0, midPos1));
  27. } else if (mid1 - start < end - mid2) {
  28. return findMissingLetter(arr.slice(midPos2));
  29. } else {
  30. // If both split are equal then the letter missing is at the center.
  31. return String.fromCharCode(mid1 + 1);
  32. }
  33. }
  34.  
  35. while ((str = readline())) {
  36. console.log(findMissingLetter(str.split('')));
  37. }
Success #stdin #stdout 0.02s 16916KB
stdin
abcdf
acdef
OQRS
OPRS
stdout
e
b
P
Q