JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
<< PREV    [ 1 2 3 4 5 ]    NEXT >
Re: Foiled by a Div2-250! (response to post by aussie) | Reply
He wants abc1 to point back to the beginning after the end of routine.
To do it you can add other loop going with both pointers in the opposite directions until the character after *abc2 is \0
Re: Foiled by a Div2-250! (response to post by slex) | Reply
Yeah, I guess thats what I assumed I had to do (as did my interviewers!).

Has anyone got any more of these questions they have been posed in interviews?

They are frustrating but enjoyable at the same time.
Re: Foiled by a Div2-250! (response to post by Stroker_Ace) | Reply
Thing is, any question "write a function to do ..." is quite hard as interview questions go - often C++ questions are asking standard things like "why won't this compile" or "why will this not work right" (and 9 times out of 10 it's a question on inheritence, virtuals or copy constructors).
But being on TC means that's exactly what I do a lot already. I can't answer the questions requiring good algorithm knowledge but the kind asked in interview aren't likely to be of that sort - so I should be in a great position compared to most programmers.
Re: Foiled by a Div2-250! (response to post by slex) | Reply
void reverse(char *str1)
{
   char c = str[0];
   str[0]='\0';
   char *str2;
   while(*str1)
      str1++;
   str--;
   while(*str1){
      *str2=*str1;
      str2++;
      str1--;
   }
   *str2=c;
 
}


Is this ok?
Re: Foiled by a Div2-250! (response to post by slex) | Reply
void reverse(char * str1)
{
   char * str2=str1+strlen(str1)-1;
   while(*str1)
   {
       char c = *str2;
       *str2=*str1;
       *str1=c;
       str2--;
       str1++;
   }
   *str2='\0';
   //even when at the end str1 doesn`t point to the array. The pointer by value isn`t modified so the caller has the answer.
}
Re: Foiled by a Div2-250! (response to post by Stroker_Ace) | Reply
Well, the pointer is on the stack, so it doesn't matter if it is not reset to the beginning of the string. If it had been passed as an address to a pointer, that would have been a different story. Manipulating abc1 doesn't change the caller's abc1 pointer.

Edit: I didn't see that rserranop stated this in a comment in his code above.
Meh? (response to post by d000hg) | Reply
Maybe I'm missing something - is there something wrong with your code?

Looks fine to me. Sure, you didn't take advantage of the string coming in sorted - but in terms of production code, I like your method a lot more than a clever/unreadable solution that tries to work with str in place in one pass.

All in all, I don't think this problem gives you the opportunity to demonstrate anything substantive.

Whenever I've administered testing for applicants, I've done the following:

1. Narrowed the field by sending out a TopCoder style challenge involving a non-trivial problem.
2. For promising applicants, we have the coder in. I sit the coder down and have them work through a real problem with our normal development tools.

Naturally, this kind of thing is only part of evaluating an applicant - but it gives me a good feel for someone's coding skill and adaptability. Writing horrible code on a blackboard does not, at least for me.
Re: Meh? (response to post by jmzero) | Reply
I thought the exact same thing, i.e. that I was missing something. Turns out, there isn't actually anything wrong with his code. I think it's his wording in his post that's misleading, making it seem as though we're supposed to be searching for a bug. ;-)
Re: Meh? (response to post by NeverMore) | Reply
Not only that, it's very nearly the best you can do (you can break out of the loop when you've found everything, which should be better on longer inputs.)
Re: Meh? (response to post by Insight) | Reply
Given that the input is sorted (and heavily constrained), if you had a huge input you could do some binary-search-esque stuff to find which letters are present without looking at a large percentage of the letters. But without some idea that there's going to be large inputs, I don't think it makes sense to jump in with that kind of optimization.

And that's the problem with this kind of problem - even if you want to demonstrate some cleverness and have a good grasp of the problem, there's no way to predict how you're being judged. d000hg's solution would have met my criteria very well - it's easy to read, easy to add to, and performs well. Is that what they wanted? Or did they want a "edit-as-you-pass-through" clever solution (which I think is horrible - negligible performance benefit over d000hg's approach, and painful to work with)? Or did they want a high-performance, binary-search-ish solution?

Whatever solution they wanted, they could have come up with a challenge that would test that skill specifically - instead of a challenge that tests your applicants ability to guess what you're looking for.
Re: Foiled by a Div2-250! (response to post by Stroker_Ace) | Reply
void reverse(char *abc1) {
	char *abc2 = abc1;
	while(*abc2!=0) abc2++;
	abc2--;
	while(abc2>abc1) {
		*abc1 += *abc2;
		*abc2 = *abc1 - *abc2;
		*abc1 = *abc1 - *abc2;
		abc1++;
		abc2--;
	}
}


One less variable ;-)
Re: Meh? (response to post by jmzero) | Reply
d000hg does not get problem correctly.

1. If we are given length for some reasons - how to return length of resulting string ? Null-terminating - but why original string is no not null-terminated ?
2. Where the heck validation for length of string in his code ? For example is length is zero (or much worse - negative) - he is touching string for some reasons.
3. Lowercase letters - but is this guaranteed ? What if something wrong in input - his program will crash touching bad stack memory.


You must understand - TopCoder algorithm skills are DIFFERENT from real skills companies looking for. They need something a lot of text books does not teach - making programs work regardless on how stupid user input is and how hostile environment they are deployed.
Re: Meh? (response to post by TAG) | Reply
You must understand - TopCoder algorithm skills are DIFFERENT from real skills companies looking for.


I've worked as a programmer in "real" companies for around 9 years, managed a lot of hiring, and dealt with high level IT staff from a lot of large companies. I can say with some confidence that different companies prioritize a lot of different things.

Maybe the interviewer did want someone to do that input validation. Or maybe they think that's a waste of time, and the person should have just solved the problem given. If my experience is any guide, they likely wanted to see how clever an algorithm could be written. But honestly, you can't know what they were after, and as an interviewee there's no safe plan except perhaps to ask: Do you want me to validate inputs? Do you want me to do this with a small, simple algorithm or something faster?

As an interviewer, the smart and fair plan is to ask a better question.
Re: Meh? (response to post by jmzero) | Reply
Correct. Most of people simply don't get pointers thing at all.
But this is expected from best candidate to ask all those questions.
Re: Meh? (response to post by jmzero) | Reply
As a mainly commercial programmer I still find it hard to switch off the part of my brain that wants to validate the input provided in SRMs!

If I was interviewing someone and posed one of the problems stated in this thread then they would get a 'bonus point' for asking questions about the range and validity of input.
<< PREV    [ 1 2 3 4 5 ]    NEXT >

RSS