Get the latest Education e-news
 

Go Back   Game Career Guide Forums > Programming
Forum Home Register Members List Mark Forums Read

Closed Thread
 
Thread Tools Display Modes
Old 11-02-2010, 08:59 PM   #1
Killerapathy
Junior Member

Activity Longevity
0/20 14/20
Today Posts
0/11 sssssss23
Default 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 (recursion)

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.
Killerapathy is offline  
Old 11-03-2010, 04:25 AM   #2
ndimucci
Senior Member

Activity Longevity
0/20 17/20
Today Posts
0/11 ssssss156
Location: CT
Default

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
http://www.linkedin.com/in/ndimucci
ndimucci is offline  
Old 11-03-2010, 06:34 AM   #3
dmmik
Junior Member

Activity Longevity
0/20 13/20
Today Posts
0/11 sssssss17
Default

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

Code:
bool IsPalendrome(char* str)
{

}
dmmik is offline  
Old 11-03-2010, 08:49 AM   #4
yaustar
Administrator
 
yaustar's Avatar

Activity Longevity
2/20 19/20
Today Posts
1/11 sssss2270
Location: UK
Default

Try the demo test on http://codility.com/
yaustar is offline  
Old 11-03-2010, 09:59 AM   #5
ndimucci
Senior Member

Activity Longevity
0/20 17/20
Today Posts
0/11 ssssss156
Location: CT
Default

Quote:
Originally Posted by yaustar View Post
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
http://www.linkedin.com/in/ndimucci
ndimucci is offline  
Old 11-04-2010, 01:29 AM   #6
yaustar
Administrator
 
yaustar's Avatar

Activity Longevity
2/20 19/20
Today Posts
1/11 sssss2270
Location: UK
Default

The free demo test itself is usually pretty decent. Codility is for the hiring managers to sends tests out rather then preparing for interviews.
yaustar is offline  
Old 12-23-2010, 06:18 AM   #7
Tonsko
Junior Member

Activity Longevity
0/20 11/20
Today Posts
0/11 ssssssss8
Location: UK
Default

Quote:
Originally Posted by dmmik View Post
Here is a whiteboard C coding question I was asked at an actual game company interview. Complete this function:

Code:
bool IsPalendrome(char* str)
{

}
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 Tonsko : 12-23-2010 at 06:24 AM.
Tonsko is offline  
Old 12-23-2010, 12:23 PM   #8
Protector one
Moderator

Activity Longevity
2/20 17/20
Today Posts
1/11 ssssss698
Location: Netherlands
Default

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.)
__________________
My name is Louis. Hi!
Uh oh! The princess is in another signature!
Protector one is offline  
Old 01-16-2011, 09:21 AM   #9
yaustar
Administrator
 
yaustar's Avatar

Activity Longevity
2/20 19/20
Today Posts
1/11 sssss2270
Location: UK
Default

Quote:
Originally Posted by cuxxie View Post
int strlen(char * sz)
{
if((*sz)!='\0') return (strlen(++sz)+1);
else return 0;
}

string length counter... 2 lines..
Technically, that is 4 logical lines of code. Otherwise we could do this and call it one line of code.

Code:
int strlen(char * sz)
{
	if((*sz)!='\0') return (strlen(++sz)+1); else return 0;
}
yaustar is offline  
Old 02-24-2011, 08:43 AM   #10
cowsonparade
Junior Member

Activity Longevity
0/20 11/20
Today Posts
0/11 ssssssss9
Lightbulb

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 View Post
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
cowsonparade is offline  
Closed Thread


Thread Tools
Display Modes

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


Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
All times are GMT -8. The time now is 08:28 AM.






UBM Tech