152

Background

I was just asked in a tech interview to write an algorithm to traverse an "object" (notice the quotes) where A is equal to B and B is equal to C and A is equal to C.

That's it. That is all the information I was given.

I asked the interviewer what the goal was but apparently there wasn't one, just "traverse" the "object".

I don't know about anyone else, but this seems like a silly question to me. I asked again, "am I searching for a value?". Nope. Just "traverse" it.

Why would I ever want to endlessly loop through this "object"?? To melt my processor maybe??

The answer according to the interviewer was that I should have written a recursive function.

OK, so why not simply ask me to write a recursive function? And who would write a recursive function that never ends?

My question:

Is this a valid question to the rest of you and, if so, can you provide a hint as to what I might be missing? Perhaps I am thinking too hard about solving real world problems. I have been successfully coding for a long time but this tech interview process makes me feel like I don't know anything.

Matt Cashatt
  • 3,325

13 Answers13

306

It's a baffling, invalid interview question. The interviewer couldn't clearly articulate what it was that he/she was looking for and expected you to read his/her mind instead of responding meaningfully to your appropriate attempts to clarify the statement of the problem. Consider yourself lucky you didn't get the job.

The meaning of the verb "traverse" operating on a generic "object" is ambiguous, in my opinion. Start substituting a variety of different nouns for the word object and it quickly becomes obvious that traversal of an object is only meaningful for a small subset of the universe of things that are objects.

It makes sense to "traverse" the nodes of a "binary tree". It doesn't make sense to "traverse" a "clown". Yet, an object can just as easily represent a "clown" as it can represent a "binary tree".

gnat
  • 20,543
  • 29
  • 115
  • 306
Matt
  • 2,222
39

I can see three possibilities here.

  1. She was completely incompetent. Not much more to say on that one.
  2. She was deliberately making it ambiguous, to see how well you'd do at asking questions to figure out what you were supposed to do, and what she was really after.
  3. For whatever reason, she'd decided she didn't want you hired, so she asked a question that was unanswerable as given. When she was asked about your skills, she'd skip over that part and say something like: "I asked him about how to traverse a three-node graph, and he was completely stumped -- didn't have even a clue of how to start. Obviously he's grossly incompetent! We shouldn't even consider hiring him."
Jerry Coffin
  • 44,795
31

