Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,453
4,156
Isla Nublar
Hi guys,

I've been working on getting my first programming job and I tried looking up programming test questions for entry level programming positions and they are either way too easy to be real (like write a program that counts from 10 - 1, or write a program to count words in a file), or way too hard (for entry level) such as questions relating to programming solutions for encryption programs relating to cloud storage and things like that that a beginner I would assume not be expected to know.

Does anyone have any real world examples of what kind of questions are asked on programming tests? Does anyone here interview programmers and could throw me a few questions by chance? Any help would be greatly appreciated :)

EDIT: I'm looking at questions for entry level C++ coding jobs or entry level iOS development jobs.
 

JoshDC

macrumors regular
Apr 8, 2009
115
0
In my quite limited experience for university and industrial placements, questions often are quite easy. This is because they're more interested in how you get to the answer.

One that I was given was "Sum all numbers between two arbitrary values". If you don't put much thought into you might be tempted by:

Code:
for (int i = start; i <= end; i++) {
    total += i;
}

Which is complexity n, and not really that great. Doing a bit of research (or remembering some math), you can use a summation formula, such as:

Code:
total = (start + end)*(end - start + 1) / 2

Which is constant complexity, and much better in the majority of situations.

Point is there'll usually be a number of different answers: some better than others. It's your job to walk through your thoughts, why you dismissed certain options, and why you chose the solution you did.

Again, with the word count question there are a number of different options (good and bad) to the one you claim is easy! It's probably worth mentioning when'd you'd use libraries/frameworks in production code. While it's clearly not the approach the question wants you to take, Cocoa has a method to enumerate words that will almost certainly have a better concept of what a "word" is than what you can come up with in an interview/short test.

A couple of other questions I can remember are:

How do you switch the value of two variables without an intermediary variable? (x,y = y,x in Python, but that's cheating!)
How do you efficiently store a sparse 2D list of values?

Hope this helps.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
The most recent interview I was conducting was for a DBA, and all of my questions were high-level, not asking to write queries, etc.

The most recent interview I was subjected to was not entry level, but the questions weren't all incredibly involved. One round was designing the data model for an app. Two were get-to-know-you (be able to answer what your biggest flaw is, how you'd deal with a hostile co-worker, etc.), one round was Java-specific because it's a Java shop (write a dice game implementation on the whiteboard, what does this code do, set vs. List, hashing, etc.), one round was SQL... joins, subqueries, aggregates, etc. All of this was about 5 hours one afternoon after a 1 hr phone interview with a smattering of all of that.

If you're going entry-level I don't know how technical they'll get. It depends on the interviewer. Write some code on paper. Not perfect, but good enough. Logically sound if not syntactically correct. How do you handle scarce resources like file handles? If you use the XOR trick for an in-place swap be sure to check for pointer equivalence or you'll wipe your value. Hell, you could just check for equivalence of the values.

Be ready to solve a problem you've never thought about before. Get the rules to some games and write pseudo code to solve them. Know data structures. What's a map/dictionary? What's a linked list? What's a binary tree, what's the cost to rebalance it? When is the right time to use these?

Know things about your "target" language. What's a pointer? What's pass by value? A pointer by value? By reference? By const reference? What's good and bad about these? Know constructors and destructors. Know exception handling. Know inheritance. If you don't know one of these and can't figure it out in time, be honest. I'd rather someone admit that than guess wrong.

They're going to want to see how you work and how you'll fit in as much as they want to see what you know. Be yourself. If you're fake, ingenuine, etc they'll figure it out eventually.

Good luck.

-Lee
 

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,453
4,156
Isla Nublar
In my quite limited experience for university and industrial placements, questions often are quite easy. This is because they're more interested in how you get to the answer.

One that I was given was "Sum all numbers between two arbitrary values". If you don't put much thought into you might be tempted by:

Code:
for (int i = start; i <= end; i++) {
    total += i;
}

Which is complexity n, and not really that great. Doing a bit of research (or remembering some math), you can use a summation formula, such as:

Code:
total = (start + end)*(end - start + 1) / 2

Which is constant complexity, and much better in the majority of situations.

Point is there'll usually be a number of different answers: some better than others. It's your job to walk through your thoughts, why you dismissed certain options, and why you chose the solution you did.

Again, with the word count question there are a number of different options (good and bad) to the one you claim is easy! It's probably worth mentioning when'd you'd use libraries/frameworks in production code. While it's clearly not the approach the question wants you to take, Cocoa has a method to enumerate words that will almost certainly have a better concept of what a "word" is than what you can come up with in an interview/short test.

A couple of other questions I can remember are:

