Code Challenge: May 3, 2017
Every week, we feature the type of brain-teasing question that might be asked in a full-stack developer's job interview, similar to the FizzBuzz Challenge often used at places like Facebook and Google.
For this week's challenge, inspired by a question reported to have been asked in developer job interviews at Microsoft (and also inspired by Yoda in honor of Star Wars Day!):
Write a function that will take a given string and reverse the order of the words. "
Hello world" becomes "
world Hello" and "
May the Fourth be with you" becomes "
you with be Fourth the May"
You may assume that the string is a sentence that contains only letters and spaces, with all words separated by one space.
If you find this challenge easy, that's actually not unusual in interview situations, but interviewers often ask you to not only thoroughly explain your process but to up your game in an extension to the initial challenge, so you should definitely try for extra credit (see below)!
Scroll down and
reply to this thread with your code to participate! Don't just submit your code, remember to
explain your solution, too! If you want your entry to be considered under our "best code" assessment, you must link to your code running on repl.it to make assessing your entry easier (see below).
The string in question now has punctuation, for example "
Hello world!" and "
With you, be May the Fourth" Write a function to reverse the order of words, but keep the punctuation in place. For example, turn "
Hello world!"into "
world Hello!" and "
With you, be May the Fourth." to "
Fourth the, May be you With."
In the case of multiple consecutive punctuation marks or multiple consecutive whitespace characters, deal with them as you see fit.
This week, our easy level challenge is a little easier than normal, so only submissions at intermediate level and above will be eligible to "win" this challenge.
If you'd like to go even further:
Can you solve the initial challenge (without punctuation) in-place without using additional data structures, and with the time complexity being
O(n)? Can you do the same for the intermediate level challenge (including punctuation)?
See the full list and feedback here.
How to participate
Submissions in C++, C, or other languages not taught on Codecademy are welcomed, but these may not be eligible to "win" as a featured submission (see below)
Please include a link to your code running on repl.it, or as a backup CodePen, JSFiddle, or CodeBits so that our team (and your fellow students) can assess your submission easier and faster - if we have dozens of submissions, we will not have time to assess entries that do not have links in this manner.
If you want to post your code directly, please make sure that it is correctly formatted!
The best submission or submissions will be featured here on the forums and via various social media channels (like our Twitter)
Solutions submitted before 6:00pm in New York City on the Friday immediately following the code challenge are eligible for consideration as a featured "best entry."
To be considered as a "best submission," you must explain your thought processes in making your answer: how and why you designed your function the way you did. This way, we'll be more sure that you came up with your own solution, but crucially this is part of the reason why this type of question is asked in job interviews.
The fine print:
* Remember, the point of code challenges like these is to test and stretch yourself with an unusual problem, so don't be dissuaded if you find it difficult or don't know where to start! Start with Googling, but see if you can find out how to think about the problem and what tools you need to solve it, but don't just go looking for the solution itself. This way, it'll be a better learning exercise for you - developers can't always find and copy "the right answer" online, which is why questions like these are used in developer job interviews! Interviewers want to be able to see how you think through problems and not just whether or not you can solve them.
* If you are interested in "winning" the code challenge, please don't use any unusual repos or anything that will make it difficult for us or your fellow users to assess your answer quickly.
* Do you have a code challenge to share with other users? Issue it! Make a new topic with
[Challenge] in the title to open a challenge, maybe we'll even feature it in our next newsletter!