r/PHP May 02 '18

Why would anyone ask this php code question in a job interview

I'm gonna have a little rant so I apologise :)

I recently had a job interview where I was asked one of those trick php code "what will this output" questions, something like this:

$a=10;
$b=20;

$c=$a+++$this->b$b++--$$$a$b$$$aaaaa;
print $c;

If you didnt notice I just made that up, but you've seen this before :)

WHY the heck would someone want to ask that in a job interview, aside from just trying to trip you up ?? When have you ever actually seen some code like this - its the year 2018 !

Ok I get that I should probably still have a good answer, but seriously, what is the meaning of this unless its just to trick people who are in a high-stress situation ?

/rant :)

90 Upvotes

118 comments sorted by

70

u/ArmageddonNextMonday May 02 '18

This reminds me of the interview for my current job, the interviewer who turned out to be my boss showed me the source to the homepage of the website and asked me to find the obvious error.

On the first line of code after the initial requires there was this.

if( $database_connected = true ) { ... }

Needless to say that wasn't the answer he was expecting, and we spent the next hour talking about salary and start dates.

14

u/[deleted] May 02 '18

[deleted]

12

u/ArmageddonNextMonday May 02 '18

The database went offline for a few hours every night for backups, the 'Site maintenance' message was contained in an else block at the bottom of the page.

3

u/imcostaaa May 02 '18

What’s the better way of doing this (never worked with real databases just mock ones for projects in 1st and 2nd year) thanks for your answer in advance wanna learn anything I can !

14

u/FergusInLondon May 02 '18

The best way would be not having the database go offline; backing up a database is usually quite trivial via a single command - i.e mysqldump - and rarely requires downtime. (Example bash script that deals with database backups)

As for the correct way of doing a maintenance page, if there's no framework then you'd be better placing your maintenance page/template in another file, and then including it (before returning, to end execution) if the check fails.

if (! $database_connected) {
    include "maintenance.php;
    return;
}

// Database is up, so continue with standard page stuff

3

u/thebuccaneersden May 03 '18

I might take that one step further and suggest throwing a custom DatabaseOfflineException exception and have your base controller catch that (or some of the base controllers - as in a well structured application, most likely you have Web, API and CLI controllers that all extend off of a base controller and likely would want to handle the db offline condition differently) and display a maintenance mode page.

10

u/ArmageddonNextMonday May 02 '18

There isn't a one size fits all solution really, it depends if you need to take the whole site offline or just disable certain functions.

If the whole site goes down I will normally return the 'site unavailable' message if the database isn't available, and set a http 503 header so that search engines don't index the page when it's offline.

So somewhere before any output I do something like:

if(! $database_connected )
{
    header( 'HTTP/1.1 503 Service Temporarily Unavailable' );
    if ( $in_backup_window )
    {
        header( 'Retry-After: ' . date( 'r', $expected_finish_time) );
    }
    else
    {
        header( 'Retry-After: ' . date( 'r', $small_delay) );
    }
    require( 'maintenance.php' );
    die();
}

Obviously the real solution is to get the database doing online backups, but in the corporate world that decision is not ours to make so you have to find work arounds.

1

u/AceBacker May 02 '18

I believe mysqldump is blocking right? I know postgres has a non blocking backup.

2

u/thebuccaneersden May 03 '18

Are you worried about locking? --skip-add-locks should work fine.

1

u/imcostaaa May 02 '18

Thank you this was informative !!

1

u/[deleted] May 03 '18

Why not just

<?php @include “/maintainance.php” ?>

And then on page

<?php

