Game Career Guide Forums

Game Career Guide Forums (http://www.gamecareerguide.com/forums/index.php)
-   Programming (http://www.gamecareerguide.com/forums/forumdisplay.php?f=7)
-   -   C++ Coding Challenges (http://www.gamecareerguide.com/forums/showthread.php?t=4240)

Killerapathy 11-02-2010 08:59 PM

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.

ndimucci 11-03-2010 04:25 AM

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.

dmmik 11-03-2010 06:34 AM

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

Code:

bool IsPalendrome(char* str)
{

}


yaustar 11-03-2010 08:49 AM

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

ndimucci 11-03-2010 09:59 AM

Quote:

Originally Posted by yaustar (Post 22275)
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.

yaustar 11-04-2010 01:29 AM

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

Tonsko 12-23-2010 06:18 AM

Quote:

Originally Posted by dmmik (Post 22274)
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.

Protector one 12-23-2010 12:23 PM

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.)

yaustar 01-16-2011 09:21 AM

Quote:

Originally Posted by cuxxie (Post 23139)
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;
}


cowsonparade 02-24-2011 08:43 AM

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. :D


Quote:

Originally Posted by Tonsko (Post 22904)
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.



All times are GMT -8. The time now is 04:39 PM.

Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.