How do you switch the value of two variables without an intermediary variable? (x,y = y,x in Python, but that's cheating!)
How do you efficiently store a sparse 2D list of values?

Hope this helps.

Wow thanks so much for that! I never thought about optimizing a simple loop like that but it makes perfect sense. I could even take that division out and multiply it by 0.5 (something I do frequently in graphics code).

Sadly I cannot answer the switching variables without an intermediate question :( I'd assume maybe using bitwise operators but again, without researching I honestly can't answer that :/

Even worse is I don't know what a sparse 2D list of values is :( I'm assuming based of other code I've ran across (and feel free to correct me since I know I'm about to be wrong) that a sparse list would be something like numbers with large differences in values, like 1, 450, 45632, 300,000 etc. To store it efficiently I'd assume you'd use hash tables but again, I really have no clue on that one without looking something up.

I'm starting to not feel so good about myself : / (No matter I'll learn and get better).


The most recent interview I was conducting was for a DBA, and all of my questions were high-level, not asking to write queries, etc.

The most recent interview I was subjected to was not entry level, but the questions weren't all incredibly involved. One round was designing the data model for an app. Two were get-to-know-you (be able to answer what your biggest flaw is, how you'd deal with a hostile co-worker, etc.), one round was Java-specific because it's a Java shop (write a dice game implementation on the whiteboard, what does this code do, set vs. List, hashing, etc.), one round was SQL... joins, subqueries, aggregates, etc. All of this was about 5 hours one afternoon after a 1 hr phone interview with a smattering of all of that.

If you're going entry-level I don't know how technical they'll get. It depends on the interviewer. Write some code on paper. Not perfect, but good enough. Logically sound if not syntactically correct. How do you handle scarce resources like file handles? If you use the XOR trick for an in-place swap be sure to check for pointer equivalence or you'll wipe your value. Hell, you could just check for equivalence of the values.

Be ready to solve a problem you've never thought about before. Get the rules to some games and write pseudo code to solve them. Know data structures. What's a map/dictionary? What's a linked list? What's a binary tree, what's the cost to rebalance it? When is the right time to use these?

Know things about your "target" language. What's a pointer? What's pass by value? A pointer by value? By reference? By const reference? What's good and bad about these? Know constructors and destructors. Know exception handling. Know inheritance. If you don't know one of these and can't figure it out in time, be honest. I'd rather someone admit that than guess wrong.

They're going to want to see how you work and how you'll fit in as much as they want to see what you know. Be yourself. If you're fake, ingenuine, etc they'll figure it out eventually.

Good luck.

-Lee

Thank you so much for the advice! :) But again, ouch. Although I could write many game pseudocode implementations (I focus on games in my programming), I could not explain what checking for equivalence in values means without googling it. I haven't ran across that in any of the roughly 8 C++ books I've gone through over the years. (I started learning it in 1998 I believe). I am also very unfamiliar with bitwise operators. I've seen them, but no books (the ones I have anyway) go into detail about how or where to use them. The closest I've gotten was "They're used for flags". Again, something else I will definitely brush up on. (I'm making quite a long list!)

As for data structures, I'm familiar with some of them, not all. I don't know what a map is but I do know dictionaries, linked lists, binary trees, and a few others are. I do not know the cost to balance one. I hope that I just skimmed over that in one of my data structures books and its in fact in there somewhere.

As for pointer, references, const references, constructors, destructors, etc, I am good with all of these, same with inheritance. I'm sure there is plenty to learn but I use these on a regular basis. One thing that caught my eye was the bad about const references. I'm sure I could think of a reason eventually but right off the top of my head I cannot.

Again, thanks so much for the advice!


Try the practice stuff at http://codility.com/

B

This site is awesome! Thanks so much! Sadly I took the practice test and failed. Well, not really failed since I spent 10 min on it and had to leave but I didn't fully understand the question on it which is pretty bad :( I'm going to try it again later when I have more time and hopefully the answer will pop in there.

Thanks so much for the advice guys, I thought I was ready for a programming job but it doesn't seem so :( Even though I can generally get anything I've tried running smoothly I've been having trouble answering some of the basics thrown at me here.

No matter though, I will simply keep practicing. I practice every day but not as much as I need to, school really does get in my way. (100 hours of school work a week that isn't programming doesn't help things). Luckily I graduate in October and will have plenty of time then to not only finish an iOS game I've been working on for 3 months but also time to sit down and really analyze my programming skills, what I know and don't know and focus on what I need to.

Thank you guys again for the suggestions. It really helps me to know what to study and how to get my skills in order to get my first programming job. If anyone has any other suggestions please feel free to comment :)
 

JoshDC

macrumors regular
Apr 8, 2009
115
0
Even worse is I don't know what a sparse 2D list of values is I'm assuming based of other code I've ran across (and feel free to correct me since I know I'm about to be wrong) that a sparse list would be something like numbers with large differences in values, like 1, 450, 45632, 300,000 etc. To store it efficiently I'd assume you'd use hash tables but again, I really have no clue on that one without looking something up.

You're right about what it is. I think that's my fault for not explaining it well. The solution I was given was a linked list of linked lists, but in some ways I think the hash table is better!

Sadly I cannot answer the switching variables without an intermediate question I'd assume maybe using bitwise operators but again, without researching I honestly can't answer that :/

You're on the right track with bitwise operators. It may help to simplify it to switching two integer variables, in which case you don't use bitwise operators.