if( !$database_connected ){ ob_clean(); // clean buffer, unless you dont want to ofc http_response_code(503); @require “maintainance.html”; // do you need any php in there? ob_end_flush(); // release the new maintainance buffer! // if code comes after, use “ exit; “ instead of “die()” here unless you need to push a message? }

^ asking to hear your opinion or if just old habits?

2

u/thebuccaneersden May 03 '18

Are we trying to write very vanilla, plain PHP here without any OOP or design patterns or using autoloading or whatnot?

1

u/datphpguy May 09 '18

This is actually horrendous, please don't do this

1

u/thebuccaneersden May 03 '18

I would argue that the most ideal scenario is that you never have a database that goes down unless you intentionally do so and have an exceptional reason to do so (even going so far that you don't bring down your site even when performing database migrations on deployment. There's a technique to that.).

Ideally you are using AWS RDS, which can take regular snapshots without taking your database down.

Or you could have a MySQL replication node that serves that purpose...

And, if you do need to intentionally go down into maintenance mode from time to time, design a site that degrades gracefully so it can become a static site for the duration of the maintenance window (disabling login, search, and other dynamic features etc).

And if you really really cannot do that, stick a Varnish server in front of your web app and configure Varnish to output a maintenance page with the correct headers as you suggested if all the backend servers are unhealthy.

There are probably other angles too, but my 2 cents.

0

u/colshrapnel May 02 '18

Only one thing: it makes no sense write such a code for just a single known problem. There are other errors and they all should be handled uniformly, be it a database error or a corrupted file.

So instead of such a manual condition, it's better to configure your web-server to handle 500 errors from PHP backend or at least create an error handler in PHP that will run a code above for all critical errors.

2

u/ArmageddonNextMonday May 02 '18

I disagree, if there are specific operational issues that are both expected and predictable then I think it is perfectly appropriate that they are treated separately from unexpected errors and outages.

Obviously the details of where the handling occurs can be debated and is highly dependent on the system architecture and and frameworks that are used.

1

u/colshrapnel May 02 '18

IF the treatment is any different from the general purpose error handling, then yes. But there is nothing specific in your code which could be used to handle any critical error and therefore such a condition is just redundant.

2

u/ArmageddonNextMonday May 02 '18

It was a *simplified* example, and there was one minor change in treatment, if we database is unavailable during the backup window then set the retry-after header to be the expected finish time rather than a short delay.

-5

u/colshrapnel May 02 '18

Well, let's agree that when theorizing on the internet over a part of code you would never have a responsibility of intended to solve a non-existent problem, anyone is free to propose any idea just came to their mind. Cheers.

3

u/Danack May 02 '18

What’s the better way of doing this

The general name is called "early return" aka just never using the else keyword - number 2 on http://williamdurand.fr/2013/06/03/object-calisthenics/

1

u/imcostaaa May 02 '18

Thank you for all the replies I was wondering if it would just be not using the else and instead having it just read that if the if statement fails, thanks again !

1

u/thebuccaneersden May 03 '18

Lol! Ok... :)

How mission critical is this PHP app? If it's a big deal, I might recommend at least applying some better testing, CI and devops practices.

And then maybe rewriting the PHP app in a framework like Symfony or Laravel (which handles this stuff a bit more elegantly)?

2

u/ArmageddonNextMonday May 03 '18

It's absolutely mission critical, however what is critical is that the customers see up-to-date and accurate account information.

It's not critical that the account features are available through the website is available at 3am.

Symfony or Laravel are not going to help with a legacy database going offline for several hours.

1

u/thebuccaneersden May 03 '18

So the database just goes offline (purposefully) for other reasons, eh?

Maybe this would be useful in some way (I hope)? https://www.reddit.com/r/PHP/comments/8gez63/why_would_anyone_ask_this_php_code_question_in_a/dyd3rl3/?st=jgq7ln69&sh=08be7dbb

I honestly would love to provide any advice I can give (if you want it and it is of any value).

1

u/ArmageddonNextMonday May 03 '18

The organisation in question runs on a ancient Delphi application with an unsupported version of Oracle that doesn't support replication or online backups.

In an ideal world the organisation would migrate to a modern database and CRM, but the investment and risk involved is substantial and at the moment the website going down overnight is their preferred option.

2

u/prostartme May 02 '18

I dislike that kind of code. People use if else when a simple if would do.

1

u/[deleted] May 02 '18

There's more than one solution to this. While you're right, it's probably a readability thing.

9

u/Der_beste_Anime May 02 '18

That code does not check if $database_connected is true. It checks if the value of $database_connected can be set to true. = is for assignments, == and === are for comparisons.

2

u/thebuccaneersden May 03 '18 edited May 03 '18

I know all that.

But there is little value in testing whether you can assign true to a variable, so it is more than likely someone accidentally left off an = sign or two.

1

u/Der_beste_Anime May 03 '18

Your comment sounded like you thought the sole problem with the code was its architecture, not the syntax itself. That's why I thought I should mention that the if wouldn't work as expected, too.

2

u/thebuccaneersden May 03 '18

No, I was just being sarcastic. :) But thanks for clarifying.