This is just a wild guess, but assuming the interviewer is talking about pointer references (and it's a trick question), the answer is: there's nothing to traverse, because all of the references point to the same object.

A recursive function? That's for traversing a tree. I see nothing in the original question that would imply that he's talking about a tree.

Robert Harvey
  • 200,592
14

While I cannot speak for this specific interviewer, I've seen similar questions in a front-end developer position interview, so the language I'll use in this example will be JavaScript.

Given:

var A = {
    key1: 'value1',
    key2: 2,
    key3: {
        innerkey1: 'value3'
    }
}

A typical wrong response may "traverse" the first level only and print/compare:

'value1'
2
[Object object]

So while coding a recursive example that would traverse all levels, I would mention things like:

  • Circular reference handling
  • How to handle arrays (should they be recursively traversed too?)
  • Should functions be evaluated and their return value processed?
  • For JavaScript: should the prototype match and should inherited properties also be compared?

So the "solution" that I'm guessing the interviewer was going for was to get a conversation started on a seemingly-simple question that has many advanced topics - recursiveness, pointers/references, expectations, etc.

WSkid
  • 240
  • 1
  • 5
9

Some interviewers specifically try to ask questions to see if the candidate is smart and honest enough to give one of these two answers:

I don't know.

or maybe:

I can't answer that as stated.

They don't want a candidate who will accept pure BS as a spec, and waste their employer's time and pay trying to implement it.

hotpaw2
  • 7,988
7

It seems to me that this is a (poorly articulated) question regarding a circular linked list. I would have likely asked if that is what was meant (because the answer would be certainly different than another above which is to say they are all references to the same object).

If this was a linked list question, then you (in this case) have a singly linked list, where the end node points to the other end (although if it was worded as you say - then it may be doubly linked if A points to B and C - but clarification on the interviewer's part would help this).

A -> B -> C -> A

Also (and this happens all the time), the interviewer may have read this question, thought it was a 'good' question, but didn't actually know the answer themselves (or even what it meant).

Maich
  • 99
5

Part of the challenge here is to get more detail by asking specific questions to get out that there is a tree structure and what are the components involved in doing a traversal. There may have been the assumption that there aren't many other data structures that one traverses besides trees but that is a bit of a leap to my mind.

JB King
  • 16,775
3

They may want to try to figure out how you deal with strange problems. But in this case, it has nothing to do with a "tech interview". It looks more like a psychological interview.

BenjaminB
  • 1,706
3

Write an algorithm to traverse an "object" (notice the quotes) where A is equal to B and B is equal to C and A is equal to C.

It seems like most people assume that A, B, and C are pointers, but they could just as easily be clowns, too. (Or members of the clown class.) Or they could be clown names. (Or class names. Or subclasses of the clown class.)

I would have turned the tables and asked if this is how they typically prepare development specifications, and then tell them how I could help them with the requirements specification stage of development. Poor communication of expectations leads to poor work product. Either they would get it or they wouldn't, If they didn't get it, I would walk away.

Jim
  • 161
2

While the question was poorly worded and the interviewer was clearly unhelpful in providing any direction, I have a slightly different take on what was being asked.

I think interviewer was looking for a solution that traversed the object structure using some type of reflection. The information that the three objects were equal should have prompted a conversation of object identity comparison (A == B means the objects are really the same object in memory), or object equality comparison (A == B means the values of the objects are the same).

The fact that the interviewer said that the answer was a "recursive" function, probably indicated that a discussion of deep versus shallow copying and comparison was expected.

Lucas
  • 121
2

Coming very late to this party, but I think the interviewer incorrectly asked this question:

Write an algorithm to traverse an array and determine that A is equal to B and B is equal to C and A is equal to C, in that order.

Then the correct answer would be a recursive algorithm.

pgthew
  • 31
1

I was just asked in a tech interview to write an algorithm to traverse an "object" (notice the quotes) where A is equal to B and B is equal to C and A is equal to C.

The object in question is made up of the parts A,B and C, and it forms a triangle. The person is simply asking if the object (a collection) contains all equal parts.

The interviewer wants to know if presented with parts A, B and C can you tell if they are all equal without getting stuck in an infinite loop. This question is stupidly simply to understand and still they managed to f*** it up in asking it.

They are all equal when A == B && B == C && A == C, but that can be simplified to just A == B && A == C.

The simplicity of the question resulted in confusion, and it really is worded badly.

The correct wording should have been.

Write an algorithm to check the parts of a collection to see if they are all equal to each other. Care must be given not to get stuck in an infinite loop. For example; if parts A is equal to B and B is equal to C and A is equal to C could cause problems.


The answer according to the interviewer was that I should have written a recursive function.

Yes, you can answer the question are all my parts equal using recursive functions. No, this is not an efficient solution.

EDIT: After some thought. No it's not possible to check a collection contains all equal parts using a recursive function.

The most efficient solution is as follows.

function are_all_equal(parts)
{
   for(int i=1; i < parts.length; i++)
       if parts[i] is not same as parts[0]:
           return false;
   return true;
}

print are_all_equal(parts) ? "yes" : "no";

This problem does occur in programming, and asking someone to write an algorithm to test a collection is perfectly normal. Depending upon the programming language this problem can often be solved with just one line of code.

Wording as they did, and expecting the wrong answer is not normal. Since this question was ask a year ago. I truly hope you ended up working somewhere else. I'd be interested in hearing from the original post how things turn out for him/her.

Reactgular
  • 13,120
  • 4
  • 50
  • 81
0

Was this a Java interview question, if so, may be he wanted to test your skills with overriding "hashcode" and "equals".

You would have to override these two methods and use the overridden equals method to stop the recursion when you compare A with A.

Without overriding, your comparison for "object" A to B, A to C and A to A will all result true but after overriding, only when object A compared to object A will return true where as other comparisons will return false.

rpatali
  • 21