{"id":1094,"date":"2026-11-29T09:47:00","date_gmt":"2026-11-29T08:47:00","guid":{"rendered":"https:\/\/grindloop.ai\/blog\/?p=1094"},"modified":"2026-11-28T16:07:48","modified_gmt":"2026-11-28T15:07:48","slug":"test-your-code-coding-interview","status":"publish","type":"post","link":"https:\/\/grindloop.ai\/blog\/test-your-code-coding-interview\/","title":{"rendered":"How to Test Your Code in a Coding Interview Without Running Out of Time"},"content":{"rendered":"<p>To test your code in a coding interview without running out of time, write the cases first. Start by asking whether the interviewer wants runnable tests or a spoken plan. Then list four or five cases as plain comments. Cover empty input and a single item. Add a boundary case, a nested case and unsorted input. Each case is also a question about the spec. So the list settles the rules early. Next, write a tiny check function that prints PASS or FAIL for each case. Skip the test framework. Run every case after each change. If everything passes on the first run, break one line on purpose to prove the checks can fail. The trap is testing only the example from the prompt. A buggy solution can pass that example. Below, an Android engineer works a merge-intervals problem in a shared editor. You&#8217;ll see the bug the prompt example hides and the harness that finds it. A script to say out loud comes last.<\/p>\n<h2>Ask how they want you to test your code in a coding interview<\/h2>\n<p>Interviewers don&#8217;t agree on this, so ask. Engineer Robert Heaton wrote a guide for the candidates he interviews, dated August 2023. His guide is called <a href=\"https:\/\/robertheaton.com\/interview\/\" target=\"_blank\" rel=\"noopener\">How to pass a coding interview with me<\/a>. It says simple unit tests help on most questions. He also says some interviewers won&#8217;t want them. Some problems are hard to test too. Code that makes HTTP calls is his example. His advice is to ask.<\/p>\n<p>Shopify&#8217;s engineering blog described its own coding exercise in a <a href=\"https:\/\/shopify.engineering\/nail-your-technical-shopify-interview\" target=\"_blank\" rel=\"noopener\">July 2022 guide to its technical interviews<\/a>. The page is Shopify&#8217;s recruiting material. It describes a 40-minute exercise in CoderPad. It tells candidates to run their code early and often. It quotes VP Cathy Polinsky too. &#8220;Think about edge cases and limitations and share your thought process out loud.&#8221; The page doesn&#8217;t say tests are scored. But an editor that runs code makes a test harness cheap. And edge cases are what a harness checks.<\/p>\n<p>Take Nadia, an Android engineer with four years of Kotlin, in a round like that. Her prompt is to merge a list of busy calendar blocks into the fewest blocks. The example input is 9 to 12, 11 to 13 and 15 to 16. The expected output is 9 to 13 and 15 to 16. She has 45 minutes in a shared editor that runs Kotlin. She opens with the question. &#8220;Do you want runnable tests, or should I talk through it?&#8221; The interviewer says runnable is fine.<\/p>\n<h2>A written case list doubles as the spec<\/h2>\n<p>Nadia spends three minutes on cases before any code. She writes them as comments at the top of the file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">\/\/ empty list        -&gt; empty list\n\/\/ one block         -&gt; same block\n\/\/ 9-10, 10-11       -&gt; 9-11 ?\n\/\/ 9-17, 12-13       -&gt; 9-17\n\/\/ 14-15, 9-10       -&gt; 9-10, 14-15 (input not sorted)<\/pre>\n<p>The third case has a question mark, because the prompt doesn&#8217;t say. Two meetings that touch at 10:00 could count as one busy block or two. So she asks. The interviewer says back-to-back meetings are one busy block. That answer changes one comparison in her code. Without the list, she would have guessed.<\/p>\n<p>The fifth case asks the same kind of question. The prompt example arrives sorted. Nothing says every input will. So the list catches a second rule before it becomes a bug.<\/p>\n<h2>A print-based check beats a test framework in a 45-minute editor<\/h2>\n<p>Heaton says to skip a testing framework unless it&#8217;s required. Knowing one very well is his other exception. A plain check that prints Pass or Fail is enough, in his view. You can mention what you would use at work. He also warns against printing return values and checking them by eye. Nadia&#8217;s version takes a few lines of Kotlin.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">data class Slot(val start: Int, val end: Int)\n\nfun check(name: String, actual: List&lt;Slot&gt;, expected: List&lt;Slot&gt;) {\n    println(if (actual == expected) &quot;PASS $name&quot; else &quot;FAIL $name: got $actual&quot;)\n}<\/pre>\n<p>A <code>data class<\/code> gives her structural equality for free. So one <code>==<\/code> compares whole lists. JUnit would need a build setup that a shared editor may not have. Here it costs minutes and adds nothing the round needs.<\/p>\n<p>Heaton also says to keep one test per input rather than editing a single case. Nadia follows that. Each comment from her list becomes one line.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">fun runAll() {\n    check(&quot;empty&quot;, merge(emptyList()), emptyList())\n    check(&quot;single&quot;, merge(listOf(Slot(9, 10))), listOf(Slot(9, 10)))\n    check(&quot;touching&quot;, merge(listOf(Slot(9, 10), Slot(10, 11))), listOf(Slot(9, 11)))\n    check(&quot;nested&quot;, merge(listOf(Slot(9, 17), Slot(12, 13))), listOf(Slot(9, 17)))\n    check(&quot;unsorted&quot;, merge(listOf(Slot(14, 15), Slot(9, 10))), listOf(Slot(9, 10), Slot(14, 15)))\n}<\/pre>\n<h2>The prompt example passes a solution with a boundary bug<\/h2>\n<p>Here is Nadia&#8217;s first <code>merge<\/code>. It sorts by start time, then folds each block into the last one when they overlap.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">fun merge(slots: List&lt;Slot&gt;): List&lt;Slot&gt; {\n    val out = mutableListOf&lt;Slot&gt;()\n    for (s in slots.sortedBy { it.start }) {\n        val last = out.lastOrNull()\n        if (last != null &amp;&amp; s.start &lt; last.end) {\n            out[out.lastIndex] = Slot(last.start, maxOf(last.end, s.end))\n        } else out += s\n    }\n    return out\n}<\/pre>\n<p>Run it on the prompt example and it returns 9 to 13 and 15 to 16. That&#8217;s correct. A candidate who tests only the example stops here. We ran Nadia&#8217;s five checks against this version with <code>kotlinc<\/code>. Four passed. The touching case failed and printed two blocks, 9 to 10 and 10 to 11.<\/p>\n<p>The cause is the strict <code>&lt;<\/code>. A block that starts exactly when the last one ends isn&#8217;t merged. The fix is <code>&lt;=<\/code>. That rule came from the question she asked in minute three. With that one change, all five checks printed PASS. The prompt example could never have caught it. Its blocks overlap by a full hour, so the boundary never comes up.<\/p>\n<h2>Break one line on purpose if every test passes the first time<\/h2>\n<p>Suppose Nadia had written <code>&lt;=<\/code> from the start. Then all five checks pass on the first run. Heaton&#8217;s guide says that&#8217;s the moment to add a bug on purpose. A check that can&#8217;t fail proves nothing. A typo in <code>check<\/code> could print PASS for everything.<\/p>\n<p>Nadia flips <code>&lt;=<\/code> back to <code>&lt;<\/code> and runs again. The touching case fails, so the harness works. She restores the line. The detour takes under a minute. It also shows the interviewer which case guards which line of code.<\/p>\n<h2>Spend about a quarter of the round on cases and checks<\/h2>\n<p>Nadia&#8217;s 45 minutes break down like this. Clarifying and the case list take minutes 0 to 8. The harness takes minutes 8 to 11. The first working <code>merge<\/code> lands around minute 20. Then she runs all checks after every edit. That&#8217;s Shopify&#8217;s run-early-and-often advice turned into a habit. The fix and the deliberate-bug test end by minute 25. That leaves 20 minutes for the follow-up. In Nadia&#8217;s round, it changes a rule.<\/p>\n<p>The follow-up is where the harness pays off a second time. Say the interviewer asks for a minimum gap, so blocks less than five minutes apart also merge. Nadia adds two checks for the new rule and reruns all seven. The old five tell her the change broke nothing.<\/p>\n<p>Some rounds have no run button. In an in-person whiteboard round, the same case list drives a hand trace instead. Our post on <a href=\"https:\/\/grindloop.ai\/blog\/in-person-coding-interview-hand-trace\/\">hand-tracing in an in-person coding interview<\/a> covers that version.<\/p>\n<h2>Say the test plan out loud as you build it<\/h2>\n<p>Nadia says each step as she does it, so the interviewer hears why she picked each case. These six lines fit most array or list problems.<\/p>\n<blockquote>\n<p>&#8220;Do you want runnable tests, or should I talk through how I&#8217;d test it?&#8221;<\/p>\n<p>&#8220;Before I code, here are my cases. Empty and one item. Two that touch. One inside another. Unsorted input.&#8221;<\/p>\n<p>&#8220;I&#8217;m not sure about the touching case. Do back-to-back blocks count as one?&#8221;<\/p>\n<p>&#8220;I&#8217;ll use a small check that prints PASS or FAIL. Then I don&#8217;t read output by eye.&#8221;<\/p>\n<p>&#8220;Everything passed first time. So I&#8217;ll break the comparison and see if the touching case catches it.&#8221;<\/p>\n<p>&#8220;For the gap rule, I&#8217;m adding two cases. I&#8217;ll rerun the old five to catch regressions.&#8221;<\/p>\n<\/blockquote>\n<p>The first line settles the format before you spend time on it. The second gives the interviewer your whole plan in one sentence. They can add a case you missed. The third turns an edge case into a spec question instead of a guess. The fourth explains the harness in terms of a risk. The fifth proves the checks work. The last shows you treat old tests as a safety net. That&#8217;s how tests work on a real team.<\/p>\n<p>The same habit helps with complexity. A case list makes you think about input size early. Our post on <a href=\"https:\/\/grindloop.ai\/blog\/kotlin-coding-interview-time-complexity\/\">the Kotlin list calls that make your Big-O wrong<\/a> covers what to check there.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to test your code in a coding interview: ask what the interviewer wants, list edge cases first, run a PASS\/FAIL check in Kotlin. Script inside.<\/p>\n","protected":false},"author":3,"featured_media":1095,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to Test Your Code in a Coding Interview Without Running Out of Time","rank_math_description":"How to test your code in a coding interview: ask what the interviewer wants, list edge cases first, run a PASS\/FAIL check in Kotlin. Script inside.","rank_math_focus_keyword":"test your code in a coding interview","footnotes":""},"categories":[20],"tags":[15,44,52,16,73,45],"class_list":["post-1094","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-interview-strategy","tag-android","tag-coding-interview","tag-hiring-process","tag-interview-prep","tag-live-coding","tag-technical-interview"],"_links":{"self":[{"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/posts\/1094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/comments?post=1094"}],"version-history":[{"count":1,"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/posts\/1094\/revisions"}],"predecessor-version":[{"id":1096,"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/posts\/1094\/revisions\/1096"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/media\/1095"}],"wp:attachment":[{"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/media?parent=1094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/categories?post=1094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/grindloop.ai\/blog\/wp-json\/wp\/v2\/tags?post=1094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}