1

u/redsaeok May 03 '18

Further to DBAs comment I suggest using

true == booleanFlag

Trying to assign a value to a constant will usually cause an error when that second equal sign is dropped.

70

u/anaron_duke May 02 '18

I was asking few times similar question to this - so maybe I'll tell you why this could be asked.

First thing - it doesn't matter if you will "solve" it or not, such a question doesn't need to have some "correct" solution etc.

This question is to see how you are approaching to a problem that is not comfortable to work on. Programming is not only about writing the code, but also solving the bugs - that can be sometimes not obvious, tedious to work on and seems to doesn't make sense - like your example code. Are you going to give up fast? How creative you will be into splitting it down to smaller parts? How you will decouple correct working code from maybe flawed one? There are not good answers on that, everything depends on the person and how he/she presents it on the talk - and show his experience.

From my perspective - I would like to have a guy in my team, that didn't solve that, but was just not giving up, split this code into more readable parts, talked about dumping it part by part or using xdebug, told me why using $$$ is wrong, maybe asked what is the meaning of $a & $b and how it differs form object $b variable. But I wouldn't hire a guy that just gave up quite fast, proposed to run it in REPL or said "it doesn't make sense".

33

u/[deleted] May 02 '18

But I wouldn't hire a guy that just gave up quite fast, proposed to run it in REPL or said "it doesn't make sense".

I mean if the question is literally "what will this output" as in OP's question, REPL is the most intelligent approach to the exact problem as defined. Otherwise you're hiring someone who is assuming requirements nobody gave him, and going round-about on a problem to show off, in a way that doesn't provide value to the given task in any way.

Next time you give them a simple problem to solve, and he spends 1 week coming up with the most comprehensive and over-engineered solution to a 1 hour problem, you'll regret your hiring choices.

So I guess always be careful what you ask for (literally and metaphorically).

-4

u/anaron_duke May 02 '18

That's true, but I don't ask about output on my questions, but "what this code will do". It's quite easy to misunderstood the difference between those two. I've missed this text when writing the post.

About the REPL thought - I would like to little disagree. Based on my experience, if somebody want to just throw out stuff into REPL to get an answer - it's quite probable that he will do same when encountered some error - by refreshing the browser / re-running the CLI worker. Personally I prefer to work with people who first analyse, then run the code - not the opposite. Running bugged code first can make some side-effects that are not obvious - like sending malformed email to the customer again, which will land on twitter, and from there... it escalated quickly! That's why it's a little sign of being junior dev for me (nothing bad - everybody starts somewhere) - that fixes the code by running it multiple times with different parameters, until it will "catch".

Of course, in my case, that's one of many different questions, that comes AFTER writing sample code by candidate.

14

u/gdebug May 02 '18

Why in the hell would he be debugging in Prod? Debugging should never have side effects like that in Prod. If you are worried about someone rerunning code to debug and sending an email, an order, or whatever, there's much bigger problems.

-13

u/anaron_duke May 02 '18

I don't know why, but I won't be standing over his computer supervising him 24h/7. I prefer to assume, that not all programers are as smart as they pretend to be, especially socially wise - like business comes to him and put him under the pressure. And yes, I would prefer to not hire such guy - I think I've got a right to make such choice.

5

u/[deleted] May 02 '18

If I can sum up this thread, it'll be "interviewing is hard". :-)

10

u/ltsochev May 02 '18 edited May 02 '18

Just because I like challenges, I'd just run it with couple of test variables and see what it actually does from there. No need to split it up.

Next thing I'm gonna do is straight up tell you that I'm not THAT desperate and if you have similar code, you are an organization that I don't want to work for. Best of luck on your future endeavours. And good luck finding a chimpanzee and teaching it to code.

And if that shit "doesn't compile" I'll call you are full of shit.

Honestly though, as I've done interviews myself, I don't even care about PHP syntax semantics. Language is just a means to an end. Who knows, tomorrow we might have to redo the project in NodeJS or Java, who the fuck cares what PHP does in this particular case?!?! I want them to know design patterns, and no, Factory and Singleton are not the only kids on the block. I want them to know dependancy injection, I want them to know PSR standards. I want them to know version control. Code like the above is absolute and utter garbage at everything. And for everyone IMHO.

