# Tough Interview Questions

**URL:** <https://boards.straightdope.com/t/tough-interview-questions/267937>\
**Category:** In My Humble Opinion\
**Created:** [October 6, 2004, 10:38pm UTC](https://boards.straightdope.com/t/tough-interview-questions/267937 "2004-10-06T22:38:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanBlather](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@DanBlather](https://boards.straightdope.com/u/DanBlather)\
**Post date:** [October 6, 2004, 10:38pm UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/1 "2004-10-06T22:38:43Z")

</div>

[This](http://boards.straightdope.com/sdmb/showthread.php?t=278004) thread got me worrying. I have an interview coming up with a software company notorious for tough interviews with questions such as the linked thread.

What kinds of tough thought problems have been thrown at you?

---

<div class="post-metadata">

**Author:** ![BurnMeUp](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@BurnMeUp](https://boards.straightdope.com/u/BurnMeUp)\
**Post date:** [October 6, 2004, 10:47pm UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/2 "2004-10-06T22:47:52Z")

</div>

well if it is the same company, as i said in that thread, a lot of groups really downplay the “wacky” questions now.

---

<div class="post-metadata">

**Author:** ![Punoqllads](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Punoqllads](https://boards.straightdope.com/u/Punoqllads)\
**Post date:** [October 6, 2004, 11:02pm UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/3 "2004-10-06T23:02:57Z")

</div>

You have two files:

array.c:

```auto

char hello[] = "Hello, world!
";

```

ptr.c:

```auto

#include <stdio.h>
extern char *hello;

main()
{
  printf(hello);
}

```

You try to compile and run them. Explain what happens and why.

---

<div class="post-metadata">

**Author:** ![DanBlather](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@DanBlather](https://boards.straightdope.com/u/DanBlather)\
**Post date:** [October 6, 2004, 11:44pm UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/4 "2004-10-06T23:44:44Z")

</div>

I know there must be a trick, just can’t figure out what it is.

---

<div class="post-metadata">

**Author:** ![Punoqllads](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Punoqllads](https://boards.straightdope.com/u/Punoqllads)\
**Post date:** [October 7, 2004, 12:04am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/5 "2004-10-07T00:04:29Z")

</div>

The answer to the first part is that each file will compile just fine, and the linker will create an executable just fine. But the resultant executable will generate indeterminate results, probably a core dump.

As for why, it’s because in C and C++, pointers and arrays linguistically look very similar, but internally they’re quite different animals. At the memory location where a given array exists, there will be the explicit array. At the memory location where a given pointer exists, there will be an address.

Here’s another one: you have two rectangles, a and b. Each has an x,y origin coordinate, a length l (in the x direction), and a height h (in the y direction). L and h for each of them are guaranteed to not be negative. Write some pseudocode to detect if the two rectangles intersect.

---

<div class="post-metadata">

**Author:** ![Joe\_Random](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@Joe\_Random](https://boards.straightdope.com/u/Joe_Random)\
**Post date:** [October 7, 2004, 1:26am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/6 "2004-10-07T01:26:20Z")

</div>

> [@Punoqllads](#):
>
> Here’s another one: you have two rectangles, a and b. Each has an x,y origin coordinate, a length l (in the x direction), and a height h (in the y direction). L and h for each of them are guaranteed to not be negative. Write some pseudocode to detect if the two rectangles intersect.

You know, at first that question looked easy. Then I though about it and it seemed pretty difficult. Then I though about it some more and decided that it’s not difficult at all - just tedious.

Without actually writing any pseudocode, you can tell if two rectangles intersect if they meet one of two criteria. One: one of the corners of either rectangle lies inside (or on the boundary of) the other rectangle. That’s pretty easy to figure out. The only other case is when the rectangles form a “plus-sign” configuration, where every edge overlaps two other edges, but none of the corners are within the other rectangle.

None of that would be difficult to code, but I can’t think of an easy way to make it look nice. It’s going to be some messy code.

---

<div class="post-metadata">

**Author:** ![Punoqllads](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Punoqllads](https://boards.straightdope.com/u/Punoqllads)\
**Post date:** [October 7, 2004, 1:34am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/7 "2004-10-07T01:34:55Z")

</div>

Let me be a little more specific. Use no more than 4 comparisons.

---

<div class="post-metadata">

**Author:** ![EllisDee](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ellisdee/32/4531_2.png) [@EllisDee](https://boards.straightdope.com/u/EllisDee)\
**Post date:** [October 7, 2004, 1:43am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/8 "2004-10-07T01:43:38Z")

</div>

> [@Punoqllads](#):
>
> Here’s another one: you have two rectangles, a and b. Each has an x,y origin coordinate, a length l (in the x direction), and a height h (in the y direction). L and h for each of them are guaranteed to not be negative. Write some pseudocode to detect if the two rectangles intersect.

I’ll take a stab at it out of boredom.

```auto

Function Intersect(ax, ay, al, ah, bx, by, bl, bh) As Boolean
  Intersect = Not ((ax+al < bx Or ax > bx+bl) And (ay+ah < by Or ay > by+bh))
End function

```

---

<div class="post-metadata">

**Author:** ![John\_Kentzel-Griffin](https://avatars.discourse-cdn.com/v4/letter/j/ccd318/32.png) [@John\_Kentzel-Griffin](https://boards.straightdope.com/u/John_Kentzel-Griffin)\
**Post date:** [October 7, 2004, 1:45am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/9 "2004-10-07T01:45:06Z")

</div>

General Questions is for questions with factual answers. IMHO is for polls.

Off to IMHO.

**DrMatrix** - GQ Moderator

---

<div class="post-metadata">

**Author:** ![bitwise](https://avatars.discourse-cdn.com/v4/letter/b/ea5d25/32.png) [@bitwise](https://boards.straightdope.com/u/bitwise)\
**Post date:** [October 7, 2004, 1:48am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/10 "2004-10-07T01:48:51Z")

</div>

I could be wrong, I believe

(A x B)\intersect (C x D) = (A\intersect C) x (B\intersect D)

So you can just check for the intersection of the intervals that are the projections of the rectangles onto the x-axis and y-axis. I believe this can be done with two comparisons each. For [a,b] and [c,d], you need to compare a with c and b with d.

---

<div class="post-metadata">

**Author:** ![bitwise](https://avatars.discourse-cdn.com/v4/letter/b/ea5d25/32.png) [@bitwise](https://boards.straightdope.com/u/bitwise)\
**Post date:** [October 7, 2004, 1:57am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/11 "2004-10-07T01:57:43Z")

</div>

> [@bitwise](#):
>
> I could be wrong, I believe
> 
> (A x B)\intersect (C x D) = (A\intersect C) x (B\intersect D)
> 
> So you can just check for the intersection of the intervals that are the projections of the rectangles onto the x-axis and y-axis. I believe this can be done with two comparisons each. For [a,b] and [c,d], you need to compare a with c and b with d.

Oops, that should be, b with c and a with d.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [October 7, 2004, 2:00am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/12 "2004-10-07T02:00:07Z")

</div>

**bitwise** has the right answer.

Here’s one I like.

```auto

class D
{
    public:
        D() { n = 0; };
        ~D() {};

       int foo() { return 0; };
       int bar() { return n; };

    private:
        int n;
};

int main()
{
    D* badPointer = 0;

    if ( badPointer )
        badPointer->foo();
    else
        badPointer->bar();

    return 0;
}

```

What happens when that code is compiled? What about when it’s executed? And most importantly, why?

---

<div class="post-metadata">

**Author:** ![Punoqllads](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Punoqllads](https://boards.straightdope.com/u/Punoqllads)\
**Post date:** [October 7, 2004, 2:18am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/13 "2004-10-07T02:18:58Z")

</div>

Compiled? I think it will compile fine.  
Running? A segmentation fault when you try to dereference this-\>n.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [October 7, 2004, 2:21am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/14 "2004-10-07T02:21:39Z")

</div>

> [@Punoqllads](#):
>
> Compiled? I think it will compile fine.  
> Running? A segmentation fault when you try to dereference this-\>n.

What line does it choke on, and why?

You might want to compile and run this to be sure.

---

<div class="post-metadata">

**Author:** ![EllisDee](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ellisdee/32/4531_2.png) [@EllisDee](https://boards.straightdope.com/u/EllisDee)\
**Post date:** [October 7, 2004, 2:23am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/15 "2004-10-07T02:23:57Z")

</div>

> [@ultrafilter](#):
>
> **bitwise** has the right answer.

Really? Reading the requirements, I think he was looking for my answer. (“No more than 4 comparisons”…)

But I agree with **Joe Random** that it is more tedious than complex.

---

<div class="post-metadata">

**Author:** ![Punoqllads](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Punoqllads](https://boards.straightdope.com/u/Punoqllads)\
**Post date:** [October 7, 2004, 2:33am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/16 "2004-10-07T02:33:40Z")

</div>

It chokes in bar(). It tries to put this-\>n on the top of the stack. When it tries to do so, it’ll get a segmentation fault.

As far as the rectangle question, there were quite a few people who would try do do point-in-rectangle tests and line-cross-rectangle tests that went out so far they’d hit the back of your head. You both got the answer right, though in different forms.

---

<div class="post-metadata">

**Author:** ![Punoqllads](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Punoqllads](https://boards.straightdope.com/u/Punoqllads)\
**Post date:** [October 7, 2004, 2:35am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/17 "2004-10-07T02:35:58Z")

</div>

Of course, if you have the compiler optimize the code, it might be smart enough to inline bar(), at which point it would realize the return value is being ignored, and would then just do nothing. Then it wouldn’t crash at all.

---

<div class="post-metadata">

**Author:** ![bitwise](https://avatars.discourse-cdn.com/v4/letter/b/ea5d25/32.png) [@bitwise](https://boards.straightdope.com/u/bitwise)\
**Post date:** [October 7, 2004, 2:44am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/18 "2004-10-07T02:44:52Z")

</div>

> [@Ellis Dee](#):
>
> Really? Reading the requirements, I think he was looking for my answer. (“No more than 4 comparisons”…)
> 
> But I agree with **Joe Random** that it is more tedious than complex.

I was trying to show how one would approach the problem. I put down the first thing that came into my head. “A rectangle is a Cartesian product of intervals. Can I solve the problem with intervals first, and then work my way back up?” etc.

---

<div class="post-metadata">

**Author:** ![EllisDee](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ellisdee/32/4531_2.png) [@EllisDee](https://boards.straightdope.com/u/EllisDee)\
**Post date:** [October 7, 2004, 3:04am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/19 "2004-10-07T03:04:34Z")

</div>

> [@bitwise](#):
>
> I was trying to show how one would approach the problem. I put down the first thing that came into my head. “A rectangle is a Cartesian product of intervals. Can I solve the problem with intervals first, and then work my way back up?” etc.

Shhh, I’m trying desperately to avoid admitting that I don’t even understand your solution.

Ah, crap, I went and did it. In the interest of fighting my ignorance, could you elaborate your answer?

For example, my thinking was “no edges can overlap”, and thus my answer.

> [@](#):
>
> (A x B)\intersect (C x D) = (A\intersect C) x (B\intersect D)

My questions:

“” is integer division?

What are you referring to when you say “intersect”?

---

<div class="post-metadata">

**Author:** ![bitwise](https://avatars.discourse-cdn.com/v4/letter/b/ea5d25/32.png) [@bitwise](https://boards.straightdope.com/u/bitwise)\
**Post date:** [October 7, 2004, 3:20am UTC](https://boards.straightdope.com/t/tough-interview-questions/267937/20 "2004-10-07T03:20:17Z")

</div>

> [@Ellis Dee](#):
>
> Shhh, I’m trying desperately to avoid admitting that I don’t even understand your solution.
> 
> Ah, crap, I went and did it. In the interest of fighting my ignorance, could you elaborate your answer?
> 
> For example, my thinking was “no edges can overlap”, and thus my answer.My questions:
> 
> “” is integer division?
> 
> What are you referring to when you say “intersect”?

Sorry, it was sort of a LaTeX-ish notation without resorting to full LaTeX; perhaps, I should have gone with full LaTeX to be less confusing. That would be

(A imes B)\cap (C imes D) = (A\cap C) imes (B\cap D)

Here imes denotes Cartesian product and \cap denotes intersection; in my original post, \intersect denotes intersection and x denotes Cartesian product.

[Next page](https://boards.straightdope.com/t/tough-interview-questions/267937.md?page=2)
