Interview Challenge (Word Reversal)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Interview Challenge (Word Reversal)

 
0
  #1
Jul 13th, 2007
Let's pretend that I'm interviewing you for a C++ job. I want to know how you[1] approach problems and the extent of your C++ prowess, so I'm going to ask you to solve short problems in C++[2]. These problems range from beginner to advanced, and may be more than they seem. Good luck!

[1] "You" being collective. Everyone is welcome to put their heads together and/or build on the questions or solutions of everyone else. In fact, I encourage it.

[2] Each problem will have a separate thread, to keep things on-topic. If it turns out that this is a fun game, I'll post new problems regularly.

Okay then, here we go:

Problem: Write some code that reverses words.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Interview Challenge (Word Reversal)

 
0
  #2
Jul 13th, 2007
Let's pretend you actually have some real work to do
Last edited by iamthwee; Jul 13th, 2007 at 5:51 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Interview Challenge (Word Reversal)

 
0
  #3
Jul 13th, 2007
Let's pretend that I'll heavily moderate this thread to keep it on-topic.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Interview Challenge (Word Reversal)

 
0
  #4
Jul 13th, 2007
Are you also pretending you are a c++ employer? Cos all this presence is confusing me.

[edit]Sorry I'm being really stupid, ignore me[/edit]
Last edited by iamthwee; Jul 13th, 2007 at 6:01 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Interview Challenge (Word Reversal)

 
0
  #5
Jul 13th, 2007
because i happen to think you are one of the brighter minds i have met in a while i will take you up on your challenges. i will post back shortly with some code :-D
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 488
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Interview Challenge (Word Reversal)

 
0
  #6
Jul 13th, 2007
Originally Posted by Narue View Post
Problem: Write some code that reverses words.
My first thought which springs to mind, is, how would you define a 'word'?

is it..
  • A sequence of alphanumeric characters
  • A sequence of alphanumeric characters & punctuation
  • A sequence of purely Alphabetical characters
  • Assuming any particular character set (ASCII, Unicode, etc)

My next thought... what do you mean by "reverses words"
  • Reverses the order of a list of "words" (assuming "word" is defined)
  • Reversing the order a set of characters appear in a word
  • A combination of the above (Which would be more like reversing a 'sentence')


Those questions aside, here's how i'd do it in C++ (Making assumptions about the above, of course )
  1. #include <iostream>
  2. #include <ostream>
  3. #include <string>
  4. #include <sstream>
  5. #include <deque>
  6. #include <algorithm>
  7. #include <functional>
  8.  
  9. class reverse_word : public std::unary_function<std::string, std::string>
  10. {
  11. public:
  12. std::string& operator()(std::string& str)
  13. {
  14. std::reverse(str.begin(), str.end());
  15. return str;
  16. }
  17. };
  18.  
  19. int main()
  20. {
  21. std::string str ("the quick brown fox jumps over the lazy dog");
  22. std::cout << str << std::endl;
  23.  
  24. std::stringstream ss (str);
  25. typedef std::deque<std::string> list_t;
  26. list_t word_list;
  27. std::string temp;
  28. while( ss >> temp )
  29. word_list.push_back(temp);
  30. std::for_each(word_list.begin(), word_list.end(), reverse_word() );
  31. list_t::const_iterator i;
  32. for( i = word_list.begin() ; i != word_list.end() ; ++i )
  33. std::cout << *i << " ";
  34. }
Last edited by Bench; Jul 13th, 2007 at 7:08 pm.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Interview Challenge (Word Reversal)

 
0
  #7
Jul 13th, 2007
>My first thought which springs to mind, is, how would you define a 'word'?
That's an excellent reaction. After a moment's thought, I'm thinking we (the imaginary company you're interviewing for) could use something like this to process subroutine parameters in a compiler while building a stack frame. The parameters are evaluated from last to first, but written first to last. So for our needs a word is a valid identifier (our compiler uses the same identifier rules as C++).

>My next thought... what do you mean by "reverses words"
Another good question. We'll be using the parameter identifiers themselves as keys into a map for the symbol table, so it's probably not a good idea to modify the source strings. So in our case, the list [arg1, arg2, arg3] would be reversed into [arg3, arg2, arg1].

>Those questions aside, here's how i'd do it in C++
I like it. But I forgot to tell you that our development environment is extremely buggy and resource intensive (management is looking into replacing it). Using the standard library would be a bad idea for this component since it'll be used very often during a compilation phase.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Interview Challenge (Word Reversal)

 
0
  #8
Jul 13th, 2007
simple and it works

  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <cstdlib>
  5. using namespace std;
  6. void reverseword(char * reverse) {
  7. size_t size = strlen(reverse); //get the size of the char *
  8. for(size_t i = 1; i <= size; i++)
  9. {
  10. cout << reverse[size - i]; //output the word
  11. }
  12. }
  13. void reverseword (string reverse) {
  14. size_t size = reverse.size();
  15. string::reverse_iterator walkword;
  16. for(walkword = reverse.rbegin(); walkword < reverse.rend(); walkword++)
  17. {
  18. cout << *walkword;
  19. }
  20. }
  21. int _tmain(int argc, _TCHAR* argv[])
  22. {
  23. cout << "Method one for string reversal" << endl;
  24. cout << "==============================" << endl;
  25. cout << "== Using Char * and cin.getline" << endl;
  26. cout << "== using for statement to itterate" << endl;
  27. cout << "==============================" << endl;
  28.  
  29. char * getwords = new char(); //will hold the words
  30. cout << "Please enter the words to reverse" << endl << endl << endl;
  31. cin.getline(getwords,100); //get the word(s) to reverse
  32. reverseword(getwords); //reverse the words
  33. cin.sync();//clean up buffer for next example
  34.  
  35. cout << endl << endl << endl;
  36. cout << "Method Two for string reversal" << endl;
  37. cout << "==============================" << endl;
  38. cout << "== Using std::basic_string cin.getline" << endl;
  39. cout << "== using build in itterator members" << endl;
  40. cout << "==============================" << endl;
  41.  
  42. string getwordsagain;
  43. cout << "Please enter the words to reverse" << endl << endl << endl;
  44. getline(cin, getwordsagain);
  45. reverseword(getwordsagain);
  46. cin.sync(); //clean the buffah
  47. return 0;
  48. }

i know of a few things i could do to optimize but for the question it works...or so i believe
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Interview Challenge (Word Reversal)

 
0
  #9
Jul 13th, 2007
>simple and it works
Your code exhibits undefined and implementation-defined behavior (start a new thread asking why if you want details). It also alters the words, which won't work for us, we need the words to be reversed without also reversing the characters in each word. But it's a start.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Interview Challenge (Word Reversal)

 
1
  #10
Jul 13th, 2007
Originally Posted by Narue View Post
>simple and it works
Your code exhibits undefined and implementation-defined behavior (start a new thread asking why if you want details). It also alters the words, which won't work for us, we need the words to be reversed without also reversing the characters in each word. But it's a start.
cool i started a new thread.

so from what i am gathering if i were to input the string

hello world

you dont want

dlrow olleh

you want

olleh dlrow

the words reversed but their location is intact?



i figure i should ask questions now rather than later
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC