<titledata-rh="true">Practice exam B | mf</title><metadata-rh="true"name="viewport"content="width=device-width,initial-scale=1"><metadata-rh="true"name="twitter:card"content="summary_large_image"><metadata-rh="true"property="og:url"content="https://blog.mfocko.xyz/pb071/pexam/garbage_collect/"><metadata-rh="true"name="docusaurus_locale"content="en"><metadata-rh="true"name="docsearch:language"content="en"><metadata-rh="true"name="docusaurus_version"content="current"><metadata-rh="true"name="docusaurus_tag"content="docs-pb071-current"><metadata-rh="true"name="docsearch:version"content="current"><metadata-rh="true"name="docsearch:docusaurus_tag"content="docs-pb071-current"><metadata-rh="true"property="og:title"content="Practice exam B | mf"><metadata-rh="true"name="description"content="Garbageeverywhere…
<em>faculty computers</em>.</li><li>In browser you are only allowed to have the following tabs open:<ul><li><ahref="https://en.cppreference.com"target="_blank"rel="noopener noreferrer">C documentation</a></li><li>page containing the assignment</li></ul></li><li>You <strong>are not</strong> allowed to use your own source code, e.g. prepared beforehand
or from the seminars.</li><li>You have <strong>5 minutes</strong> to read through the assignment and ask any follow-up
questions should be there something unclear.</li><li>You have <strong>60 minutes</strong> to work on the assignment, afterward your work will be
discussed with your seminar tutor.</li></ul></div></div><p>You have gotten into a trouble during your regular upgrade of your archLinux<supid="fnref-1"><ahref="#fn-1"class="footnote-ref">1</a></sup>
installation… You've been carelessly running the upgrades for months and forgot
about clearing up the caches.</p><p>Your task is to write a program <code>garbage_collect</code> that will evaluate the shell
history provided as a file and will try to find files or directories that are
suspiciously big and decide which of them should be deleted to free some space.</p><h2class="anchor anchorWithStickyNavbar_LWe7"id="format-of-the-shell-history">Format of the shell history<ahref="#format-of-the-shell-history"class="hash-link"aria-label="Direct link to Format of the shell history"title="Direct link to Format of the shell history"></a></h2><p>You are provided one file consisting of the captured buffer of the terminal. You
can see only two commands being used:</p><ol><li><p><code>cd ‹somewhere›</code> that changes the current working directory.</p><p>At the beginning you start in the root of the filesystem (i.e. <code>/</code>).</p><p>You are <strong>guaranteed</strong> that <code>‹somewhere›</code> is:</p><ul><li><code>.</code> that is a current working directory (i.e. does nothing),</li><li><code>..</code> that moves you up one level (in case you are in <code>/</code>, does nothing), or</li><li>is a valid directory in the current working directory.</li></ul><divclass="theme-admonition theme-admonition-caution alert alert--warning admonition_LlT9"><divclass="admonitionHeading_tbUL"><spanclass="admonitionIcon_kALy"><svgviewBox="0 0 16 16"><pathfill-rule="evenodd"d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>caution</div><divclass="admonitionContent_S0QG"><p>There are no guarantees or restrictions on the names of the files or
directories!</p></div></div></li><li><p><code>ls</code> that will list files in the current working directory and their
respective sizes. If there is a directory in the current working it has <code>dir</code>
instead of the size.</p></li></ol><divclass="codeBlockContainer_Ckt0 theme-code-block"style="--prism-color:#000000;--prism-background-color:#ffffff"><divclass="codeBlockContent_biex"><pretabindex="0"class="prism-code language-text codeBlock_bY9V thin-scrollbar"><codeclass="codeBlockLines_e6Vv"><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ ls</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">dir a</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">14848514 b.txt</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">8504156 c.dat</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">dir d</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd a</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd .</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd .</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd .</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ ls</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">dir e</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">29116 f</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">2557 g</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">62596 h.lst</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd e</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ ls</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">584 i</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd ..</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd ..</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd d</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ ls</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">4060174 j</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">8033020 d.log</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">5626152 d.ext</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">7214296 k</span><br></span></code></pre><divclass="buttonGroup__atx"><buttontype="button"aria-label="Copy code to clipboard"title="Copy"class="clean-btn"><spanclass="copyButtonIcons_eSgA"aria-hidden="true"><svgviewBox="0 0 24 24"class="copyButtonIcon_y97N"><pathfill="currentColor"d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svgviewBox="0 0 24 24"class="copyButtonSuccessIcon_LjdS"><pathfill="currentColor"d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>For this input, you will get following file system:</p><divclass="codeBlockContainer_Ckt0 theme-code-block"style="--prism-color:#000000;--prism-background-color:#ffffff"><divclass="codeBlockContent_biex"><pretabindex="0"class="prism-code language-text codeBlock_bY9V thin-scrollbar"><codeclass="codeBlockLines_e6Vv"><spanclass="token-line"style="color:#000000"><spanclass="token plain">- / (dir, size=48381165)</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain"> - a (dir, size=94853)</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain"> - e (dir, size=584)</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain"
directory that is supposed to be listed.</li></ul><p>When your program is being run with this switch, it is is supposed to print out
all files <strong>and</strong> directories that are bigger than the provided <code>min_size</code>.</p><h3class="anchor anchorWithStickyNavbar_LWe7"id="-f-total_size-min_unused"><code>-f ‹total_size›‹min_unused›</code><ahref="#-f-total_size-min_unused"class="hash-link"aria-label="Direct link to -f-total_size-min_unused"title="Direct link to -f-total_size-min_unused"></a></h3><p>With this switch you are provided two additional arguments:</p><ul><li><code>total_size</code> that is a total size of the filesystem<supid="fnref-2"><ahref="#fn-2"class="footnote-ref">2</a></sup>.</li><li><code>min_unused</code> that is a minimum of free space required for an upgrade.</li></ul><p>Your program should find <strong>exactly one</strong> file or a directory that is of the
smallest size, but big enough to free enough space for the upgrade to proceed.</p><p>In other words, if that file or directory is deleted, following should hold:</p><divclass="math math-display"><spanclass="katex-display"><spanclass="katex"><spanclass="katex-mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"display="block"><semantics><mrow><mrow><mimathvariant="monospace">t</mi><mimathvariant="monospace">o</mi><mimathvariant="monospace">t</mi><mimathvariant="monospace">a</mi><mimathvariant="monospace">l</mi><mimathvariant="monospace">_</mi><mimathvariant="monospace">s</mi><mimathvariant="monospace">i</mi><mimathvariant="monospace">z</mi><mimathvariant="monospace">e</mi></mrow><mo>−</mo><mrow><mimathvariant="monospace">u</mi><mimathvariant="monospace">s</mi><mimathvariant="monospace">e</mi><mimathvariant="monospace">d</mi></mrow><mo>≥</mo><mrow><mimathvariant="monospace">m</mi><mimathvariant="monospace">i</mi><mimathvariant="monospace">n</mi><mimathvariant="monospace">_</mi><mimathvariant="monospace">u</mi><mimathvariant="monospace">n</mi><mimathvariant="monospace">u</mi><mimathvariant="monospace">s</mi><mimathvariant="monospace">e</mi><mimathvariant="monospace">d</mi></mrow></mrow><annotationencoding="application/x-tex">\mathtt{total\_size} - \mathtt{used} \geq \mathtt{min\_unused}</annotation></semantics></math></span><spanclass="katex-html"aria-hidden="true"><spanclass="base"><spanclass="strut"style="height:0.7063em;vertical-align:-0.0951em"></span><spanclass="mord"><spanclass="mord mathtt">total_size</span></span><spanclass="mspace"style="margin-right:0.2222em"></span><spanclass="mbin">−</span><spanclass="mspace"style="margin-right:0.2222em"></span></span><spanclass="base"><spanclass="strut"style="height:0.7719em;vertical-align:-0.136em"></span><spanclass="mord"><spanclass="mord mathtt">used</span></span><spanclass="mspace"style="margin-right:0.2778em"></span><spanclass="mrel">≥</span><spanclass="mspace"style="margin-right:0.2778em"></span></span><spanclass="base"><spanclass="strut"style="height:0.7063em;vertical-align:-0.0951em"></span><spanclass="mord"><spanclass="mord mathtt">min_unused</span></span></span></span></span></span></div><h2class="anchor anchorWithStickyNavbar_LWe7"id="example-usage">Example usage<ahref="#example-usage"class="hash-link"aria-label="Direct link to Example usage"title="Direct link to Example usage"></a></h2><p>You can have a look at the example usage of your program. We can run your
program from the shell like</p><divclass="codeBlockContainer_Ckt0 theme-code-block"style="--prism-color:#000000;--prism-background-color:#ffffff"><divclass="codeBlockContent_biex"><pretabindex="0"class="prism-code language-text codeBlock_bY9V thin-scrollbar"><codeclass="codeBlockLines_e6Vv"><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ ./garbage_collect shell_history.txt -gt 10000000</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">24933642 /d</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">14848514 /b.txt</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">48381165 /</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain"style="display:inline-block"></span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ ./garbage_collect shell_history.txt -f 70000000 30000000</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">24933642 /d</span><br></span></code></pre><divclass="buttonGroup__atx"><buttontype="button"aria-label="Copy code to clipboard"title="Copy"class="clean-btn"><spanclass="copyButtonIcons_eSgA"aria-hidden="true"><svgviewBox="0 0 24 24"class="copyButtonIcon_y97N"><pathfill="currentColor"d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svgviewBox="0 0 24 24"class="copyButtonSuccessIcon_LjdS"><pathfill="currentColor"d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><h2class="anchor anchorWithStickyNavbar_LWe7"id="requirements-and-notes">Requirements and notes<ahref="#requirements-and-notes"class="hash-link"aria-label="Direct link to Requirements and notes"title="Direct link to Requirements and notes"></a></h2><ul><li>Define <strong>structures</strong> (and <strong>enumerations</strong>, if applicable) for the parsed
information from the files.</li><li>For keeping the “records”, use some <strong>dynamic</strong> data structure.<ul><li>Don't forget to consider pros and cons of using <em>specific</em> data structures
before going through implementing.</li></ul></li><li>You <strong>are not required</strong> to produce 1:1 output to the provided examples, they
are just a hint to not waste your time tinkering with a user experience.</li><li>If any of the operations on the input files should fail,
<strong>you are expected to</strong> handle the situation <em>accordingly</em>.</li><li>Failures of any other common functions (e.g. functions used for memory
management) should be handled in <strong>the same way</strong> as they were in the