{"id":680,"date":"2020-04-29T21:48:00","date_gmt":"2020-04-29T21:48:00","guid":{"rendered":"http:\/\/markedcode.com\/?p=680"},"modified":"2020-04-29T21:48:00","modified_gmt":"2020-04-29T21:48:00","slug":"length-of-last-word-coding-interview-question","status":"publish","type":"post","link":"https:\/\/markedcode.com\/index.php\/2020\/04\/29\/length-of-last-word-coding-interview-question\/","title":{"rendered":"Length of Last Word \u2013 Coding interview question"},"content":{"rendered":"<p>This Wednesday we will be looking at Length of Last Word. Another common interview question that shouldnt be too challenging. As always, I suggest you give it a try or following along at: <a href=\"https:\/\/leetcode.com\">https:\/\/leetcode.com<\/a><\/p>\n<h2><strong>Question<\/strong><\/h2>\n<p>&#8220;Given a string s consists of upper\/lower-case alphabets and empty space characters &#8216; &#8216;, return the length of last word (last word means the last appearing word if we loop from left to right) in the string.<br \/>\nIf the last word does not exist, return 0.<br \/>\nNote: A word is defined as a maximal substring consisting of non-space characters only.&#8221;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-683\" src=\"http:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Example-1.png\" alt=\"Length of Last Word Examples\" width=\"278\" height=\"107\" \/><\/p>\n<h2><strong>Code<\/strong><\/h2>\n<p>We will solve this problem in 3 steps. Step 1. Removing trailing white space on either side of the string. We can easily do this with the Java trim() function.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-684 size-medium\" src=\"http:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Trim-300x142.png\" alt=\"trim function\" width=\"300\" height=\"142\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Trim-300x142.png 300w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Trim.png 417w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>We need to remove white space for a few edge cases. For examples the string: &#8221; Hello world &#8220;. Would cause some issues for our algorithm which requires finding the last space prior to the last word in the string.<\/p>\n<p>Step 2. Finding the index of the last instance of space in the string. We will make use of another function here, lastIndexOf().<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-685\" src=\"http:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/LastIndex-300x123.png\" alt=\"Last index\" width=\"300\" height=\"123\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/LastIndex-300x123.png 300w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/LastIndex.png 498w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>After find the index we need to add one. This will put us at the start of the last word. We know we can&#8217;t go out of bound since we trimmed the white space.<\/p>\n<p>Step 3. Return the length whole string, minus the index of the last space found, plus one.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-686\" src=\"http:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/AllCode-1-300x123.png\" alt=\"All code\" width=\"300\" height=\"123\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/AllCode-1-300x123.png 300w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/AllCode-1.png 457w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>The difference between the lastIndex+1 and the end of the string will just be the length of the last word.<\/p>\n<p>For example: &#8221; Hello world &#8221; -&gt; calling trim -&gt; &#8220;Hello World&#8221; -&gt; finding index of last &#8221; &#8221; (5) + 1 = 6. -&gt; s.length (11) &#8211; 6 = 5! The length of &#8220;World&#8221;!<\/p>\n<h2><strong>Tests<\/strong><\/h2>\n<p>As always with LeetCode we can submit our solution to have it run for a variety of tests. Success! We passed!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-682 size-medium\" src=\"http:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Results-1-300x96.png\" alt=\"Length of Last Word results\" width=\"300\" height=\"96\" srcset=\"https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Results-1-300x96.png 300w, https:\/\/markedcode.com\/wp-content\/uploads\/2020\/04\/Results-1.png 541w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><iframe loading=\"lazy\" title=\"Coding Problem: Length of Last Word\" width=\"770\" height=\"433\" src=\"https:\/\/www.youtube.com\/embed\/WAZXQBbV8Us?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p>For more tech blogs, subscribe to the code_marks news letter: http:\/\/eepurl.com\/gZCMQz<\/p>\n<p><a href=\"https:\/\/markedcode.com\/index.php\/2020\/04\/17\/coding-interview-question-two-sums\/\">Check out the next interview\u00a0 question!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This Wednesday we will be looking at Length of Last Word. Another common interview question that shouldnt be too challenging. As always, I suggest you give it a try or following along at: https:\/\/leetcode.com Question &#8220;Given a string s consists of upper\/lower-case alphabets and empty space characters &#8216; &#8216;, return the length of last word<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8],"tags":[22],"class_list":{"0":"post-680","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-cpsc-interview-questions","7":"tag-length-of-last-word-coding-interview-question-leetcode-java-string-array-char-trim-index"},"_links":{"self":[{"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/posts\/680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/comments?post=680"}],"version-history":[{"count":1,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/posts\/680\/revisions"}],"predecessor-version":[{"id":687,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/posts\/680\/revisions\/687"}],"wp:attachment":[{"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/media?parent=680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/categories?post=680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/markedcode.com\/index.php\/wp-json\/wp\/v2\/tags?post=680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}