Interviews: Testing (2021)

When syntax errors or minor logical issues can make or break your interview success, being able to test without thinking is essential.

Something that comes up often in interviews is manual testing. In the age of in-person interviews, you could often wave your hand and say "imagine I have a working heap class".

Facebook and a few other companies still have no-compile environments – and you can still assume working data structure implementations as a result.

But the large majority of companies in the post-COVID age, with virtual on-sites as the new normal, require you to run your code.

From my perspective, this is reasonable for practical questions (eg: parse a CSV and use it for some computation), but less reasonable for questions with intense conditional logic or edge case handling (eg: Valid Number).

Instead of spending time on problem solving, you may spend lots of time manually verifying your code (when in a compiled environment, you could just run and test it, similar to on-the-job).

When syntax errors or minor logical issues can make or break your interview success, being able to test without thinking is essential.

If you haven't practiced the skill, you should. You can and should use a template/framework to test your code. In most languages, a test array is the optimal approach.

Instead of having print(...) statements for every test case, write your test logic like you would write any other piece of efficient code – DRY.

That's all – as promised, here are the templates:

Javascript

// Create an array of test cases
const tests = [
 [[[1,0,0,1],[0,1,1,0],[0,1,1,1],[1,0,1,1]], 1],
 [[[1,1,0],[1,1,0],[0,0,1]], 2]
];

// Define variable for all tests
let allTestsPassed = true;

// Iterate through test cases
for (const [param, expected] of tests) {
	const result = findCircleNum(param);
    if (result !== expected) {
        allTestsPassed = false;
    	console.log(`Test failed for ${param}. Expected: ${expected}, Actual: ${result}`)
    } else {
    	console.log(`Test passed.`);
    }
}

// Log "all tests passed message"
if (allTestsPassed) console.log("** ALL TESTS PASSED **");

Python

# Create an array of test cases:
tests = [
 (params1, expected1),
 (params2, expected2),
 ...
]
for params, expected in tests:
  actual = f(*params)
  if actual != expected:
    print(f"Test failed for {params}. Expected: {expected}, Actual: {actual}")
  else:
    print(f"Works fine for {params}.")