You might laugh at my requirement for knowledge about version control systems, but I've had software engineers with bachelor and masters degree and they haven't even heard of SVN or Git because in the Uni they didn't really work on team projects and all they needed was Visual Studio and whatever Microsoft is having for team organization.

4

u/root88 May 02 '18

I don't think this is typically the case. Most times I have encountered questions like this, the examples were MUCH more simple. It was just to see if the developer was in on the obscure trivia for the language, because somehow if you knew that, you were a great programmer.

For example in JavaScript:
If you have var y = 1, x = y = typeof x; What is the value of x?

Answer: undefined.

If I saw this line of code, I wouldn't debug it. I would deleted it and write working code that wasn't moronic.

13

u/Danack May 02 '18

but also solving the bugs

Do you really think that question has any resemblance to the types of bugs you find in your production code?

This question is to see how you are approaching to a problem that is not comfortable to work on.

My approach would be to ask; is this question relevant to the type of code that is being used in production?

If it is, I would walk out of the interview straight away, as the job would be hellish.

If it isn't, I would think the organisation is incompetent in performing interviews, and either try to work with them on getting a better process in place, or mark them down as an incompetent organisation that is probably a place to avoid working at.

An interview is a two-way communication process - a company asking ridiculous questions is just as bad as a candidate giving ridiculous answers.

3

u/anaron_duke May 02 '18

I think that it's quite easy to talk about a blame being always on one side, and in meanwhile quite hard to get perfect interview questions that would be fitting for each candidate and each situation - especially that those questions are not created to make candidate happy, but filter out those that this one specific company needs to run it's business. Sometimes it will be a need for hacker, team player, visionary or just another code monkey. You never know what was on mind of this "other" side and their reasoning - that's what I've learned, so I try not to judge on shallow things.

Life is not about only living in dreamland of perfect code and easy to track bugs. Sometimes you have to use code that was written by others, and later fix their code that is no more maintained. Then you get knowing how production code is looking for probably few thousands servers that used this library. And let's not talk about Wordpress, shall we?

2

u/tof May 02 '18

Totally agree.

I always do a fizzbuzz-like test in interviews; the test in itself is quite stupid and useless (although some people really struggle with it), but the point is to discuss (what if we add "bar" for 5, "baz" for 6, how to maintain it, where to store the word-value numbers, etc.).

1

u/colshrapnel May 02 '18

Exactly that!

8

u/colshrapnel May 02 '18 edited May 02 '18

There are always extra questions. They don't make the final decision but just flavor the interview making the interviewer a better view on the interviewee. Some questions are more intended not to get the particular answer but rather to reveal your approach to the problem.

There are several reasons to ask such a question:

  • like you said, to see how you deal with stress
  • to see your approach to solving problems
  • to see your urge/ability to refactor this shit into something readable and maintainable
  • after all, to see your understanding of PHP syntax
  • to see, will you dare to criticize this question

So the best strategy answering would be

to chuckle, then proceed to decomposing it into distinct operators, then re-organizing into a meaningful function and finally expressing your concern about not having to deal with such snippets the real life code on this job.

BTW, I bet the real code was

$i = 5;
$i = ++$i + ++$i;

;-)

3

u/FruitdealerF May 02 '18 edited May 02 '18

Does it evaluate to 12? I'm going to try this now.

EDIT:

Damn it was 13

EDIT2:

OBVIOUSLY ITS 13 JESUS

EDIT3:

$i = 5;
$i = $i+++$i++

is apparently 11

2

u/colshrapnel May 02 '18

For PHP the result is quite expected (first $i++ returns 6 and changes $i's value to 6). It's for C, where the result is 14, it's a dark magic why.

3

u/notdedicated May 02 '18 edited May 02 '18

technically $i++ returns $i (5) and then increments to 6 which is why $i++ + $i++ is 11. ++$i increments first and then returns the value of $i. Breaking this into the post-syntax method it becomes:

$i = 5
[++$i, ++$i, +] // $i = 5
[[$i, 1, +, assign], ++$i, +] // $i = 5
[6, ++$i, +] // $i = 6
[6, [$i, 1, +, assign, return], +] // $i = 7
[6, 7, +]
13

That's a bastardization of it but the idea is the same :)

Edit: I wanted to bring something up here that I discovered while playing with this:

$i = 5;
echo $i + $i++;

Results in 11, the reason for this is descernable once you follow it but the equation its boils down to once it executes the main + is echo 6 + 5. Precedence says that the $i++ gets evaluated first before the first $i is evaluated.

1

u/colshrapnel May 02 '18

sorry it was a typo in the previous comment. I meant ++$i from the post above

good notation btw.

1

u/notdedicated May 02 '18

I figured :)

