Using object oriented design, white board a high level car class using inheritance, encapsulation, etc.
After the candidate comes up with a plausible solution, ask how they would change the design to create a bicycle using the same classes.
The answer is just looking for some sort of CWheeledVehicle class that can instance a variable number of CWheels, but it trips up a lot of people.
imo its a better question than those “use bitwise operators to find power of two” questions. Like you said, I want to answer those with “google.com”
The only problem with the bicycle question I can see is it may require some leading, by default would not keep track of tires on a car unless I was told/asked that it was a game with specific tire damage or it was something to simulate a real life car completely or something. Though that may be a good thing since it encourages them to ask questions, Which is a good lesson for those looking into interviews!
Ask lots of questions. If you get that question about the cars(for example) be sure to ask stuff like what it’s for. For example, in a racing game you wouldn’t care at all about how many seats the car has, but halo/gta etc… you totally would. If the game has no damage to individual tires you don’t need to keep track of tires. If you get asked an open ended question like that try and narrow it down, and if you start working make sure you state any assumptions you’re making. I ask questions even on the simple stuff IE “How do you find an arbitrary number in an array” or any of the other simple array questions, I always ask “Is it sorted?” and if it’s sorted “Do we know the min/max values”, “are there repeating values” etc…
It’s a good lesson both in the real world and something interviewers look for, in the real world it makes sure you don’t waste work implementing something the wrong way and makes sure both you and the designer/client/whatever are exactly on the same page.
That’s a good point. When I was being interviewed for my current job, he asked me “If I was going to ask you to program a dice rolling function, how would you do it?”
and my first response was “How many sides is the dice?”
And he smiled and said “Exactly.” Asking questions isn’t something to be afraid of, and it’s the right move when designing software
Yeah, at a shop that does a lot of OOP and design pattern work, so that sort of question is used to test for basic ability to think in object-oriented terms. Like IEngine class with functions that take fuel and output torque. CGasEngine inherits from IEngine, CDieselEngine inherits from IEngine. CCar encapsulates an instance of CGasEngine, CTruck encapsulates an instance of CDieselEngine, CBicycle doesn’t have any IEngine. CCar has an array of four wheels and one instance of wheel for the spare tire, CTruck has an array of 18 wheels, etc etc etc
If a candidate can’t BS about high-level class design like that for a while, they probably don’t have a firm grasp of inheritance and encapsulation.
so tl;dr:
in the car/bike/truck question, the vehicle is for to demonstrate the candidate’s knowledge of object oriented design. If they start pontificating about particle physics, angular momentum, or usefulness of UML diagrams, they are probably failing the question
I’m currently working on a smaller version that goes from 0 to 99 only to get me started but I’d like to see how you guys would approach this. Thanks in advance!
So the real question on this assignment is can you figure out how to get each individual digit of an arbitrary number(and maybe taking input if you haven’t done that). There’s a couple ways to get the individual digit using division and/or modulus and taking into consideration we’re in base 10. There may even be a better way than that but I’ve never tried to optimize this problem.
if (number / 100 > 0)
{
//the number is at least 100, get the hundreds digit
int hundredsDigit = number / 100;
}
You would probably have to round that number down. That would at least work for that digit…I’d have to put more thought into getting the other two.
EDIT: Looking at the code now, I wonder if that would work at all. I guess if it’s an integer, it should work…if you divide 99 by 100, you get 0.99, but would an int read that number as 0? I should try it myself and check, but then the point is moot if you just round that number down.
conveniently ints already round down! The only slightly ugly thing about that(it works perfectly fine for the question) is that it’s not very general. You could fairly easily make a program that works for ANY number no matter how many digits(so long as we have words for them I guess). Of course the QUESTION is 1-999 so it’s perfectly valid to do a non general solution(and in fact will be more optimal, though less expandable).
Also instead of dividing the number by 100 twice I’d just do it once and check that number when creating the screen to get one less divide.
lol before you go using those numbers as an index in arrays, first things first:
if ((number >= 0) && (number < 1000))
{
int hundredsDigit = number / 100;
int tensDigit = (number % 100) / 10;
int oneDigit = number % 10;
//do other logic
}
else
{
//populate strEnglish with nasty error message and exit
}
Here’s the 0-99 version I was talking about. If it looks bad, sorry, I was typing this while watching TV a few nights ago. =P
#include <stdio.h>
int main()
{
int n, ones, tens;
printf("Enter an integer between 0 and 99(0 to exit): ");
scanf("%d",&n);
while(n!=0)
{
while(n<10||n>99)
{
printf("Please enter an integer between 0 and 99
");
printf("Enter a two-digit number: ");
scanf("%d",&n);
}
ones = n%10;
tens=n/10;
switch(tens)
{
case 1: switch(ones)
{
case 9:printf("nineteen");
break;
case 8: printf("eighteen");
break;
case 7: printf("seventeen");
break;
case 6: printf("sixteen");
break;
case 5: printf("fifteen");
break;
case 4: printf("fourteen");
break;
case 3: printf("thirteen");
break;
case 2: printf("twelve");
break;
case 1: printf("eleven");
break;
case 0: printf("ten");
break;
}
break;
case 2: printf("twenty ");
break;
case 3: printf("thirty ");
break;
case 4: printf("forty ");
break;
case 5: printf("fifty ");
break;
case 6: printf("sixty ");
break;
case 7: printf("seventy ");
break;
case 8: printf("eighty ");
break;
case 9: printf("ninty ");
break;
}
if(n>20)
{
switch(ones)
{
case 9: printf("nine");
break;
case 8: printf("eight");
break;
case 7: printf("seven");
break;
case 6: printf("six");
break;
case 5: printf("five");
break;
case 4: printf("four");
break;
case 3: printf("three");
break;
case 2: printf("two");
break;
case 1: printf("one");
break;
}
}
printf("
");
printf("Enter an integer between 0 and 99 (0 to exit): ");
scanf("%d",&n);
}
getch();
return 0;
}
Anyway trying to figure out why it gives me the undefined first referenced error.
Too accustomed to C like languanges and things like regular expressions. … Spent all of yesterday writing a simple Text parser in VBA. Mind you I know nothing of vba, but still worst programming experience ever.
I have never missed the array accessor [ ] so much in my life.
GNU Octave http://www.gnu.org/software/octave/ is a good alternative to Matlab. Its close enough to Matlab were one can port most code over easily/write code that will run in both. Stanford uses it for their numerically heavy courses. Speaking of Stanford, this year they offered free online classes in the topics of machine learning, databases, and AI. From what I heard, the machine learning class was great, the database class was good, and the AI class could have been better. There has been a huge positive response to these classes: practical assignments and if you will it, they will grade your programming assignments, having real deadlines seems to force people to keep with them, a forum for communicating with others and videos are 7-12 minute digestible segments. They have opened the flood gates and are now offering a ton of free online classes. http://jan2012.ml-class.org/
The other courses are listed at the bottom include cryptography, cyber-security, natural language processing and a host of other things. Also shout-out to getting started on TI-BASIC
You should try and use the arrays of digit names, your teacher gave 'em to you for a reason(you can eliminate those huge switch statements).
edit: additionally for your error I’d put an = 0 after your ints one at a time till it fixes the error to figure out which it is. I’m guessing it’s the N because you don’t explicitly set it ever, you just let scanf set it(I’m always wary of functions setting stuff for me).
edit again: also since you’re teacher gave you zero in the array of number names they probably want 0 to be a valid input not an exit code.
I think I got it working now based on what you said on the first edit, just let me clean it a bit and I’ll post it here after I take my Astronomy quiz… and a nap lol.
#include<stdio.h>
#include<math.h>
main()
{
int ones, tens, hundreds = 0;
int number;
while(1)
{
printf("Enter an integer between 0-999 (enter -1 to exit): ");
scanf("%d", &number);
if (number==-1) break;
hundreds = number / 100;
tens = (number - (hundreds * 100)) / 10;
ones = number - (hundreds * 100) - (tens * 10);
printf("This number in English is ");
if (number==0)
{
printf("zero");
}
if (hundreds > 0)
{
if (hundreds == 9) printf("nine");
else if (hundreds == 8) printf("eight");
else if (hundreds == 7) printf("seven");
else if (hundreds == 6) printf("six");
else if (hundreds == 5) printf("five");
else if (hundreds == 4) printf("four");
else if (hundreds == 3) printf("three");
else if (hundreds == 2) printf("two");
else printf("one");
printf(" hundred ");
if (tens == 0 && ones == 0) printf("
");
}
if (tens != 1)
{
if (tens == 9) printf("ninety ");
else if (tens == 8) printf("eighty ");
else if (tens == 7) printf("seventy ");
else if (tens == 6) printf("sixty ");
else if (tens == 5) printf("fifty ");
else if (tens == 4) printf("forty ");
else if (tens == 3) printf("thirty ");
else if (tens == 2) printf("twenty ");
if (ones > 0)
{
if (ones == 9) printf("nine
");
else if (ones == 8) printf("eight
");
else if (ones == 7) printf("seven
");
else if (ones == 6) printf("six
");
else if (ones == 5) printf("five
");
else if (ones == 4) printf("four
");
else if (ones == 3) printf("three
");
else if (ones == 2) printf("two
");
else printf("one
");
}
else
{
printf("
");
}
}
else
{
if (ones > 0)
{
if (ones == 9) printf("nineteen
");
else if (ones == 8) printf("eightteen
");
else if (ones == 7) printf("seventeen
");
else if (ones == 6) printf("sixteen
");
else if (ones == 5) printf("fifteen
");
else if (ones == 4) printf("fourteen
");
else if (ones == 3) printf("thirten
");
else if (ones == 2) printf("twelve
");
else printf("eleven
");
}
else
{
printf("ten
");
}
}
}
return 0;
}
I am worried that I didn’t even use void numToEnglish(unsigned int num, char english[]);, that I didn’t follow my professor’s hints and that I’m basically hardcoding. :oops: