Last active
March 6, 2025 20:48
-
-
Save primaryobjects/4539d323e7103237ed0efd92032c3bd9 to your computer and use it in GitHub Desktop.
CodeSignal interview problems https://jsfiddle.net/g6qyubs0/1/ See also https://codesignal.com/blog/interview-prep/example-codesignal-questions/
This file contains hidden or 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
| const powersOf2 = (numbers: [number]): number => { | |
| let count: number = 0; | |
| const counts = {}; | |
| const matches: [ [number] ] = []; | |
| for (let i: number = 0; i<numbers.length; i++) { | |
| const element = numbers[i]; | |
| counts[element] = counts[element] ? counts[element] + 1 : 1; | |
| for (let twoPower: number = 0; twoPower < 21; twoPower++) { | |
| // Calculate the amount remaining to get to this power of two. | |
| const second_element = (1 << twoPower) - element; | |
| // If the remainder exists in our lookup table, then we have a match. | |
| count += counts[second_element] ? counts[second_element] : 0; | |
| if (counts[second_element]) { | |
| matches.push([element, second_element]); | |
| } | |
| } | |
| } | |
| return { count, matches }; | |
| } |
This file contains hidden or 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
| const patternMatch = (pattern: string, source: string): number => { | |
| let count = 0; | |
| const matches = []; | |
| const vowels: string = 'aeiouy'; | |
| const VOWEL: string = '0'; | |
| const CONSONANT: string = '1'; | |
| for (let i: number = 0; i<source.length - pattern.length + 1; i++) { | |
| let isMatch = true; | |
| for (let offset: number = 0; offset<pattern.length; offset++) { | |
| if (pattern[offset] === VOWEL && !vowels.includes(source[i + offset])) { | |
| isMatch = false; | |
| break; | |
| } | |
| else if (pattern[offset] === CONSONANT && vowels.includes(source[i + offset])) { | |
| isMatch = false; | |
| break; | |
| } | |
| } | |
| count += isMatch ? 1 : 0; | |
| if (isMatch) { | |
| matches.push(source.substring(i, i + pattern.length)); | |
| } | |
| } | |
| return { count, matches }; | |
| }; |
This file contains hidden or 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
| console.log(patternMatch('010', 'amazing')); | |
| console.log(patternMatch('100', 'codesignal')); | |
| console.log(patternMatch('110', 'frosty')); | |
| console.log(patternMatch('0110', 'thisisalongsentence')); | |
| /* | |
| { | |
| count: 2, | |
| matches: ["ama", "azi"] | |
| } | |
| { | |
| count: 0, | |
| matches: [] | |
| } | |
| { | |
| count: 2, | |
| matches: ["fro", "sty"] | |
| } | |
| { | |
| count: 2, | |
| matches: ["ente", "ence"] | |
| } | |
| */ | |
| console.log(powersOf2([1, -1, 2, 3])); // 5 | |
| console.log(powersOf2([-2, -1, 0, 1, 2)); // 5 | |
| console.log(powersOf2([3, 5, 7, 9])); // 2 | |
| /* | |
| { | |
| count: 5, | |
| matches: [[1, 1], [2, -1], [2, 2], [3, -1], [3, 1]] | |
| } | |
| { | |
| count: 5, | |
| matches: [[1, 0], [1, 1], [2, -1], [2, 0], [2, 2]] | |
| } | |
| { | |
| count: 2, | |
| matches: [[5, 3], [9, 7]] | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment