Help!!!! Interview Question
Hi all :
This is an interview question that i was asked to solve. I actually had a block and I couldn't answer it and I left it blank. But ever since I wanted to answer it , but I couldn't ...any help would be appreciated:
Engineer Programming Challenge Problem:
Given a simple virtual machine that has:
1. A program memory where the machine language bytes are stored (a pointer to this buffer will be passed in to your RunProgram function). The interpreter should run the program starting with the instruction at offset 0.
2. A register file of 10 DWORD registers which all start out with a value of 0.
3. A memory of 1024 bytes in size, with all bytes starting out with a value of 0.
4. And the following instruction set:
MOV R#, value Moves the immediate value into the specified register
Encoding: 0x00, byte destregnum, dword value (little-endian form)
LOAD R#, R# Loads the little-endian DWORD from memory at the location specified by the value of the second register and places the value into the first register.
Encoding: 0x01, byte destreg, byte memindexreg
STORE R#, R# Stores the DWORD value in the second register into the memory at the location specified by the value in the first register.
Encoding: 0x02, byte memindexreg, byte srcreg
ADD R#, R# Adds the value in the second register to the value in the first register and stores the result in the first register.
Encoding: 0x03, byte destregnum, byte srcregnum
JE R#, R#,dwOff Compares the value in the first specified register to the value in the second specified register. If the first value is equal to the second value
then jump to the specified absolute offset and continue executing instructions. Otherwise
continue with the next instruction. The offset is absolute from the top of the program.
Encoding: 0x04, byte regnum, byte regnum, dword offset value in little-endian form
JMP dwOff Jump to the specified absolute offset and continue executing instructions. The offset is absolute from the top of the program.
Encoding: 0x05, dword offset value in little-endian form
IN R# Input a decimal # from the user and store into the specified register
Encoding: 0x06, byte regnum
OUT R# Prints out the value of the specified register in decimal form
Encoding: 0x07, byte regnum
HLT Terminates the current program.
Encoding: 0x08 (no other parameters)
Challenge part 1: In one hour, write a well-structured C++ class to interpret this machine language and run well-formed machine-language programs passed in to your class.
Challenge part 2: If you have time left over, write a function in this machine language to compute a factorial (if you really want to impress us, make your function
recursive). Show a declaration like the one below (“unsigned char abyProgram[] = …”) with your solution.
For example, the following program would write out the numbers between 0 and 9:
#include <stdio.h>
main()
{
unsigned char abyProgram[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // MOV R0, 0 (at offset 0)
0x00, 0x01, 0x01, 0x00, 0x00, 0x00, // MOV R1, 1 (at offset 6)
0x00, 0x02, 0x0a, 0x00, 0x00, 0x00, // MOV R2, 0xA (at offset 12)
0x07, 0x00, // OUT R0 (at offset 18)
0x03, 0x00, 0x01, // ADD R0, R1 (at offset 20)
0x04, 0x00, 0x02, 0x23, 0x00, 0x00, 0x00, // JE R0, R2, 35 (at offset 23)
0x05, 0x12, 0x00, 0x00, 0x00, // JMP 18 (at offset 30)
0x08 // HLT (at offset 35)
};
YourInterpreterClass var;
var.RunProgram(abyProgram);
}
Re: Help!!!! Interview Question
Wow, a real world question in an interview! :rolleyes:
Re: Help!!!! Interview Question
Sounds more like a test question to me. If I were asked this in an interview, I'd have gotten up, and thanked the interviewer for wasting my time. I passed 12 years of school, and 4 years of undergrad work already.
If this were a real world problem, the question should be, "Give me a timeline, with milestones, on when you can implement this."
Just my $0.02...
Viggy
Re: Help!!!! Interview Question
Quote:
Originally Posted by MrViggy
Sounds more like a test question to me. If I were asked this in an interview, I'd have gotten up, and thanked the interviewer for wasting my time. I passed 12 years of school, and 4 years of undergrad work already.
If this were a real world problem, the question should be, "Give me a timeline, with milestones, on when you can implement this."
Just my $0.02...
Viggy
Exactly my point. Didn't catch the sarcasm?
:p
Re: Help!!!! Interview Question
Looks like they want a guy with experience.
:ehh:They just arent saying that.
You could solve this with a dispatch table.
struct dtbl {
char cmd[8];
int (*fnptr) (unsigned char * data, double dparam);
} MYDISPATCH_TABLE;
QUASI CODE:
0 WHILE COMMANDS LEFT
1 PARSE OUT CURRENT COMMAND
2 LOOP ONCE THRU CMDS TIL COMMAND MATCHES REQ ONE.
3 EXECUTE FUNCTION IN FUNCTION POINTER
4 GO TO STEP 0
ahoodin
Re: Help!!!! Interview Question
Quote:
Originally Posted by ahoodin
Looks like they want a guy with experience.
:ehh:They just arent saying that.
ahoodin
Yes, but the problem is un-realistic. Has your boss come up to you with a problem along the lines of writing a machine language interpretter, and gave you only 1 hour to solve the whole thing? If so, then I'd suggest looking for a new job.
You can find out a lot more about a person when they are relaxed, not when they are rushing to solve a problem.
Viggy
Re: Help!!!! Interview Question
Quote:
Originally Posted by ahoodin
struct dtbl {
char cmd[8];
int (*fnptr) (unsigned char * data, double dparam);
} MYDISPATCH_TABLE;
QUASI CODE:
0 WHILE COMMANDS LEFT
1 PARSE OUT CURRENT COMMAND
2 LOOP ONCE THRU CMDS TIL COMMAND MATCHES REQ ONE.
3 EXECUTE FUNCTION IN FUNCTION POINTER
4 GO TO STEP 0
ahoodin
But, the original problem said:
Quote:
In one hour, write a well-structured C++ class to interpret this machine language and run well-formed machine-language programs passed in to your class.
Isn't a dispatch table more C then C++ like? I'm guessing that the interviewer wants to talk about polymorphism, but doesn't know how to ask an interview-ee the right questions.
Viggy
Re: Help!!!! Interview Question
Thanks guys, but that was a real interview question , i just left it blank , and I said that, i would solve it in my spare time, but really .....i am really getting frustrated becasue I can't solve it....
Re: Help!!!! Interview Question
If those are all the instructions supported I suppose I could write it in less than an hour... :D But I wouldn't probably make the best design and most likely it won't work from the first shot... ;)
PS: take it as a joke...
Re: Help!!!! Interview Question
Quote:
Originally Posted by MrViggy
But, the original problem said:
Isn't a dispatch table more C then C++ like? I'm guessing that the interviewer wants to talk about polymorphism, but doesn't know how to ask an interview-ee the right questions.
Viggy
Class, struct, very close. Just create an array of objects then.
Some people have said a class and a struct are the same thing.
I have provided a quick solution outline to a complex problem.
Throwing in inheritance would probably complicate matters to making the problem an all year affair. I dont see where the question specifically asks about inheritance. That is circumstantial gusswork. Don't read too much in.
ahoodin
Re: Help!!!! Interview Question
Thanks alhoodin and everyone for your helpand input...I really appreciate it
Re: Help!!!! Interview Question
Quote:
Originally Posted by ahoodin
Class, struct, very close. Just create an array of objects then.
Some people have said a class and a struct are the same thing.
I have provided a quick solution outline to a complex problem.
Throwing in inheritance would probably complicate matters to making the problem an all year affair. I dont see where the question specifically asks about inheritance. That is circumstantial gusswork. Don't read too much in.
ahoodin
Which helps to prove my point. This is a bad interview question, and not a real world problem at all. The "proper" solution depends on what the interpretation of "well-structured C++ class to interpret this machine language" is. In the real world, there would be a discussion on what is meant by well structured C++.
And then meetings would ensue about who/how/when to implement such class(es); deadlines would be placed on the project; etc., etc.
Man, I gotta get out of the office!
;)
Viggy
Re: Help!!!! Interview Question
Quote:
Originally Posted by ahoodin
That is circumstantial gusswork. Don't read too much in.
ahoodin
Just too add, what I think is right, what you think is right, and what the interviewer thinks is right are three different things. And, depending on the interviewer, if you and he/she do not share the same thoughts about how to solve a problem, you are automatically not fit for the position.
As I said, this is a bad interview question.
Viggy
Re: Help!!!! Interview Question
Thanks u guys...for a minute I thought that I was the problem...good to know that it's not a good interview question...
Re: Help!!!! Interview Question
Quote:
Originally Posted by MrViggy
As I said, this is a bad interview question.
I admire the practicallity of an employer who wants the best. The question is forward thinking...but actually incomplete. Additionally it is to comprehensive to be answered eloquently in a short period of time.
Many times in business that is the way it is.
The question is bad, reality is bad. The employer attempts to be progressive but probably unfair in the case of an entry level position.
The level of tenacity and technical aptitude demonstrated on this interview will most likely amount to frantic panic on an entry level applicant.
;)
ahoodin
PS If my posts helped, please dont forget to rate. Click the scale and then click on the approve radio button.