Last active
March 13, 2019 10:35
-
-
Save pelf/efcf4ca139615e754be74957f47825a9 to your computer and use it in GitHub Desktop.
Quick and simple interview coding exercises
This file contains 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
# When given an array with a single duplicate number, | |
# it returns that number. | |
def find_duplicate_number(arr) | |
arr | |
end | |
puts '# find_duplicate_number:' | |
puts find_duplicate_number([5,3,7,2,3,1]) == 3 | |
# When given an array with any number of duplicate numbers, | |
# it returns an array of all those numbers (each number only once). | |
def find_duplicate_numbers(arr) | |
arr | |
end | |
puts '# find_duplicate_numbers:' | |
nums = find_duplicate_numbers([5,3,7,2,7,7,3,1]) | |
puts nums.sort == [3,7] | |
# When given an array with any number of duplicate numbers, | |
# it returns an array without any duplicates. | |
def remove_duplicate_numbers(arr) | |
arr | |
end | |
puts '# remove_duplicate_numbers:' | |
nums = remove_duplicate_numbers([5,3,7,2,7,3,1]) | |
puts nums.sort == [1,2,3,5,7] | |
# When given an array of numbers, it reverses it. | |
def reverse(arr) | |
arr | |
end | |
puts '# reverse:' | |
puts reverse([1,2,3,4,5]) == [5,4,3,2,1] | |
# When given an array with numbers, | |
# it returns the lowest and highest numbers (in a two dimensional array) | |
# e.g. [2,3,1,3] => [1, 3] | |
def lowest_and_highest(arr) | |
[0,0] | |
end | |
puts '# lowest_and_highest:' | |
puts lowest_and_highest([2,3,1,3]) == [1,3] | |
# When given an array of numbers (arr) and a number (n), | |
# it returns all pairs in arr that sum up to n. | |
def pairs_with_sum(arr, n) | |
[] | |
end | |
puts '# pairs_with_sum:' | |
puts pairs_with_sum([1,2,3,4,5,6], 8) == [[2,6], [3,5]] |
This file contains 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
# When given two strings, | |
# it returns true when they are anagrams of each other, false otherwise. | |
def anagrams?(str1, str2) | |
end | |
puts '# anagrams?:' | |
puts anagrams?('abcd', 'bdca') == true | |
puts anagrams?('abcd', 'badca') == false | |
# When given a string, it reverses it using recursion | |
def recursive_reverse(str) | |
end | |
puts '# recursive_reverse:' | |
puts recursive_reverse('abcdef') == 'fedcba' | |
# When given a string with multiple words in it, | |
# it returns a string with each individual word reversed. | |
# e.g. 'ab cd' => 'ba dc' | |
def reverse_words(str) | |
end | |
puts '# reverse_words:' | |
puts reverse_words('one two three') == 'eno owt eerht' | |
# When given a string, | |
# it returns true if it is a palindrome, false otherwise. | |
def palindrome?(str) | |
end | |
puts '# palindrome?:' | |
puts palindrome?('abcbca') == false | |
puts palindrome?('abcdcba') == true | |
# When given two strings, | |
# it returns true when they are rotation of the same string. | |
def rotated_equals?(str1, str2) | |
end | |
puts '# rotated_equals?:' | |
puts rotated_equals?('abcdef', 'cdefab') == true | |
puts rotated_equals?('abcdef', 'bacdef') == false | |
puts rotated_equals?('abcdef', 'cdefxab') == false | |
# When given a string, | |
# it returns the vowel count and consonant count (2 element array) | |
def counts(str) | |
[0, 0] | |
end | |
puts '# counts:' | |
puts counts('abCdEf') == [2, 4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment