{"id":1064,"date":"2026-10-30T12:16:27","date_gmt":"2026-10-30T12:16:27","guid":{"rendered":"https:\/\/blog.debuggersspace.com\/?p=1064"},"modified":"2026-10-30T14:13:06","modified_gmt":"2026-10-30T14:13:06","slug":"dry-run-this-loop-and-tell-me-the-output-pseudocode-with-nested-loops","status":"publish","type":"post","link":"https:\/\/blog.debuggersspace.com\/index.php\/2026\/10\/30\/dry-run-this-loop-and-tell-me-the-output-pseudocode-with-nested-loops\/","title":{"rendered":"Dry run this loop and tell me the output (pseudocode with nested loops)."},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>1 Minute, 37 Second                <\/div>\n\n            <\/div>\n<p class=\"wp-block-paragraph\">Pseudocode questions give you a short program and ask for the output. They reward careful tracing, not guessing. The method is always the same: write a table with one column per variable and one row per step.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Worked example 1<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>count = 0\nfor i = 1 to 4\n    for j = i to 4\n        if (i + j) mod 2 == 0\n            count = count + 1\nprint count<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">List the (i, j) pairs and mark the ones with an even sum:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>i=1: j=1(2 \u2713) j=2(3) j=3(4 \u2713) j=4(5)   -&gt; 2\ni=2: j=2(4 \u2713) j=3(5) j=4(6 \u2713)          -&gt; 2\ni=3: j=3(6 \u2713) j=4(7)                   -&gt; 1\ni=4: j=4(8 \u2713)                          -&gt; 1\ncount = 6<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output: <strong>6<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Worked example 2: a pattern<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>for i = 1 to 3\n    for j = 1 to i\n        print j (no new line)\n    print new line<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n12\n123<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The inner loop runs i times, so the first row has one number and the third has three.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Traps the test sets<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Whether &#8220;to&#8221; includes the end value. In these tests it usually does; check the question.<\/li>\n\n\n<li>The inner loop starting from <code>i<\/code> rather than 1.<\/li>\n\n\n<li>Integer division: <code>7 \/ 2<\/code> is 3, and <code>mod<\/code> with negative numbers.<\/li>\n\n\n<li>A variable changed inside the loop that also controls it.<\/li>\n\n\n<li>Where the print sits: inside the inner loop, the outer loop, or after both.<\/li>\n\n\n<li>Short-circuit: in <code>a &amp;&amp; b<\/code>, b is not evaluated when a is false.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Tips<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Count iterations first: nested loops of 1..n and i..n run n(n+1)\/2 times, a quick sanity check.<\/li>\n\n\n<li>Do not skip rows to save time; most mistakes come from skipping.<\/li>\n\n\n<li>In the online test, eliminate options that are impossible (for example an odd count when only pairs are counted).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><em>This answer was written by Debugger&#8217;s Space as practice material. See <a href=\"https:\/\/www.debuggersspace.com\/companies\/infosys\">how Infosys hires, step by step, and more Infosys practice questions<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to solve pseudocode and nested-loop output questions in the Infosys test and interview: a trace table method with two worked examples and the common traps.<\/p>\n","protected":false},"author":43,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[245],"tags":[275,229,294,293],"class_list":["post-1064","post","type-post","status-publish","format-standard","hentry","category-infosys-interview-preparation","tag-freshers","tag-infosys","tag-pseudocode","tag-puzzles"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Himanshu Namdeo","author_link":"https:\/\/blog.debuggersspace.com\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"How to solve pseudocode and nested-loop output questions in the Infosys test and interview: a trace table method with two worked examples and the common traps.","_links":{"self":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1064","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/comments?post=1064"}],"version-history":[{"count":1,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1064\/revisions"}],"predecessor-version":[{"id":1105,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1064\/revisions\/1105"}],"wp:attachment":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/media?parent=1064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/categories?post=1064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/tags?post=1064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}