Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's my solution. For what it's worth, it took me about 20 minutes to make both the justify function and acceptance tests for it; 5 of those minutes debugging the justify function. Also, I used notpad++ instead of a more robust IDE to try to simulate to some extent the interview environment.</p> <p>I think this might be too large of a problem for a whiteboard interview question, unless the interviewer lets you write in pseudocode and is more interested in your thought process that what you are putting on the board.</p> <pre><code>&lt;?php function justify($str_in, $desired_length) { $words = preg_split("/ +/",$str_in); // handle special cases if(count($words)==0) { return str_repeat(" ",$desired_length); } // turn single word case into a normal case if(count($words)==1) { $words = array("",$words[0],""); } $numwords = count($words); $wordlength = strlen(join("",$words)); // handles cases where words are longer than the desired_length if($wordlength&gt;($desired_length-$numwords)) { return substr(join(" ",$words),0,$desired_length); } $minspace = floor(($desired_length-$wordlength)/($numwords-1)); $extraspace = $desired_length - $wordlength - ($minspace * ($numwords-1)); $result = $words[0]; for($i=1;$i&lt;$numwords;$i++) { if($extraspace&gt;0) { $result.=" "; $extraspace--; } $result.=str_repeat(" ",$minspace); $result.=$words[$i]; } return $result; } function acceptance_justify($orig_str, $just_str, $expected_length) { // should be the correct length if(strlen($just_str)!=$expected_length) { return false; } // should contain most of the words in the original string, in the right order if(preg_replace("/ +/","",substr($orig_str,0,$expected_length)) != preg_replace("/ +/","",substr($just_str,0,$expected_length))) { return false; } //spacing should be uniform (+/- 1 space) if(!preg_match("/( +)/",$just_str,$spaces)) { return false; } $space_length=strlen($spaces[0]); $smin=$space_length; $smax=$space_length; for($i=1;$i&lt;count(@spaces);$i++) { $smin=min($smin,strlen($spaces)); $smax=max($smax,strlen($spaces)); } if(($smax-$smin)&gt;1) { return false; } return true; } function run_test($str,$len) { print "&lt;pre&gt;"; print "$str ==&gt; \n"; $result = justify($str,$len); print preg_replace("/ /",".",$result) . "\n"; print acceptance_justify($str,$result,$len)?"passed":"FAILED"; print "\n\n&lt;/pre&gt;"; } run_test("hello world there ok then",48); run_test("hello",48); run_test("this string is almost certainly longer than 48 I think",48); run_test("two words",48); run_test("three ok words",48); run_test("1 2 3 4 5 6 7 8 9",48); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload