{"id":3593,"date":"2011-01-11T16:00:00","date_gmt":"2011-01-11T16:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2011\/01\/11\/c-programming-interviews-what-interviewers-want-you-to-show\/"},"modified":"2021-10-06T11:00:30","modified_gmt":"2021-10-06T11:00:30","slug":"c-programming-interviews-what-interviewers-want-you-to-show","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/c-programming-interviews-what-interviewers-want-you-to-show\/","title":{"rendered":"C++ Programming Interviews: What Interviewers Want You To Show"},"content":{"rendered":"<p>A <a href=\"http:\/\/www.eweek.com\/c\/a\/Application-Development\/Java-C-C-Top-18-Programming-Languages-for-2011-480790\/\" target=\"_blank\" rel=\"noopener\">recent study<\/a> showed that C++ is still among the most used languages (ahead of any other Visual Studio officially supported language like C# or Visual Basic). But despite being widely used and known by several generations of developers \u2013thanks that it has been out there for decades now, while being still the most taught language in the Academy- when you look for a C++ development job those facts don\u2019t guarantee that you\u2019ll be chosen. It will depend on how well you perform in the programming interview \u2013among other factors. Regardless of how deeply you know the C++ syntax, how many STL components you memorize, it will be your abilities of using \u201cthe right tool for the right task\u201d what will define your eligibility. This post will seem like a lost chapter of the book <a href=\"http:\/\/bit.ly\/a7hM1i\" target=\"_blank\" rel=\"noopener\">Programming Interviews Exposed-Secrets to Landing Your Next Job, 2nd Edition<\/a> [Mongan, Soujanen, Gigu\u00e8re; Wrox 2007], but it\u2019s based on reflections I can share from a programming interview I participated. While this post is initially intended for candidates who are to be interviewed, it feels like a novice programming interviewer may get some ideas from here about what to check on a candidate.<\/p>\n<p>&nbsp;<\/p>\n<p>In an interview, I was asked to pseudo-code a function that returns the decimal equivalent of a roman numeral received as input. It seems like a trivial challenge but what the aspiring candidate must keep in mind, first of all, is the fact that the interviewer isn\u2019t curious about your background with roman numerals. What he\/she wants to check are your abilities in problem solving. Beside, the roman numeric system isn\u2019t that trivial compared with the decimal system. It\u2019s non-positional (in the sense that you don\u2019t get ones in the last position, tens in the penultimate, hundreds in the position before, etc.). Beside, the system only contains symbols to represent powers of 10 <em>(10<sup>n<\/sup>, n=0, \u2026,3)<\/em> and <em>5&#215;10<sup>n<\/sup> (n=0, \u2026,2)<\/em>. Bizarre enough, huh? I wonder if the romans created this numeral system in order to later use it in programming interviews.<\/p>\n<p>Regarding those unusual features, the first thing I started doing \u2013in front of my interviewer and with the whiteboard marker in my hand- was an exploration of typical equivalences, easy and not so easy roman numeral cases with their expected result. Those served me to both, craft the algorithm toward the general case and test my derived solution for correctness.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2011\/01\/7026.image_1DDB4998.png\"><img decoding=\"async\" class=\"size-full wp-image-29314 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2011\/01\/7026.image_1DDB4998.png\" alt=\"Image 7026 image 1DDB4998\" width=\"240\" height=\"154\" \/><\/a><\/p>\n<p>Those and some few other examples help us determine some basic syntax rules in roman numerals. Rules like repetition of characters (III, CC, XXX, \u2026); subtraction (IX, CD, \u2026); etc.<\/p>\n<p>Those same rules have their own limitations, for instance<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2011\/01\/4300.image_4F66B72D.png\"><img decoding=\"async\" class=\"size-full wp-image-29313 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2011\/01\/4300.image_4F66B72D.png\" alt=\"Image 4300 image 4F66B72D\" width=\"240\" height=\"158\" \/><\/a><\/p>\n<p>In other words, repetition is only allowed to characters I, X, C and M (1, 10, 100 and 1000 respectively) but not to V, L, D (5, 50, 500 respectively) and -while not represented in these examples- it can\u2019t happen more than three times either. Subtracting V, L or D isn\u2019t allowed either. Likewise, the 3rd erroneous example shows that we can get a character subtracting its immediate follower, but this latter can\u2019t subtract to a third one (that is to say, a subtracting portion in a whole roman numeral only involves two characters).<\/p>\n<p>Talk about all these rules, exceptions and thoughts like thinking in loud voice, as you write them down in some margin (whiteboard in my case but it could be a piece of paper you were given). If available, use two different colors to separate DO\u2019s and DON\u2019T\u2019s.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2011\/01\/0576.image_1D02E3AE.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-29312\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2011\/01\/0576.image_1D02E3AE.png\" alt=\"Image 0576 image 1D02E3AE\" width=\"500\" height=\"288\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2011\/01\/0576.image_1D02E3AE.png 500w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2011\/01\/0576.image_1D02E3AE-300x173.png 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>There are yet other syntax rules but, with my interviewer, we agreed to stop here as was good enough to show some skills in the remaining time.<\/p>\n<p>Let\u2019s start shaping the function skeleton, its public interface and expected return types<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0afe1f38-d098-4c65-960c-3c92c1aeb676\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; float: none; padding: 0px;\">\n<div style=\"border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;\">\n<div style=\"background: #fff; max-height: 300px; overflow: auto;\">\n<ol style=\"background: #ffffff; margin: 0; padding: 0 0 0 5px;\">\n<li><span style=\"color: #0000ff;\">namespace<\/span> RomanNumeral {<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">unsigned<\/span> <span style=\"color: #0000ff;\">int<\/span> rtoi(<span style=\"color: #0000ff;\">const<\/span> basic_string&lt;Ch&gt;&amp; romanStr);<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0&#8230;<\/li>\n<li style=\"background: #f3f3f3;\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p>The function name, <span style=\"font-family: Courier New; font-size: x-small;\">rtoi()<\/span>, was chosen for its similarity in intention with <span style=\"font-family: Courier New; font-size: x-small;\">atoi()<\/span>. There are several decisions to explain here:<\/p>\n<ul>\n<li>The returned type was chosen as <span style=\"font-family: Courier New; font-size: x-small;\">unsigned int<\/span>. Roman numerals don\u2019t consider neither 0 (zero) nor negative numbers. For that reason, I wanted to make sure that the returned type were as restrictive as possible.\nIt may be argued that by allowing all kind of <span style=\"font-family: Courier New; font-size: x-small;\">int<\/span>, we could have taken the convention of using negative numbers to represent errors like invalid inputs, etc. But the drawback of that approach is that the caller of the function will have to take the precaution of explicitly asking for error conditions coming as output. If the caller fails to check this out and just used the result instead, the execution would turn into an undetermined state.\nThat\u2019s why I preferred to escalate an error. We recently debated about exceptions and while there\u2019s no one-size-fits-all solution for it, I liked to declare my conversion exception as a subclass of STL runtime error:<\/li>\n<\/ul>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f2e8d34a-be47-45bd-b7c3-8a14a93c94fc\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; float: none; padding: 0px;\">\n<div style=\"border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;\">\n<div style=\"background: #fff; overflow: auto;\">\n<ol style=\"background: #ffffff; margin: 0; padding: 0 0 0 5px;\">\n<li><span style=\"color: #0000ff;\">namespace<\/span> RomanNumeral {<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0&#8230;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">enum<\/span> ConversionErrorCause {<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0IllegalSymbol,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">\/\/ input contains a symbol other than I, V, X, L, C, D, M<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0IllegalRepetition,\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">\/\/ I, X, C, M repeated more than three times, or any other symbol repeated<\/span><\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0IllegalSubtraction\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">\/\/ V, L, D can&#8217;t subtract<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0};<\/li>\n<li style=\"background: #f3f3f3;\"><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">class<\/span> ConversionError : <span style=\"color: #0000ff;\">public<\/span> std::runtime_error<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0{<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">private<\/span>:<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ConversionErrorCause m_Cause;<\/li>\n<li><\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">public<\/span>:<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ConversionError(ConversionErrorCause);<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ConversionErrorCause cause();<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0};<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0&#8230;<\/li>\n<li>}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<blockquote><p>There\u2019s no prescription indicating that your exceptions should inherit from STL <span style=\"font-family: Courier New; font-size: x-small;\">runtime_error<\/span> or any other STL exception, but there\u2019s some consensus that if this practice were widely adopted, you have an alternative to the <span style=\"font-family: Courier New; font-size: x-small;\">catch (&#8230;)<\/span> ellipsis that in Windows and non-Windows systems may catch things other than C++ exceptions (beside not telling you exactly what exception occurred, just that something went wrong).\nConversely, some other authors have arguments against throwing exceptions (arguing that it comes at the expense of a runtime overhead among other reasons.) They suggests alternatives like the old C global <span style=\"font-family: Courier New; font-size: x-small;\">errno<\/span> or similar (especially when the function is intended to validate user input). Those authors propose that exception throwing should be a last instance resort when nothing else may be attempted. In my case, this function is just a library function: it may be called by a UI validator object or by another component not necessarily related with user input so I can\u2019t make such assumptions like a human been being in front of the application to attempt a manual recovery, etc. This general purpose function will react to input errors by throwing exceptions. Period.\nAs you can see in the code sample above, I created a generic exception for any potential cause instead of making a complete exception hierarchy, as long as I still provide through its <span style=\"font-family: Courier New; font-size: x-small;\">cause()<\/span> method (and, when defining this exception, through its <span style=\"font-family: Courier New; font-size: x-small;\">std::runtime_error::what()<\/span> method as well) what specifically failed.\nAlright, enough said about exceptions, let\u2019s keep reviewing the function interface.<\/p><\/blockquote>\n<ul>\n<li>Regarding its input argument, the reader might wonder why to declare it as <span style=\"font-family: Courier New; font-size: x-small;\">const<\/span>. The fact is that this function shouldn\u2019t modify its input in any way. Therefore, is not a bad idea to enforce that so if some day, as a consequence of maintenance, any programmer modified the input declared as constant, the application wouldn\u2019t compile.<\/li>\n<li>Another aspect considering the input string is the fact that I chose it as <span style=\"font-family: Courier New; font-size: x-small;\">wstring<\/span> instead of simply <span style=\"font-family: Courier New; font-size: x-small;\">string<\/span> type. The fact is that most systems nowadays work with expanded character sets (typically UNICODE), as they better handle internationalization in currently Internet-enabled, interconnected age. You don\u2019t have to reason like me here, as long as you are able to understand the context where your decisions are being taken. A wrong decision, though, would have been preferring <span style=\"font-family: Courier New; font-size: x-small;\">string<\/span> for simplicity: from a programming perspective, both types are siblings, specializing a common STL type called basic_string (with <span style=\"font-family: Courier New; font-size: x-small;\">char<\/span> and <span style=\"font-family: Courier New; font-size: x-small;\">wchar_t<\/span>, depending on the simple or expanded case). In fact, if the interviewer asked you about a generic solution, that would come as follows:<\/li>\n<\/ul>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d5cf8182-8922-4aa7-8874-454dde3de2e9\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; float: none; padding: 0px;\">\n<div style=\"border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;\">\n<div style=\"background: #fff; max-height: 500px; overflow: auto;\">\n<ol style=\"background: #ffffff; margin: 0; padding: 0 0 0 5px;\">\n<li><span style=\"color: #0000ff;\">namespace<\/span> RomanNumeral {<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">template<\/span>&lt;<span style=\"color: #0000ff;\">typename<\/span> Ch&gt; <span style=\"color: #0000ff;\">unsigned<\/span> <span style=\"color: #0000ff;\">int<\/span> rtoi(<span style=\"color: #0000ff;\">const<\/span> basic_string&lt;Ch&gt;&amp; romanStr);<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0&#8230;<\/li>\n<li style=\"background: #f3f3f3;\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<blockquote><p>Yet one may argue about the decision of using the STL version of strings instead of the C-style, 0 (zero)-terminated char arrays. The decision in favor of the STL ones was based on the fact that they encapsulate a lot of the functionality a string must accomplish in order to be considered <em>safe<\/em>. For instance, a <span style=\"font-family: Courier New; font-size: x-small;\">wchar_t*<\/span> counterpart of our <span style=\"font-family: Courier New; font-size: x-small;\">wstring<\/span> could have come without the expected binary 0 (zero) ending char, leading to a potential buffer overrun. An STL-based string doesn\u2019t have this problem because it handles its own memory (first reason), and its length isn\u2019t based on a potentially missed ending flag. STL strings are safer than their C-style counterparts, what doesn\u2019t mean they are safe. They are just less error-prone and therefore recommended by several authors. They may impose certain overhead but in most of cases that overhead won\u2019t be absent in high quality, defensive-coding version using C-style strings. It\u2019s just a matter of deciding whether you programmer want to be responsible for that overhead or take advantage of those already baked versions that the STL put available for you.<\/p><\/blockquote>\n<ul>\n<li>A last comment, prior to move inside the function definition itself, is the fact that the string argument is passed as a reference (by adding the ampersand modifier). It\u2019s a matter of efficiency: if the ampersand was not present, the string would be passed by value, being copied to the stack with the consequent overhead. I\u2019d rather avoid that.<\/li>\n<\/ul>\n<p>All these said, let\u2019s go through the function definition. I\u2019ll finally implement here a generic version (now that I\u2019ve mentioned that possibility)<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8cc7a533-080c-45dd-b4b9-d860eda65556\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; float: none; padding: 0px;\">\n<div style=\"border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;\">\n<div style=\"background: #fff; overflow: auto;\">\n<ol style=\"background: #ffffff; margin: 0; padding: 0 0 0 5px;\">\n<li><span style=\"color: #0000ff;\">template<\/span>&lt;<span style=\"color: #0000ff;\">typename<\/span> Ch&gt; <span style=\"color: #0000ff;\">unsigned<\/span> <span style=\"color: #0000ff;\">int<\/span> rtoi(<span style=\"color: #0000ff;\">const<\/span> basic_string&lt;Ch&gt;&amp; romanStr) {<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">int<\/span> result = 0;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">\/* value to return *\/<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0AdditiveTerm currentTerm;<\/li>\n<li style=\"background: #f3f3f3;\"><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">unsigned<\/span> <span style=\"color: #0000ff;\">int<\/span> repeatedTimes;<\/li>\n<li style=\"background: #f3f3f3;\"><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0Ch lastSymbol = L<span style=\"color: #a31515;\">&#8216;\\0&#8217;<\/span>, currentSymbol, nextSymbol;<\/li>\n<li style=\"background: #f3f3f3;\"><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">\/* main computing loop: inside reasonable length and without reaching the<\/span><\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">end of the null terminated string *\/<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #0000ff;\">typename<\/span> basic_string&lt;Ch&gt;::const_iterator iter = romanStr.begin(),<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0iterNext;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0iter!=romanStr.end();<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0iter+= currentTerm.size,<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result+= currentTerm.value) {<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0currentSymbol = *iter;<\/li>\n<li><\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">\/* Rule 1: Repetition. Can&#8217;t happen more than three times and not every<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">symbol is repeatable *\/<\/span><\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">if<\/span> (lastSymbol==currentSymbol) {<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">if<\/span> ((++repeatedTimes==4)||(isNotRepeatable(currentSymbol))) {<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">throw<\/span> ConversionError(IllegalRepetition);<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} <span style=\"color: #0000ff;\">else<\/span> {<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0repeatedTimes = 1;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0lastSymbol = currentSymbol;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/li>\n<li style=\"background: #f3f3f3;\"><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">\/* the current symbol plus its follower (if not at the end) are <\/span><\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">evaluated in getNexAdditive() to see how much to cumulate*\/<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0nextSymbol = ((iterNext = iter+1)==romanStr.end()) ? L<span style=\"color: #a31515;\">&#8216;0&#8217;<\/span> : *(iterNext);<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0currentTerm = getNextTerm(currentSymbol, nextSymbol);<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0}<\/li>\n<li style=\"background: #f3f3f3;\"><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">return<\/span> result;<\/li>\n<li style=\"background: #f3f3f3;\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p>In few words, this function consists in a loop through all the input string chars. For each char we check whether it repeats or not the last one (in case it does, we control that repeated chars don\u2019t reach 4 times). We also make sure that not every symbol is allowed to be repeated by calling the inline function <span style=\"font-family: Courier New; font-size: x-small;\">isNotRepeatable(\u2026)<\/span> \u2013defined as follow:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:cee4c41d-18e1-4a42-b092-9a4f39cfb159\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; float: none; padding: 0px;\">\n<div style=\"border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;\">\n<div style=\"background: #fff; overflow: auto;\">\n<ol style=\"background: #ffffff; margin: 0; padding: 0 0 0 5px;\">\n<li><span style=\"color: #0000ff;\">template<\/span>&lt;<span style=\"color: #0000ff;\">typename<\/span> Ch&gt; <span style=\"color: #0000ff;\">inline<\/span> <span style=\"color: #0000ff;\">bool<\/span> isNotRepeatable(Ch c) {<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">return<\/span> ((c==L<span style=\"color: #a31515;\">&#8216;V&#8217;<\/span>)||(c==L<span style=\"color: #a31515;\">&#8216;L&#8217;<\/span>)||(c==L<span style=\"color: #a31515;\">&#8216;D&#8217;<\/span>));<\/li>\n<li>}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p>By making it <em>inline<\/em>, we are instructing the compiler to avoid making any explicit call involving the stack. Instead this code will be spanned in the calling method, making the proper variable substitution. We could have defined a macro this way<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:275012f6-9810-4667-8019-4818e14e6a7f\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; float: none; padding: 0px;\">\n<div style=\"border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;\">\n<div style=\"background: #fff; overflow: auto;\">\n<ol style=\"background: #ffffff; margin: 0; padding: 0 0 0 5px;\">\n<li><span style=\"color: #0000ff;\">#define<\/span>isNotRepeatable(c)\u00a0\u00a0\u00a0\u00a0(((c)==L<span style=\"color: #a31515;\">&#8216;V&#8217;<\/span>)||((c)==L<span style=\"color: #a31515;\">&#8216;L&#8217;<\/span>)||((c)==L<span style=\"color: #a31515;\">&#8216;D&#8217;<\/span>))<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p>but in that case we have less control on data being passed\/received. An inline function is a better practice and presumably won\u2019t involve the overhead of a true function call.<\/p>\n<p>The iteration then locates the symbol next to the one being analyzed in the current iteration, and passes both to the function <span style=\"font-family: Courier New; font-size: x-small;\">getNextTerm(\u2026)<\/span>. The purpose of that function, given those two symbols, is to determine whether both should be combined in a subtraction (the typical case of IV, where I is being subtracted from V for instance), or a case like VI, where only the V will be considered for the iteration, discarding the I (which will be picked up by the next iteration).<\/p>\n<p>Thus, the output of this function is a <span style=\"font-family: Courier New; font-size: x-small;\">struct<\/span> defined as follows<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a330a2c6-51fc-44a7-b4c8-4aaeea82aa56\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; float: none; padding: 0px;\">\n<div style=\"border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;\">\n<div style=\"background: #fff; overflow: auto;\">\n<ol style=\"background: #ffffff; margin: 0; padding: 0 0 0 5px;\">\n<li><span style=\"color: #0000ff;\">typedef<\/span> <span style=\"color: #0000ff;\">struct<\/span> AdditiveTerm {<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">unsigned<\/span> <span style=\"color: #0000ff;\">int<\/span> value, size;<\/li>\n<li>} AdditiveTerm;<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p>As the reader may see, the <span style=\"font-family: Courier New; font-size: x-small;\">value<\/span> member contains the value to be added to the ongoing result. The <span style=\"font-family: Courier New; font-size: x-small;\">size<\/span> member, instead, will contain either 1 or 2 depending on whether both symbols were considered (as a subtraction) or only the first one while the other was ignored. Those two activities can be seen as the ending portion of the <span style=\"font-family: Courier New; font-size: x-small;\">for<\/span> loop.<\/p>\n<p>Let\u2019s complete the implementation by defining <span style=\"font-family: Courier New; font-size: x-small;\">getNextTerm(\u2026)<\/span> function:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:79c4236b-f32e-41c5-b616-9330bcb22a85\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px; float: none; padding: 0px;\">\n<div style=\"border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;\">\n<div style=\"background: #fff; max-height: 500px; overflow: auto;\">\n<ol style=\"background: #ffffff; margin: 0; padding: 0 0 0 5px;\">\n<li><span style=\"color: #0000ff;\">template<\/span>&lt;<span style=\"color: #0000ff;\">class<\/span> Ch&gt; AdditiveTerm getNextTerm(<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">const<\/span> Ch firstRomanChar, <span style=\"color: #0000ff;\">const<\/span> Ch follower) {<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0AdditiveTerm result;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0result.size = 1;\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">\/\/ default size<\/span><\/li>\n<li><\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">switch<\/span> (firstRomanChar) {<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;I&#8217;<\/span>:<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">switch<\/span> (follower) {<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;V&#8217;<\/span>:<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">\/* I (1) subtracting to V (5) results in 4 *\/<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.value = 4;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">\/* a subtraction involves 2 symbols, so size is 2 *\/<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.size = 2;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">break<\/span>;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;X&#8217;<\/span>:<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.value = 9;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.size = 2;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">break<\/span>;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;L&#8217;<\/span>:<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;C&#8217;<\/span>:<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;D&#8217;<\/span>:<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;M&#8217;<\/span>:<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">\/* a subtractor can&#8217;t be less than a 10% of its follower<\/span><\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">(i.e.: 99, IC wrong; must be XCIX) *\/<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">throw<\/span> ConversionError(IllegalSubtraction);<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">default<\/span>:<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.value = 1;\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">\/* follower ignored *\/<\/span><\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0};<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">break<\/span>;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;V&#8217;<\/span>:<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #008000;\">\/* V, L and D aren&#8217;t subtractor in Roman numerals *\/<\/span><\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.value = 5;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">break<\/span>;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;X&#8217;<\/span>:<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">switch<\/span> (follower) {<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;L&#8217;<\/span>:<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.value = 40;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.size = 2;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">break<\/span>;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;C&#8217;<\/span>:<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.value = 90;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.size = 2;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">break<\/span>;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;D&#8217;<\/span>:<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;M&#8217;<\/span>:<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">throw<\/span> ConversionError(IllegalSubtraction);<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">default<\/span>:<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.value = 10;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0};<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">break<\/span>;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;L&#8217;<\/span>:<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.value = 50;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">break<\/span>;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;C&#8217;<\/span>:<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">switch<\/span> (follower) {<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;D&#8217;<\/span>:<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.size = 2;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.value = 400;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">break<\/span>;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;M&#8217;<\/span>:<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.size = 2;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.value = 900;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">break<\/span>;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">default<\/span>:<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.value = 100;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0};<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">break<\/span>;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;D&#8217;<\/span>:<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.value = 500;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">break<\/span>;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">case<\/span> L<span style=\"color: #a31515;\">&#8216;M&#8217;<\/span>:<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0result.value = 1000;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">break<\/span>;<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">default<\/span>:<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">throw<\/span> ConversionError(IllegalSymbol);<\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0}<\/li>\n<li><\/li>\n<li style=\"background: #f3f3f3;\">\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">return<\/span> result;<\/li>\n<li>}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p>At a first glance this function could look overwhelming, but the reader will immediately discover that it\u2019s logic is pretty basic and repetitive, despite all the possible cases.<\/p>\n<p>&nbsp;<\/p>\n<p>Before the interviewer, for completeness, it follows an analysis of the border conditions. For instance, what if the string received as argument is empty (equivalent to \u201c\u201d)? In our case, the function will return 0. It could have also thrown an exception as well, but I\u2019d rather preferred the former option.<\/p>\n<p>We should also test the cases evaluated initially, in order to confirm\/tune up the algorithm. I\u2019ve already done it, but I leave those to the reader as an exercise.<\/p>\n<p>From the algorithm itself, it follows a complexity analysis. In that sense, we could conclude that its order is linear to the length of the input. Or, in other words, its complexity is <em>O(n)<\/em>, <em>n<\/em> being the length of the input, which is pretty much acceptable. Complexities to be avoided whenever possible are quadratic ones \u2013<em>O(n<sup>2<\/sup>)<\/em>&#8211; or higher, not to mention exponential ones.<\/p>\n<p>A\u00a0possible implementation of this function, together with tons of test cases plus an inverse function itor(\u2026) \u2013which takes an integer and returns its roman equivalent- is offered <a href=\"http:\/\/cid-1ad5096d63670065.office.live.com\/self.aspx\/Visual%20Studio%20Projects\/Roman.zip\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Epilogue<\/strong><\/p>\n<p>There\u2019s a golden lesson to learn regarding programming interviews: do never do more than it was requested, as it may suggest a negative perception about you having difficulties to focus on what\u2019s necessary at a given time. To put an example, I could have solved this test by creating a class called RomanNumeral, with a constructor that receives an input string like the rtoi() function I just defined, so we can later add RomanNumeral objects by redefining the + operator, multiply, etc. as a brand new numeral type. Sounds awesome but\u2026 what\u2019s the point in doing so? How likely is, in the real world, that such kind of empowered data type is required? Admittedly, the problem posed in this case is hypothetical but, what\u2019s the reason in answering a hypothetical question by extending the hypothesis beyond its original boundaries?<\/p>\n<p>Bottom line: flying too high while solving a concrete, punctual requirement won\u2019t provoke any other effect than getting your interviewer scared about your abilities\u00a0to deliver a well-designed solution without falling in the temptation\u00a0of overarchitecting it unnecessarily, with the potential risk of missing\u00a0your chance to get the job.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A recent study showed that C++ is still among the most used languages (ahead of any other Visual Studio officially supported language like C# or Visual Basic). But despite being widely used and known by several generations of developers \u2013thanks that it has been out there for decades now, while being still the most taught [&hellip;]<\/p>\n","protected":false},"author":293,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[68,70],"class_list":["post-3593","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-design","tag-modeling"],"acf":[],"blog_post_summary":"<p>A recent study showed that C++ is still among the most used languages (ahead of any other Visual Studio officially supported language like C# or Visual Basic). But despite being widely used and known by several generations of developers \u2013thanks that it has been out there for decades now, while being still the most taught [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/3593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/293"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=3593"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/3593\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=3593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=3593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=3593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}