View Full Version: C++ Class

C++ Learning Community > C++ Tips > C++ Class


Title: C++ Class
Description: How to design classes


myork - August 12, 2005 02:31 PM (GMT)
It's been very quite recently and the only questions we seem to get are C questions. Also most people believe they are witting C++ though if you look at their code they are really writing C and using classes (There is a distinction and it will hit you hard when you try and get a job).

Talking of jobs I just heard a great interview question that I am going to start using.
Q: Can you calculate the number of dentists in Los Angeles (substitute any local major city).


So back to reality, to relieve me from the boredom of work, lets have a look at safe class design and try and work on good things to do (I am playing it by ear so no flames and lets see what happens)

Part 1
Requirements: Lets design a safe C++ mutex.
Linux user see pthread_mutex for details on the C API.
Windows users see: Windows mutex for details on the C API.

Anybody that want to participate mail me your design.
Then next week I will go over the designs (anonymously) and we will see if they can be improved. If nobody sends me anything then I will present a design and we can discuss that.


PS. No cheating by looking at the Windows CMutex class.

KTC - August 12, 2005 04:18 PM (GMT)
Ah, the joy between the difference of Object Based and Object Oriented design.

QUOTE
Talking of jobs I just heard a great interview question that I am going to start using.
Q: Can you calculate the number of dentists in Los Angeles (substitute any local major city).
Hehe, those type of questions are great if you know why it's being asked and what the interviewer are actually looking for. I've just done a quick guesstimate, and I'll say about 1,000 dentists in LA. B) :cheers: If people haven't heard of this types of questions from physics / maths / IT / inteviews, I'll come back in a few days with how I arrived with the guesstimate.

myork - August 12, 2005 04:26 PM (GMT)
QUOTE (KTC @ Aug 12 2005, 11:18 AM)
QUOTE
Talking of jobs I just heard a great interview question that I am going to start using.
Q: Can you calculate the number of dentists in Los Angeles (substitute any local major city).
Hehe, those type of questions are great if you know why it's being asked and what the interviewer are actually looking for. I've just done a quick guesstimate, and I'll say about 1,000 dentists in LA. B) :cheers: If people haven't heard of this types of questions from physics / maths / IT / inteviews, I'll come back in a few days with how I arrived with the guesstimate.

I came up with a minimum of 3,500.
I'll share my thinking after KTC

dr voodoo - August 12, 2005 05:00 PM (GMT)
QUOTE
PS. No cheating by looking at the Windows CMutex class.

What's with wxMutex or boost::mutex? Oh well I guess having locked at them some months ago discalifies me.

QUOTE
Talking of jobs I just heard a great interview question that I am going to start using.
Q: Can you calculate the number of dentists in Los Angeles (substitute any local major city).

I would estimate something like 1680 but to be honest I do not see what you want to acheive with such a question. :blink:

myork - August 12, 2005 05:10 PM (GMT)
QUOTE (dr voodoo @ Aug 12 2005, 12:00 PM)
I would estimate something like 1680 but to be honest I do not see what you want to acheive with such a question. :blink:


I want to know how you got 1680.
Guessing is not going to go down well in an interview unless you can explain why.

dr voodoo - August 12, 2005 05:25 PM (GMT)
Well with guessing I mean figuring out a value which is more or less realistic but that is everything else then certain.

Well I proceeded as follwing:
A dentist works about 330 days a year and 5 days a week. During one day he can help 10 patients. This means one dentist can help 330/7*5*10 ~= 2300 patients. Each patient comes once per year to a dentist. In LA there are 3781500 people. Meaning for each 2300 people there needs to be one dentist. That means 3781500/2300 ~= 1680.

Perhaps you class that procedure under calculation and I don't.

KTC - August 13, 2005 02:56 PM (GMT)
Okay, seeing dr voodoo has started a post on how he got his answer, I might as well do mine now.

QUOTE
I would estimate something like 1680 but to be honest I do not see what you want to acheive with such a question. :blink:
There's various reason why such a question is asked. Some of which are, to see whether you can think logically, whether you can justify how you got the answer, whether you can make reasonable assumption (and justify if necessary) and ignore details that are not important in coming up with an estimate. Basically, the end answer whether it's 1000, or 3000 say, is not as important as how you gotten to the answer. In programming, such guesstimate ability might be useful when estimating how long it'll take to complete a project.

