Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Mark Nelson

Dr. Dobb's Bloggers

My Big Company Code Interview

April 09, 2014

It's always exciting to get an email like this from an iconic tech giant:

Hi Mark,

I recently found your profile in our database, and your background is impressive. The (redacted big company) Media Division will be flying several candidates in for interviews at our (redacted big city) headquarters in April and considering you.

I'm not actively seeking a new job, so normally I'd just file this away in my emergency just-laid-off folder, but this email had a twist that got my attention:

If interested in exploring Development opportunities with us, the first step will be to complete our coding challenge ideally within the next 3 to 5 days.

Who doesn't love a coding challenge?

The Code Interview

It's been years since I gave up on chat interviews. It just seems like these don't work so well — we all feel like we are insightful judges of candidate quality, but somehow the people we hire just don't always turn out as well as we hope.

So now when my name shows up on the list of people interviewing a candidate, I forgo the usual get-to-know-you experience in exchange for one or two simple programming exercises. Watching someone think on their feet under modest pressure seems more enlightening than anything I can get through conversation.

Apparently the big company contacting me has decided on a similar approach. The link included with the email sent me to a site called Interview Zen, which promised an interactive experience:

In this interview you will be shown a number of questions and asked to answer each one in turn. ... Your interviewer will be able to see your answer unfold as if they were sitting at the keyboard next to you.

This sounded pretty cool, but the actual implementation fell quite short of what I had hoped to see.

First, the promise of seeing me solve the problem interactively was just not met. I was asked a single, fairly complex question by Interview Zen, and given a choice from a dropdown of maybe 25 different languages to use for my solution. Interview Zen doesn't provide any sort of IDE; it specifically asked me to develop my answer elsewhere and post it when complete.

After posting my solution to the site, it was reformatted using some sort of pretty printer, and then...nothing. No message indicating I was done, no next step, just a page with my solution. So much for interaction.

And any interviewer watching the process would see my screen sit there completely blank for almost an hour, then a deus ex machina as somewhere around a hundred lines appear on the screen.

I expect that if you want to use Interview Zen properly, you need to create a staged series of questions so you can actually see this happen, and my big company didn't bother to do this.

But still, they get what they are looking for, which is a completed code sample of a reasonable size — big enough to actually demonstrate some work, small enough to evaluate quickly.

On To the Challenge

What's more interesting is the challenge itself. I'll lay out the assignment for you, show you my solution, and then you can decide whether I am worth hiring based on my work.

The challenge question is to write a function that produces a list of products based on what your friends are buying. I am given two functions:

  • getFriendsListForUser(): This function returns a list of users who are friends of a given user.

  • getPurchasesForUser(): This function returns a list of products that have been purchased by a given user.

The function I am asked to write will create a list of all the products purchased by a given user's friends, ranked by frequency — the most frequently purchased products should appear first. The list should exclude any products that the user has already purchased.

I'm going to present the code I created step by step, excluding comments and verbiage that belong in the finished file. You can see the whole thing by downloading getRecommendations.cpp.

The Interface

To kick things off I need to define the types I'm using, the functions provided in the problem definition, and the function I'm writing.

No big surprises here. About the only thing of interest to note is that I'm using a typedef for the customer and product IDs. It would be easy enough to just leave these as type std::string, but adding a little type safety is a nicety — and it actually surfaced a mistake made during development.

typedef std::string CustomerId;
typedef std::string ProductId;

std::vector<CustomerId> getFriendListForUser(const CustomerId &user );
std::vector<ProductId> getPurchasesForUser(const CustomerId &user );

std::vector<ProductId> getRecommendations(const CustomerId &user)

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.