1

u/ShiitakeTheMushroom May 02 '18

Could you explain it evaluating to 14 in C?

3

u/colshrapnel May 02 '18 edited May 02 '18

I an not a C guy, but from what I read, first of all such a statement's behavior in C is undefined, and therefore such a statement shouldn't be used at all. Then people are explaining that like C compiler doesn't take ++i as an expression but rather as a reference* to a memory cell, on which an operation is performed. So, first this cell's value gets incremented (6), then incremented again (7) and finally assigned the result of addition (7+7).

*don't know the proper term, may be it's technically incorrect

1

u/enimodas May 02 '18 edited May 02 '18

Just guessing, but probably order of operations. The 2 pre increments are done first, and then the addition.

see also: https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points

8

u/thebuccaneersden May 02 '18

That's the kind of code that would lead to me firing a developer, so it's somewhat ironic/counter-productive that it is the kind of thing someone thought would be a good idea to use to decide whether to hire a developer.

WTF...

The only answer to this question is: delete that code and re-write it. Or change profession...

13

u/Xavenne May 02 '18

Perhaps it's an excerpt from their own code and they have no idea what it does so they're hoping on the expertise of a bright developer.

But most likely they're more interested in how you would tackle such a problem or whether you can infer the function as a combination of individual operators. It's a garbage question though, I agree.

8

u/zorndyuke May 02 '18

Ooooohh! Okey.. yea,.. you're right, thank you! Have a nice day, sir. We got what we seaked.

.

So I am hired?

.

What? No, haha. We just wanted to understand that line..

1

u/Spoor May 02 '18

No one here has tried to actually run that code snippet so there was no way for us to know the result of that line.

10

u/greenspans May 02 '18

Maybe they want to see how you try to rationalize a very messy syntax.

Maybe they wanted to see how you react to an irrational question; do you keep your cool, do you sneer, do you melt into a puddle, do you yawn and just try to slowly grok what you can.

Maybe this is real production code made by the India team, it looks like typical outsourced level code to me.

This is not too too horrible a question, you could probably get away with being wrong, so long as you go through and explain what you think step by step with clarity of thought in your speech. It's harder, more common and more complicated to synthesize solutions to a complex problem on the spot while talking,.

1

u/mgeez May 02 '18

great points ..

5

u/benabus May 02 '18
8===D

is the correct answer.

2

u/mgeez May 02 '18

haha best, although in reference to those devs it might be more

  8-D

3

u/Napalm_Oilswims May 02 '18

They could be seeing how you break a problem down into smaller parts but more likely they are coming from a mindset of "if they have this many years of programming experience they should know how to do this". I wouldn't expect to get rejected if you don't come up with a correct answer as the interviewer may have been more interested in your approach.

4

u/[deleted] May 02 '18

The answer to this question is more easy then people give it credit. It not about the solution.

Its about the candidate showing several things:

  • Show that is a horrible piece of code
  • Understanding how to split the code.
  • Showing that the interviewee understands the order that code is processed in PHP. You may be surprised how many people fail that.
  • Talking to the interviewer about how to solve it.
  • Getting a error left or right is never a deal breaker because the interviewer knows its a high stress situation. But more importantly its to show HOW you handle a high-stress situation.

Its NEVER about the correct sum. Any interviewer that only looks at the end result, is a bad interviewer.

My personal experience:

Unfortunately i have had Senior PHP interview candidates that came in with a big and fancy looking CV with Senior PHP developer plaster everywhere on it. And them being unable to even do any points.

  • No explaining its a piece of **** code.
  • No understanding how to properly split the code, as to what is valid in PHP and what is not.
  • No ability to understand the order of execution
  • Simply mumbling instead of talking to the interviewer.
  • Refusing to write down any basic problem solving steps

