Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. ### Engineering candidate coding questions
  2.  
  3. ##### expandNumbers
  4.  
  5. Write a function `expandNumbers(inputString)` that returns a transformation of the input string where digits (0 to 9) are replaced by that many occurences of the next character in the string. So expandNumbers('a3b') would return 'abbbb', and expandNumbers('a3b42c') would return 'abbbb2222ccc'. A digit at the end of the input string is replaced by nothing -- expandNumbers('a3b9') would return 'abbbb'.
  6.  
  7.  
  8. const expandNumbers = inputString => {
  9. return inputString.replace(/(\d+)(\w)/g, (m, n, c) => {
  10. return new Array(parseInt(n, 10) + 1).join(c)
  11. })
  12. }
  13.  
  14. ##### lengthOfLongestRepeat
  15.  
  16. Write a function `lengthOfLongestRepeat(inputList)` that returns the length of the longest set of repeat values in the input list. So lengthOfLongestRepeat([1,2,3,3,4,5]) would return 2, and lengthOfLongestRepeat(['a', 'b', 'c']) would return 1. Assume that values in the input list can be compared with `==` in Python or `===` in Javascript.
  17.  
  18. const lengthOfLongestRepeat = inputList => {
  19. let curr = null, count = 0, arr = []
  20.  
  21. inputList.map(x => {
  22. if (x != curr) {
  23. if (count > 0) arr.push(count)
  24.  
  25. curr = x
  26. count = 1
  27. } else count++
  28. })
  29.  
  30. return Math.max.apply(null, arr)
  31. }
  32.  
  33. console.log(lengthOfLongestRepeat([1,2,3,3,4,5]))
  34.  
  35. ##### doAnySubstringsIntersect
  36.  
  37. Write a function `doAnySubstringsIntersect(inputStringA, inputStringB, substringLength)` that returns true if any substrings of length substringLength occur in both input strings. You can assume that substringLength will be an integer >= 1. So doAnySubstringsIntersect('abcde', 'xyabcz', 3) would return true, and doAnySubstringsIntersect('abcde', 'xybcdz', 4) would return false.
  38.  
  39. const doAnySubstringsIntersect = (inputStringA, inputStringB, substringLength) => {
  40. for (let i = 0; i <= inputStringA.length - substringLength; i++)
  41. if (inputStringB.includes(inputStringA.substring(i, i+substringLength))) return true
  42.  
  43. return false
  44. }
  45.  
  46. console.log(doAnySubstringsIntersect('abcde', 'xyabcz', 4))
  47.  
  48.  
  49. ##### areaOfRectangleContainingAllInstancesOfValue
  50.  
  51. Write a function `areaOfRectangleContainingAllInstancesOfValue(twoDimensionalArrayOfValues, value)` that returns the area of the smallest rectangle containing all the instances of that value, given a 2-dimensional array of values. If there's only one instance of the value, then the rectangle would be 1x1, returning an area of 1. If the value isn't in the grid, return an area of 0. For example, given the following grid:
  52.  
  53. ```
  54. a b c d a
  55. b c x b e
  56. ```
  57. equivalent to the two-dimensional array [['a', 'b', 'c', 'd', 'a'], ['b', 'c', 'x', 'b', 'e']], the area of the rectangle containing value 'a' is 5 (1 x 5); the area of the rectangle for 'c' is 4 (2 x 2).
  58.  
  59. const areaOfRectangleContainingAllInstancesOfValue = (twoDimensionalArrayOfValues, value) => {
  60. const dimension = [
  61. twoDimensionalArrayOfValues.length,
  62. twoDimensionalArrayOfValues[0].length
  63. ],
  64. rect = []
  65.  
  66. for (let i = 0; i < dimension[0]; i++) {
  67. for (let j = 0; j < dimension[1]; j++) {
  68. if (twoDimensionalArrayOfValues[i][j] === value) rect.push([i+1, j+1])
  69. }
  70. }
  71.  
  72. // cross product
  73. return Math.abs((rect[0][0]*rect[1][1]) - (rect[0][1]*rect[1][0]))
  74. }
  75.  
  76. console.log(areaOfRectangleContainingAllInstancesOfValue([['a', 'b', 'c', 'd', 'a'], ['b', 'c', 'x', 'b', 'e']], 'c'))
  77.  
  78.  
  79. ##### numberOfPlusPatterns
  80.  
  81. Write a function `numberOfPlusPatterns(twoDimensionalArrayOfValues)` that returns the number of "+" patterns in a two-dimensional array of values. A + is made of single value in the middle and four "arms" extending out up, down, left, and right. The arms start with the middle cell and extend until the first different cell or edge of the grid. To count as a plus, all the arms must have two or more cells (including the center cell) and must all be the same length. For example, in this grid:
  82. ```
  83. 2 1 2
  84. 1 1 1
  85. 5 1 6
  86. ```
  87. the 1s form a plus pattern (so `numberOfPlusPatterns([[2,1,2],[1,1,1],[5,1,6]])` would return 1). Note that values could be letters, numbers, or any primitive type. Assume that values can be compared with `==` in Python or `===` in Javascript.
  88.  
  89. ##### numberOfValuesWithSameCountInArrays
  90.  
  91. Write a function `numberOfValuesWithSameCountInArrays(arrayA, arrayB)` that returns the number of values that occur the same number of times in arrayA and arrayB. So, numberOfValuesWithSameCountInArrays([1,2,3,3,4], [1,1,2,3,3,4,4]) would return 2 -- the values 2 and 3 occur the same number of times in each input list.
  92.  
  93. const createObjFromArray = arr => {
  94. return arr.reduce((acc, curr) => {
  95. if (typeof acc[curr] == 'undefined') acc[curr] = 1
  96. else acc[curr] += 1
  97. return acc
  98. }, {})
  99. }
  100.  
  101. const numberOfValuesWithSameCountInArrays = (arrayA, arrayB) => {
  102. const objA = createObjFromArray(arrayA.sort()), objB = createObjFromArray(arrayB.sort())
  103.  
  104. let count = 0
  105.  
  106. for (key in objA) {
  107. if (objA[key] === objB[key]) count++
  108. }
  109.  
  110. return count
  111. }
  112.  
  113. console.log(numberOfValuesWithSameCountInArrays([1,2,3,3,4], [1,1,2,3,3,4,4]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement