Could you answer this coding question right now in an interview?
Back this week with another coding challenge!
Sometimes in a coding interview, you’ll get a quick take-home challenge from the company to make sure they want to bring you in for an onsite.
This weeds out the interviewees who aren’t ready for the onsite or might not be a fit for what the company is looking for right now.
Palindrome check: Write a function to check if a given string is a palindrome.
Input: isPalindrome("Able was I, ere I saw Elba.")
Expected Output: True
Can you solve it if you were asked about it in an interview tomorrow?
To check if a string is a palindrome, you need to write a function that takes the input and outputs whether the string is a palindrome or not.
For example, if the input string is "racecar," the function should return "True" as "racecar" is a palindrome.
Simple enough, right?
Here’s the issue, you are going to be expected to be able to write this without using a built-in method for checking palindromes.
And you’ll be expected to talk about how to optimize it and why you chose that approach.
These types of questions can seem boring but they can help you solve much more complex problems later! And they are the building blocks of a solid technical interview.
Let’s dive in with the pseudocode to start!
In a coding interview, it’s always best to start out with pseudocode. If you can spend the first few minutes really going over the approach you’ll use, you can determine if the interviewer is happy with the solution you’re going to write out.
If you jump right into coding without discussing your plan, you can miss important steps that the interviewer can point out to you.
So,to check if a string is a palindrome, we can follow these pseudocode steps..
1. Define a function `isPalindrome` that takes a string as the argument.
2. Create a variable to store the reversed string, let's call it….. `reversedString`.
3. Next, iterate over the characters of the string from the very last character to the first. And for each character, append it to the `reversedString`.
4. Finally, compare the `reversedString` with the original string. If they are the same, return `True`. Otherwise, return `False`.
1. Our function `isPalindrome` takes a string `str` as an argument.
2. Next, we create an empty string variable `reversedString` to store our reversed string.
3. Using a for loop, we loop over the characters of the input string `str` from the last character to the very first.
4. Inside our loop, we append each character from `str` to the `reversedString` variable.
5. Finally, we check if the `reversedString` is equal to the original string `str`. If they are the same, we return `True`, indicating that the string is a palindrome. Otherwise, we return `False`.
Want to try to implement this and come back?
How’d it go?
Here’s the solution!
How'd you do? Tag me on Twitter @randallkanna with your solution!
It’s always best to write some pseudocode first in a coding interview so you can collect your thoughts and see if your potenti
Let's tackle a new coding challenge today - finding the missing number in a sequence.