Don't feel bad about not getting the questions straight away. They were interview questions and designed to see how you approach a problem, not if you know a load of obscure techniques. I don't think they would have looked that favourably on someone who just gave a load of model answers without justification.
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
I am also very unfamiliar with bitwise operators. I've seen them, but no books (the ones I have anyway) go into detail about how or where to use them. The closest I've gotten was "They're used for flags". Again, something else I will definitely brush up on. (I'm making quite a long list!)

Your starting to realize that you can't possibly know everything. It's a perfectly acceptable answer to say you don't know something.

I hate interviewing, as it's mostly ********. Seriously, I would probably walk out on being asked to write a little simulation game. It has no basis in reality for most positions (I know they like to see how people problem solve), but most jobs don't require this level of thinking, and they certainly don't require you to do this on the spot.

As far as bitwise operators, they aren't used all that much outside of communications and something that has way too many flags.

A quick example: instead of having eight separate boolean values to describe a bunch of yes/no type descriptions, you can have one byte (8 bits that can have a 1 or 0) to describe the same thing with much less storage. When your sending stuff out on the net in large quantities (ie. stock quotes - 320k quotes / second), 8 separate bytes to describe something, compared to 1 byte does make a noticeable difference.

The other place you see them is when calling routines like window setup, where you define what visual elements you want to be shown (ie. border, etc.). But like others have said, you can convert to to an integer to deal with it.

There's also some cool things you can do with fast division/multiplication using the shift operators << and >>, but honestly, you don't see it all that much. In the old days when your CPU was slow and you had limited memory, you sought out these types of optimizations, but aside from embedded applications, there isn't a real need for it.

You out of college, stick with the type of questions you've been looking at and don't worry about what you don't know.

One question I was asked recently was to convert a number into Roman Numerals. I did mine the way you probably would when you google it, but the guy showed me a faster way. Good academic exercise, but your chances of pulling the most optimal answer out of the air are pretty slim.

Like Lee said, some of it is technical, a lot of it is personality and would you fit in here type of things.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
The XOR in-place swap is a "classic" trick. It uses this property of exclusive-or:
(A XOR B) XOR A => B
This is because A XOR A always yields all 0 bits, and 0 XOR A always yields A.

Code:
void inPlaceSwap(int *a, int *b) {
  if(a == b) return;
  if(a == NULL || b == NULL) return;
  *a = *a ^ *b; //a' now contains a XOR b
  *b = *a ^ *b; //b' now contains a
  *a = *a ^ *b; //a' now contains b
}

My statement about comparing the values would be doing *a == *b instead of a == b. The reason it's critical to at least check for pointer equivalence is if you called this:
inPlaceSwap(&x,&x);
Then you'll lose your value in x, because x ^ x will always yield 0.

As for a map, a dictionary is the same as map. In perl it's just called a hash. It's all the same thing, a map of keys to values.

A balanced binary tree can decay to a linked list, pushing the log(n) complexity of locating an element to n. Rebalancing can put things "back in line".
http://en.wikipedia.org/wiki/Tree_rotation
This may be academic minutiae, but some interviewers are dicks.

If you can't think of anything bad about something, that's a fine answer. But you should be able to say when passing a const ref is inappropriate (i.e. when you need to change it).

If you get your iOS game in the app store, definitely mention that.

You still have a number of months to prepare, and when you do they're still going to catch you off guard. Be confident in what you do know and frank about what you don't.

-Lee
 

itickings

macrumors 6502a
Apr 14, 2007
947
185
Wow thanks so much for that! I never thought about optimizing a simple loop like that but it makes perfect sense. I could even take that division out and multiply it by 0.5 (something I do frequently in graphics code).

One other thing to keep in mind is the difference between floating-point numbers and integers.

Dividing by the integer 2 is not necessarily equal to multiplying by the floating-point number 0.5. The two can differ both in performance and in the actual result depending on the context.
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
One other thing to keep in mind is the difference between floating-point numbers and integers.

Dividing by the integer 2 is not necessarily equal to multiplying by the floating-point number 0.5. The two can differ both in performance and in the actual result depending on the context.

Additionally, a floating point operation in the CPU is generally more expensive than an integer operation.

The divide by 2 can be removed by replacing it with a shift by one bit.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Additionally, a floating point operation in the CPU is generally more expensive than an integer operation.

The divide by 2 can be removed by replacing it with a shift by one bit.

As long as the left operand is positive and you don't mind the obfuscation. If you've found the bottleneck in your code to be an integer divide by 2 (that the compiler probably already optimized), you're in dire straits.

-Lee
 

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,453
4,156
Isla Nublar
Thank you guys again for all of the tips and advice!

I ended up landing an interview for a job. I'm obviously nervous, not about the questions since I'm very good at interview questions (I used to interview people myself at many places) but of the test!

I know I can figure out anything thrown at me unless its something way out there but it'll be timed so I'm nervous!

Wish me luck, and thanks again for all your help Mac Rumors! :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.