943,657 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12525
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 13th, 2007
0

Interview Challenge (Word Reversal)

Expand Post »
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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 13th, 2007
0

Re: Interview Challenge (Word Reversal)

Let's pretend you actually have some real work to do
Last edited by iamthwee; Jul 13th, 2007 at 5:51 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 13th, 2007
0

Re: Interview Challenge (Word Reversal)

Let's pretend that I'll heavily moderate this thread to keep it on-topic.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 13th, 2007
0

Re: Interview Challenge (Word Reversal)

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 13th, 2007
0

Re: Interview Challenge (Word Reversal)

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
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Jul 13th, 2007
0

Re: Interview Challenge (Word Reversal)

Click to Expand / Collapse  Quote originally posted by Narue ...
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 )
CPP Syntax (Toggle Plain Text)
  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.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Jul 13th, 2007
0

Re: Interview Challenge (Word Reversal)

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 13th, 2007
0

Re: Interview Challenge (Word Reversal)

simple and it works

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Jul 13th, 2007
0

Re: Interview Challenge (Word Reversal)

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 13th, 2007
1

Re: Interview Challenge (Word Reversal)

Click to Expand / Collapse  Quote originally posted by Narue ...
>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
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: ordered Linked List Copy function
Next Thread in C++ Forum Timeline: the middle character of a string





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC