Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Engineering candidate coding questions
- ##### expandNumbers
- 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'.
- const expandNumbers = inputString => {
- return inputString.replace(/(\d+)(\w)/g, (m, n, c) => {
- return new Array(parseInt(n, 10) + 1).join(c)
- })
- }
- ##### lengthOfLongestRepeat
- 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.
- const lengthOfLongestRepeat = inputList => {
- let curr = null, count = 0, arr = []
- inputList.map(x => {
- if (x != curr) {
- if (count > 0) arr.push(count)
- curr = x
- count = 1
- } else count++
- })
- return Math.max.apply(null, arr)
- }
- console.log(lengthOfLongestRepeat([1,2,3,3,4,5]))
- ##### doAnySubstringsIntersect
- 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.
- const doAnySubstringsIntersect = (inputStringA, inputStringB, substringLength) => {
- for (let i = 0; i <= inputStringA.length - substringLength; i++)
- if (inputStringB.includes(inputStringA.substring(i, i+substringLength))) return true
- return false
- }
- console.log(doAnySubstringsIntersect('abcde', 'xyabcz', 4))
- ##### areaOfRectangleContainingAllInstancesOfValue
- 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:
- ```
- a b c d a
- b c x b e
- ```
- 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).
- const areaOfRectangleContainingAllInstancesOfValue = (twoDimensionalArrayOfValues, value) => {
- const dimension = [
- twoDimensionalArrayOfValues.length,
- twoDimensionalArrayOfValues[0].length
- ],
- rect = []
- for (let i = 0; i < dimension[0]; i++) {
- for (let j = 0; j < dimension[1]; j++) {
- if (twoDimensionalArrayOfValues[i][j] === value) rect.push([i+1, j+1])
- }
- }
- // cross product
- return Math.abs((rect[0][0]*rect[1][1]) - (rect[0][1]*rect[1][0]))
- }
- console.log(areaOfRectangleContainingAllInstancesOfValue([['a', 'b', 'c', 'd', 'a'], ['b', 'c', 'x', 'b', 'e']], 'c'))
- ##### numberOfPlusPatterns
- 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:
- ```
- 2 1 2
- 1 1 1
- 5 1 6
- ```
- 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.
- ##### numberOfValuesWithSameCountInArrays
- 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.
- const createObjFromArray = arr => {
- return arr.reduce((acc, curr) => {
- if (typeof acc[curr] == 'undefined') acc[curr] = 1
- else acc[curr] += 1
- return acc
- }, {})
- }
- const numberOfValuesWithSameCountInArrays = (arrayA, arrayB) => {
- const objA = createObjFromArray(arrayA.sort()), objB = createObjFromArray(arrayB.sort())
- let count = 0
- for (key in objA) {
- if (objA[key] === objB[key]) count++
- }
- return count
- }
- 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