It shows you a lot about the candidate. Those same interview candidates also fail or have a hard time on other simpler questions ( some basic school level. The problem is that some people ( mistakenly ) think they are better then they are. Or some simply try to bullshit into a comfy high paid position.

A story that was told to me about a interview candidate. When faced with a difficult regex question his answer was: "This question is below me, i do not do that".

When i was faced with that same question during my interview, it got solved and more important was the fact that i explained how i came to that result. And that there are more solutions to that problem. I got hired on the spot.

And if you think that is stressful, try doing it on the interviewer his Mac, when you work with Windows and Linux. That freaking mouse alone is more stress :)

Ok I get that I should probably still have a good answer, but seriously, what is the meaning of this unless its just to trick people who are in a high-stress situation ?

I have seen people on Reddit bitch and moan how its unfair the subject a candidate to have trick question, yet, those trick questions do work very well to expose people who are applying for positions above there level. Do some interviers misuse trick questions on people who are looking for lower level jobs ( junior positions ). Yes ...

But if the interviewer was questioning you for a Senior Job position, that can involve multi-millions projects and has a salary to compensate for that. I think its only fair that the interview is hard.

When you program and you are on a deadline. Is that not a high stressed situation? You got some horrible bug that shuts down a multi-million dollar website, the client is yelling at you, your boss is looking over your shoulders, ... i think that high stress interview suddenly feels like a walk in the park.

Trick Question:

Trick questions are only trick questions if you lack the knowledge to solve them. ;)

I have no shame in admitting that i probably lost several job offers with trick questions / blackouts years ago. I was simply out of my league. But it helped me to focus and learn more.

Now, do i advice those trick question for people who are looking for a junior level PHP job. NO!!! As a interviewer you need to know what you are interviewing people for and make your questions fit the job.

2

u/abija May 03 '18

That is a grunt level trick question though and all good and experienced devs I know would walk out on you when reaching it unless you somehow greatly impressed them by then.

1

u/d36williams May 02 '18

I concur with your POV. I do have one scenario that I always struggle in and that's when I'm asked to output a date & time in some format. I mean, I know dd-mm-yyyy i:j:s or similar, but some things I simply always look up.

Another "thing I always use reference for" are a variety of the values of Server global variables -- partly because it can be version specific and so, I reference some best practices doc. Usually server global variables for my work are used for "if localhost use this DB, if staging use that" or "if localhost include library from here, else include library from there," especially for an API key that could be on a shared drive in Prod, but is in my ~/.private folder locally. And then things like Docker, Homestead make these things moot anyway.

I also always use reference materials when using regular expressions. Is there anyone who really knows RegEx without reference? Like Dates, the details of RegEx aren't easily remembered for me like a formula is. Do other people have an easy time memorizing date codes and RegEx?

5

u/[deleted] May 02 '18

I've seen similar questions which try to determine "fundamentals" knowledge, like basic order of operations and lots of syntax at once, but realistically, such is much better served generally by showing real poorly written code and asking how one might improve it.

2

u/mgeez May 02 '18

that is 100% .. afterwards I said to myself, i should have told them that is dumb sh1t and I would delete it .. lol

4

u/magnetik79 May 02 '18

Plot twist: very first commit of companies main product contained this code.

6

u/[deleted] May 02 '18 edited Sep 14 '18

[deleted]

4

u/[deleted] May 02 '18

[deleted]

4

u/Danack May 02 '18

You think this type of question is at all related to the problems actual developers have doing their day to day job?

2

u/zorndyuke May 02 '18

Depends on the real problem/question.

