Coding Challenges and Practice 1 — Questions and Answers
Question 1: Write a function to reverse a string. What would the function reverseString("CodeSignal") return?
- CodeSignal
- langiSedoC (Correct answer)
- SignalCode
- lanigSedoC
Correct answer: langiSedoC
A string reversal function takes an input string and returns a new string with the characters in the opposite order. For "CodeSignal", the first character 'C' becomes the last, 'o' becomes second to last, and so on, until 'l' becomes the first character. Therefore, "CodeSignal" reversed is "langiSedoC".
Question 2: Given an array of numbers from 1 to 5, but one number is missing. Which function call will find the missing number from [1, 2, 4, 5]?
- missingNumber([1, 2, 4, 5]) → 3 (Correct answer)
- missingNumber([1, 2, 4, 5]) → 6
- missingNumber([1, 2, 4, 5]) → 0
- missingNumber([1, 2, 4, 5]) → 1
Correct answer: missingNumber([1, 2, 4, 5]) → 3
The problem states that the array should contain numbers from 1 to 5, but one is missing. By inspecting the given array [1, 2, 4, 5], we can see that the number 3 is absent from the sequence. A function designed to find the missing number in such a sequence would correctly identify 3 as the output.
Question 3: Which input for the function isPalindrome returns True?
- isPalindrome("CodeSignal")
- isPalindrome("level") (Correct answer)
- isPalindrome("coding")
- isPalindrome("12345")
Correct answer: isPalindrome("level")
A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. When checking "level", reading it from left to right gives "level", and reading it from right to left also gives "level". Therefore, "level" is a palindrome, and the `isPalindrome` function would return `True` for this input.
Question 4: How many vowels are in the string "CodeSignal"?
- 3 (Correct answer)
- 2
- 4
- 5
Correct answer: 3
Vowels in the English alphabet are A, E, I, O, U (and sometimes Y). In the string "CodeSignal", we can identify the vowels as 'o' (from "Code"), 'e' (from "Code"), and 'i' (from "Signal"). Counting these distinct vowels gives a total of three.
Question 5: Sum of Array Elements
- 15
- 16
- 17 (Correct answer)
- 18
Correct answer: 17
To find the sum of array elements, you simply add all the numbers together. Assuming the array provided in the context of the question sums to 17, adding all its elements would yield this result. For example, an array like [1, 2, 3, 4, 7] would sum to 17.
Write a function to reverse a string.
What would the function reverseString("CodeSignal") return?