Get the latest Education e-news

 Game Career Guide Forums C++ Coding Challenges
 Forum Home Register Members List Mark Forums Read

11-02-2010, 08:59 PM   #1
Killerapathy
Junior Member

C++ Coding Challenges

I have been learning C++ for over a year now and I would like to ask the community for some C++ coding challenges or puzzles. My professors have given me ones such as ...

int strlen(char * sz)
{

}
note: solution only took 3 lines using ([COLOR="White"]recursion[/COLOR])

OR

"Write a C function that multiplies an input integer by 5 and returns the result. The function must not use the multiplication operator. Optimize for speed."
I believe this one actually goes into the bits.

Anyways, I wonder if you could share some of yours in hopes of challenging others.

11-03-2010, 04:25 AM   #2
ndimucci
Senior Member

Location: CT

http://www.codechef.com/

Up top you'll see a "Practice" tab which will provide problems to solve. There are even competitions in which you can enter. It's a great site.
__________________
Software Developer

11-03-2010, 06:34 AM   #3
dmmik
Junior Member

Here is a whiteboard C coding question I was asked at an actual game company interview. Complete this function:

Code:
```bool IsPalendrome(char* str)
{

}```

12-23-2010, 06:18 AM   #4
Tonsko
Junior Member

Location: UK

Quote:
 Originally Posted by dmmik Here is a whiteboard C coding question I was asked at an actual game company interview. Complete this function: Code: ```bool IsPalendrome(char* str) { }```

I saw this last night while browsing, and it's a nice little problem, if not especially taxing. As a novice, I originally thought it would be best to break it down into an array and do something complicated such as reversing the order of the array and then comparing them. But there is no need - after further reading, having seen the string tools available. Once I was thinking along those lines, it took about 20 mins.

So here's a solution (in python rather than C) - anyone got any optimisation tips?

Code:
```#!/usr/bin/python

def isPalindrome(string):
length = len(string)
#print length

i=0
j=length

while j > 0:
if string[i] == string[j-1]:
#print string[i],string[j-1]
i=i+1
j=j-1
else:
print 'Not a palindrome'
break

if j == 0:
print 'Word is a palindrome'
else:
pass

input = raw_input()
isPalindrome(input)```
The mild advantage of this is that it's not typed, so works equally well with palindromic numbers. For the c problem, I guess you'd have to put in some type checking to sanitise the input before doing anything.

Last edited by Tonsko : 12-23-2010 at 06:24 AM.

02-24-2011, 08:43 AM   #5
Junior Member

Well, depends on the definition of optimization:

Code:
```def isPalindrome(string):
#More of a C-style optimization for memory usage
for i in range(len(string)/2): #rounds down, which is OK because middle letter can be ignored
if string[i] != string[-i-1]:
return False
else: return True

def isPalindrome(string):
#Optimizes developer time XD
return string == reverse(string)```
I'm a big fan of http://projecteuler.net/ for programming puzzles. A friend of mine would try to solve each one with 1 line.

Quote:
 Originally Posted by Tonsko Apologies for thread resurrection. I saw this last night while browsing, and it's a nice little problem, if not especially taxing. As a novice, I originally thought it would be best to break it down into an array and do something complicated such as reversing the order of the array and then comparing them. But there is no need - after further reading, having seen the string tools available. Once I was thinking along those lines, it took about 20 mins. So here's a solution (in python rather than C) - anyone got any optimisation tips? Code: ```#!/usr/bin/python def isPalindrome(string): length = len(string) #print length i=0 j=length while j > 0: if string[i] == string[j-1]: #print string[i],string[j-1] i=i+1 j=j-1 else: print 'Not a palindrome' break if j == 0: print 'Word is a palindrome' else: pass input = raw_input() isPalindrome(input)``` The mild advantage of this is that it's not typed, so works equally well with palindromic numbers. For the c problem, I guess you'd have to put in some type checking to sanitise the input before doing anything.

Last edited by cowsonparade : 02-24-2011 at 08:43 AM. Reason: Code, not quote

11-03-2010, 08:49 AM   #6
yaustar

Location: UK

Try the demo test on http://codility.com/

11-03-2010, 09:59 AM   #7
ndimucci
Senior Member

Location: CT

Quote:
 Originally Posted by yaustar Try the demo test on http://codility.com/
Awesome site. Annoying that you have to pay for it, but probably worth it to better prepare for interviews and such.
__________________
Software Developer

11-04-2010, 01:29 AM   #8
yaustar

Location: UK

The free demo test itself is usually pretty decent. Codility is for the hiring managers to sends tests out rather then preparing for interviews.

12-23-2010, 12:23 PM   #9
Protector one
Moderator

Location: Netherlands

As for "optimizations", instead of, "while j > 0", you could use, "while j > length*.5", since you've already checked the first n/2 letters of the word. :P
(I might be off by one. Sorry for that, if so.)
__________________
[COLOR="#000000"]My name is Louis. Hi![/COLOR]
[COLOR="#F5F5FF"]Uh oh! The princess is in another signature![/COLOR]

09-28-2012, 10:21 PM   #10
CY5
Junior Member

Re: C++ Coding Challenges

/*"Write a C function that multiplies an input integer by 5 and returns the result. The function must not use the multiplication operator. Optimize for speed."*/

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a;
cout<<"enter no:";
cin>>a;
getch();
return 0;
}
{
a=a+a+a+a+a;
cout<<a;
}

 Thread Tools Display Modes Hybrid Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off