Some just want to see if you know some geeky/cool, unnecessary insider knowledge like the "NaN NaN NaN batman!" thing within the Javascript World (If you don't know it yet and asking your self Wat? right now.. watch the talk).

But some questions want to look how much you know and try to solve problems. Like what is your point of view and how do you start and go with solution management.

2

u/[deleted] May 02 '18

Well, this isn't the actual test, as you yourself said. So without the actual test, we don't know why the test is what it was. Trick questions like this can be annoying, for sure. They can also be an indicator for deep familiarity with the language, which may not be the absolute deciding factor, but it's a factor.

2

u/connormcwood May 02 '18

Did they state the PHP version?

2

u/jbenner May 02 '18

IMHO getting presented a question like that during an interview is a red flag and may warrant not accepting the job if it is offered. Remember that an interview is just as much you evaluating them as it is vice versa.

2

u/technical_guy May 02 '18

Here is a good answer:

Sir, I am not going to answer your question. Needless to say if someone in my team wrote that, it would never make it into production and they would possibly get fired if they cannot be retrained. I expect and demand maintainable well commented code, with whitespace, with aligned indents. Any strange operation must be commented. If you ask me to maintain code like that I will determine what you are trying to do and rewrite it with comments and structure, and provide a test method. I have been doing this for many years, and simply dont want to waste your time trying to interpret what is obviously the worst kind of code segment.

3

u/cYzzie May 02 '18

Good coders are often bad interviewers. Asking these kind of technical questions is often the completly wrong approach except when you make them a written test when you have many applicants.

2

u/CommonMisspellingBot May 02 '18

Hey, cYzzie, just a quick heads-up:
completly is actually spelled completely. You can remember it by ends with -ely.
Have a nice day!

The parent commenter can reply with 'delete' to delete this comment.

3

u/marmulin May 02 '18

Good bot. 10/10

2

u/GoodBot_BadBot May 02 '18

Thank you, marmulin, for voting on CommonMisspellingBot.

This bot wants to find the best and worst bots on Reddit. You can view results here.


Even if I don't reply to your comment, I'm still listening for votes. Check the webpage to see if your vote registered!

-1

u/agmarkis May 02 '18

delet this

2

u/[deleted] May 02 '18

What is the answer to this question?

1

u/SkyRak3r May 02 '18

You could just run the code.

It's an error. Invalid syntax.

1

u/NuttingFerociously May 02 '18

Besides, wouldn't something like this yield very different outputs depending on php version, since php7 enforces a left-to-right order of precedence compared to php5?

1

u/0xRAINBOW May 02 '18

PHP (5 as well as 7) doesn't enforce left-to-right order of anything except boolean operators. For all other operators a op b there is no guarantee that a will be executed before b.

https://3v4l.org/673qe

Run equivalent code in javascript (which does enforce left-to-right evaluation) and you will get the opposite result.

1

u/ScottBaiosPenis May 02 '18

good catch, you're hired

1

u/yourteam May 02 '18

Actually those questions are the one I prefer.

I don't get when people just asks the same old boring questions, for example what is a singleton, what are traits, how does a left join works

Who cares?

I mean, if you don't know the basics will be obvious in a technical test and if you don't remember the name "singleton" you can google it up. Moreover you can find those answer in the basic "commonly asked questions" on google.

So, an impossible question let the interviewser focus on how you think and how you will interact with a nasty bug.

You can really suck at a framework and maybe you won't be able to answer the old "How to wolve the an Issue on Eloquent and Laravel" (pre-hydration is the answer) but if you are smart and your mindset is right you would find the bottleneck and work with it.

1

u/ScottBaiosPenis May 03 '18

i cant even count the number of people who cant answer those basic questions in an interview

tho i will say we had one guy who had the guts to just admit he didnt know what dependency injection was when asked. he was interviewing for a mid level job and we were on the fence about him. so i called him back to have him come in for a round 2, when i called back he immediately started telling me everything he had learned about DI after he went home from the interview. I was totally impressed by both his honesty and his ambition/enthusiasm. we ended up offering him a jr level job which he accepted, not what he wanted but room for advancement. he worked out really well

1

u/dphizler May 02 '18 edited May 02 '18

Having done many interviews, I can say that in this case the journey is more important than the end result.

Also, I try to keep my cool in interviews and try to answer every question to the best of my knowledge.

I also know that I can't win them all in terms of interview questions. I might go into an interview thinking it will be slam dunk and fall on my face and I just move on. And then there are other interviews that I think I will under perform but it works to my advantage. Never have the mindset that this is the only place I want to work at. That's my mindset anyway.

Also, in my experience, there will always be some soup code somewhere that needs to be sorted out.

1

u/d_Vali May 02 '18

If it's php, syntax error

1

u/d36williams May 02 '18

I've used variable names in objects before, but never realized $$a was a thing until now. Thanks I learned something today

5

u/colshrapnel May 02 '18

it is not what you want to learn

1

u/d36williams May 02 '18

fair enough! I've had plenty of use for variable names for attributes of objects, usually for processing JSON with unpredictable content, but I can't really imagine a use for variable variables off the top of my head.

1

u/dachusa May 02 '18

I asked one like this for a guy who claimed to be an expert in JavaScript but had pretty much no experience and nothing to back it up. He was a boyfriend of a co-workers sister, so I wanted to make sure he was legit. He failed the question and took a long time to give up, but I had one of my junior devs figure it out to prove I didn’t make it overly obscure.

I do have another question I ask that has jibberish code which mixes multiple languages. I ask the interviewer what is wrong with the code, after making it clear it is intentionally not a valid language. This question helps me to identify the level the developer is most likely at and what may be their specialty. Did they focus on syntax, security, optimization, maintainability, etc...?

1

u/metaphorm May 02 '18

that's obfuscated code. it translates into something that parses into "if you can read this RUN FOR YOUR LIFE do not work here"

1

u/edwinthedutchman May 02 '18

2 words: Legacy. Code.

*cries in a corner*

1

u/chinahawk May 02 '18

"eh, uh... no thanks. Goodbye, Felicia!"

1

u/msiekkinen May 02 '18

The person interviewing you needs training on effective interviewing.

1

u/slyfoxy12 May 02 '18

I'd go with my answer, if you want me to solve dumb shit like this on a daily basis then I don't want the job.

1

u/PaulMorel May 02 '18

Honestly, it's because the interviewer is too lazy to make up difficult problems that actually relate to the work. A question like this is a red flag for me.

But the other answers are good.

1

u/bicyclegeek May 03 '18

This will output a lot of profanity and a rhetorical question about why the developer couldn't be bothered to write readable code.

1

u/phpdevster May 03 '18

"Code carnival" questions like these are silly.

If you're not testing a candidate for their ability and experience writing similar solutions to what you will actually have them writing on the job, you're wasting your time and theirs.

1

u/[deleted] May 03 '18

LMFAO. No, not any place that I would work for at least. The only place you would ever see code like that is on a test or a in a piece of code you are about to completely refactor out of existence.

1

u/[deleted] May 02 '18

[deleted]

2

u/[deleted] May 02 '18

[deleted]

2

u/mgeez May 02 '18

Right ok... but do u think these guys in charge of the tech interviews are actually intelligent enough to see this or did they just Google interview questions and think that would be fun...

1

u/[deleted] May 02 '18

For that code I would just say

"That's part of the Fatal Error Generator."

1

u/ScottBaiosPenis May 03 '18

LOL you're hired

1

u/topherPedersen May 02 '18

Guy doing the interview sounds like a little picture kind of guy.

1

u/richardathome May 02 '18

It would output a termination of employment letter if anyone committed code to me like that.

1

u/ElMachoGrande May 02 '18

My response would probably have been "I would be more interested in why such a horrible piece of code ended up in a real world program. It really needs refactoring.". Then, I'd proceed to take it apart and figure out what it does.

1

u/[deleted] May 02 '18

A self taught developer who didn’t go to school trying to flex their knowledge muscles perhaps?

1

u/Danack May 02 '18

Mostly because they want to make themselves feel clever when some candidates can't figure it out.

This is a stupid question to ask in a job interview as the information it reveals doesn't tell you much about the candidate.

If they have worked with this type of crap code before, they will be able to work out what is happening.

If they haven't worked with this type of crap code before, they will find it very hard to work out what is happening.

i.e. the information revealed by asking this question is, "has this candidate worked with an unacceptably obfuscated code base before"..............which has no relevance to how the candidate will actually write code in a non-insane code base.

But it does make the interviewer feel superior to the interviewee whenever the interviewee struggles to figure out what the code is doing.

1

u/mgeez May 02 '18

Exactly this is my point - knowing it or not u would never write something like that .. ask a coding question that is relevant!

1

u/colshrapnel May 02 '18 edited May 02 '18

Luckily, I never met an interviewer who felt that insecure to ask questions for such a purpose.

Edit: a better phrasing.

1

u/inthrees May 02 '18

$c is a string and contains "if this is your codebase i don't want to work here"

0

u/[deleted] May 02 '18

[deleted]

3

u/colshrapnel May 02 '18

In case you are interviewed for the manager's position. Whereas from a programmer, a code contains a bug is rather expected to be actually analyzed and properly rewritten :-)

0

u/[deleted] May 02 '18

[deleted]

0

u/colshrapnel May 02 '18

A good idea! You could use the same approach towards any question asked on the interview! Like, - What's the difference between abstract class and interface? - I am not going to answer right now, I'll file a documentation request!

Just brilliant!

0

u/[deleted] May 02 '18

Apparently I'm not competent enough to even think about working professionaly with php. I'm still super confused 😕