QUOTE
Well I proceeded as follwing:
A dentist works about 330 days a year and 5 days a week. During one day he can help 10 patients. This means one dentist can help 330/7*5*10 ~= 2300 patients. Each patient comes once per year to a dentist. In LA there are 3781500 people. Meaning for each 2300 people there needs to be one dentist. That means 3781500/2300 ~= 1680.
Well, that is the way one work out the answer to such a question. Except, you're being way way too detail and specific. Remember, it's estimate, during interview where such a question might come up, you often don't have a calculator at hand. Rounding up or down to whole number to make calculation much easier in one's head is absolutly acceptable, and perhaps expected.

Now to be predantic, when one says someone works certain number of days a year, that means that that person is in office that many number of days. So if you say a dentist works 330 days a year, that means he's seeing patient for that many number of days. Of course you did then take into account during calculation of weekends, but just be careful the words you use if you're ever in an interview.

Now to my guesstimate. B)
There's between 3.7 to 4 millions people in LA (I had to look that one up :rolleyes: ) Say 4 millions for ease of calculation. Now, say a dentist work 9 - 5 which is not unreasonable. Give them an hour lunch each day, that's 7 hours per work day. They might have one month of holiday (vacation) each year, so that's about 48 weeks where they're working. (Note I'm ignoring unneeded details like public holiday and such.) Some case takes longer, some shorter, but I'll say half an hour per patient since a lot of times is routine check-up, so it wouldn't be unreasonable. Now, people go to dentist (if they don't have specific dental problems) once a year, but not everyone go. I'll give it a number, say 8 out of 10 people go to dentists.

So, 48 weeks of 5 days gives 240 days. 7 hours per day give a total of 1680 hours of seeing patients. Double that to 3360 since we're seeing 2 patients per hour. Now, am going to round that down to 3,000 for ease of calculation. This of course isn't entirely unjusitfy since it sometimes take longer than half an hour to see a patient, and some comes in more than once a year, so the total number of patient seen is less that number so round down instead of up. In a city of 4 millions, 8 out of 10 means 3.2 millions. Since the number of people seeing dentists or not is not precise, it wouldn't hurt to round it to just 3 millions, to make our number much easier to work with. 3 millions people, 3000 patients each, would finally gives the number of around 1000 dentists in the city.

Now one can try and be clever, and suggest in an interview, if one really want to know how many dentists there really are, one can look it up. Since I would think a profession such as dentists would require to be registered with the city / state. Of course, only make such a response / comment after answering the question in the way the interviewer wants you to. Don't just say why bother guessing, just look it up. ^_^

On to you myork ;)

myork - August 13, 2005 07:14 PM (GMT)

A work day is approx 8 hours.
Assuming you can check 1 patient every 30 minutes on average.
Thats 16 patients a day or 80 patients a week.

For some reason I though that you needed a check up every 6 months (my wife says once a year) but to stay with original estimate I will stick with 6 months.

So in 6 months thats 6 (months) * 4 (4 weeks a month) * 80 = 6 * 300 = 2000
Using 4 weeks a month is a good short cut that automatically gives people 4 weeks holiday a year. Thats somthing worth mentioning at the interview.

So each dentist can see 2000 people each 6 months.

I did not know the Population of LA. But London has a population of approx 4Mil I figured that LA was nearly double (Its very spread out in comparison to London) so I guessed at 7Mil. 7Mil /2000 = 3500 dentists needed by LA residents.

KTC - August 14, 2005 12:13 PM (GMT)
Oh, I love the answer my flatmate just gave when I asked him the question about the dentist.
QUOTE
Flatmate: I would say around 800

KTC: Okay, how did you get the answer?

Flatmate: Well, my dad had around 5,000 people on his computer a few years ago. So dividing 4 millions by 5,000 gives 800.
:lol:

I didn't know his dad was / is a dentist. :rolleyes:




Hosted for free by InvisionFree