{"id":104,"date":"2019-06-02T06:46:21","date_gmt":"2019-06-02T06:46:21","guid":{"rendered":"https:\/\/www.369usa.com\/blog\/?p=104"},"modified":"2019-06-02T06:46:21","modified_gmt":"2019-06-02T06:46:21","slug":"javascript-tech-interview-question-add-2-big-integer-strings","status":"publish","type":"post","link":"https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/","title":{"rendered":"Javascript Tech Interview Question: Add 2 Big-Integer Strings"},"content":{"rendered":"<p>Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.<\/p>\n<p>Note:<\/p>\n<p>The length of both num1 and num2 is < 5100.\nBoth num1 and num2 contains only digits 0-9.\nBoth num1 and num2 does not contain any leading zero.\nYou must not use any built-in BigInteger library or convert the inputs to integer directly.\n\n\n\n<pre>\r\n\r\n\/**\r\n * @param {string} num1\r\n * @param {string} num2\r\n * @return {string}\r\n *\/\r\nvar addStrings = function(num1, num2) {\r\n    \r\n    \/\/Make sure num1 is the longer string \r\n    if(num1.length &lt; num2.length){\r\n        var t = num1;\r\n        num1 = num2;\r\n        num2 = t;\r\n    }\r\n    \r\n    \/\/Give a leading zero to make sure num1 is longer even if num1 and num2 have same length\r\n    num1 = &#8216;0&#8217; + num1;\r\n    num1 = num1.split(&quot;&quot;).reverse();\r\n    num2 = num2.split(&quot;&quot;).reverse();\r\n    \r\n    console.log(num1,num2)\r\n    \r\n    \r\n    for(var i=0;i&lt;num1.length;i++){\r\n        var n1 = parseInt(num1[i]);\r\n        var n2 = parseInt((typeof num2[i] == &#8216;undefined&#8217;)?0:num2[i]);\r\n        var t = n1 + n2;\r\n        num1[i] = t % 10;\r\n        if(t &gt;= 10){\r\n            num1[i+1] = parseInt(num1[i+1]) + 1;\r\n        }\r\n    }\r\n    \r\n    if(num1[num1.length-1] == 0){ num1.pop(); }\r\n    num1 = num1.reverse().join(&#8221;);\r\n    \r\n    return num1;\r\n    \r\n};\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero. You must not use any built-in BigInteger library or &hellip; <a href=\"https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Javascript Tech Interview Question: Add 2 Big-Integer Strings&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-104","post","type-post","status-publish","format-standard","hentry","category-6"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Javascript Tech Interview Question: Add 2 Big-Integer Strings - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Javascript Tech Interview Question: Add 2 Big-Integer Strings - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is &lt; 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero. You must not use any built-in BigInteger library or &hellip; Continue reading &quot;Javascript Tech Interview Question: Add 2 Big-Integer Strings&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-02T06:46:21+00:00\" \/>\n<meta name=\"author\" content=\"darkersss\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"darkersss\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/\",\"url\":\"https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/\",\"name\":\"Javascript Tech Interview Question: Add 2 Big-Integer Strings - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#website\"},\"datePublished\":\"2019-06-02T06:46:21+00:00\",\"dateModified\":\"2019-06-02T06:46:21+00:00\",\"author\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.369usa.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Javascript Tech Interview Question: Add 2 Big-Integer Strings\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#website\",\"url\":\"https:\/\/www.369usa.com\/blog\/\",\"name\":\"369\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.369usa.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6\",\"name\":\"darkersss\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g\",\"caption\":\"darkersss\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Javascript Tech Interview Question: Add 2 Big-Integer Strings - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/","og_locale":"en_US","og_type":"article","og_title":"Javascript Tech Interview Question: Add 2 Big-Integer Strings - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","og_description":"Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero. You must not use any built-in BigInteger library or &hellip; Continue reading \"Javascript Tech Interview Question: Add 2 Big-Integer Strings\"","og_url":"https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/","og_site_name":"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","article_published_time":"2019-06-02T06:46:21+00:00","author":"darkersss","twitter_card":"summary_large_image","twitter_misc":{"Written by":"darkersss","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/","url":"https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/","name":"Javascript Tech Interview Question: Add 2 Big-Integer Strings - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","isPartOf":{"@id":"https:\/\/www.369usa.com\/blog\/#website"},"datePublished":"2019-06-02T06:46:21+00:00","dateModified":"2019-06-02T06:46:21+00:00","author":{"@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6"},"breadcrumb":{"@id":"https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.369usa.com\/blog\/javascript-tech-interview-question-add-2-big-integer-strings\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.369usa.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Javascript Tech Interview Question: Add 2 Big-Integer Strings"}]},{"@type":"WebSite","@id":"https:\/\/www.369usa.com\/blog\/#website","url":"https:\/\/www.369usa.com\/blog\/","name":"369\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.369usa.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6","name":"darkersss","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g","caption":"darkersss"}}]}},"_links":{"self":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/104"}],"collection":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/comments?post=104"}],"version-history":[{"count":1,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"predecessor-version":[{"id":105,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/104\/revisions\/105"}],"wp:attachment":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}