{"id":798,"date":"2017-03-28T19:08:35","date_gmt":"2017-03-29T03:08:35","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=798"},"modified":"2023-02-21T16:20:42","modified_gmt":"2023-02-22T00:20:42","slug":"yahoo-phone-interview","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=798","title":{"rendered":"Yahoo Phone Interview"},"content":{"rendered":"<p>1) Write a function to accept seconds and print time in format hh:mm:ss<br \/>\n2) If the same function is to be used multiple times how would we optimize the code so that we don&#8217;t have to calculate it each time. (hint: use static)<\/p>\n<pre> \r\nfunction formatTime($seconds) {\r\n    if !($seconds) {\r\n        return \"\";\r\n    }\r\n    static $timeArray = array();\r\n    if (isset($timeArray[$seconds])) {\r\n        echo \"using cache\\n\";\r\n        return $timeArray[$seconds];\r\n    }\r\n    $origSeconds = $seconds;\r\n    $hours = floor($seconds \/ 3600);\r\n    $seconds = $seconds % 3600;\r\n    $mins = floor($seconds \/ 60);\r\n    $seconds = $seconds % 60;\r\n    $ret = sprintf(\"%02d::%02d::%02d\", $hours, $mins, $seconds);\r\n    $timeArray[$origSeconds] = $ret;\r\n    return ($ret);\r\n}\r\n<\/pre>\n<p>Another example of memoizing a function:<\/p>\n<pre class=\"lang-php s-code-block\"><code class=\"hljs language-php\"><span class=\"hljs-variable\">$memoize<\/span> = <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><span class=\"hljs-variable\">$func<\/span><\/span>)\r\n<\/span>{\r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>() <span class=\"hljs-keyword\">use<\/span> (<span class=\"hljs-params\"><span class=\"hljs-variable\">$func<\/span><\/span>)\r\n    <\/span>{\r\n        <span class=\"hljs-built_in\">static<\/span> <span class=\"hljs-variable\">$cache<\/span> = [];\r\n\r\n        <span class=\"hljs-variable\">$args<\/span> = <span class=\"hljs-title function_ invoke__\">func_get_args<\/span>();\r\n\r\n        <span class=\"hljs-variable\">$key<\/span> = <span class=\"hljs-title function_ invoke__\">md5<\/span>(<span class=\"hljs-title function_ invoke__\">serialize<\/span>(<span class=\"hljs-variable\">$args<\/span>));\r\n\r\n        <span class=\"hljs-keyword\">if<\/span> ( ! <span class=\"hljs-keyword\">isset<\/span>(<span class=\"hljs-variable\">$cache<\/span>[<span class=\"hljs-variable\">$key<\/span>])) {\r\n            <span class=\"hljs-variable\">$cache<\/span>[<span class=\"hljs-variable\">$key<\/span>] = <span class=\"hljs-title function_ invoke__\">call_user_func_array<\/span>(<span class=\"hljs-variable\">$func<\/span>, <span class=\"hljs-variable\">$args<\/span>);\r\n        }\r\n\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-variable\">$cache<\/span>[<span class=\"hljs-variable\">$key<\/span>];\r\n    };\r\n};\r\n\r\n<span class=\"hljs-variable\">$fibonacci<\/span> = <span class=\"hljs-variable\">$memoize<\/span>(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><span class=\"hljs-variable\">$n<\/span><\/span>) <span class=\"hljs-keyword\">use<\/span> (<span class=\"hljs-params\">&amp;<span class=\"hljs-variable\">$fibonacci<\/span><\/span>)\r\n<\/span>{\r\n    <span class=\"hljs-keyword\">return<\/span> (<span class=\"hljs-variable\">$n<\/span> &lt; <span class=\"hljs-number\">2<\/span>) ? <span class=\"hljs-variable\">$n<\/span> : <span class=\"hljs-variable\">$fibonacci<\/span>(<span class=\"hljs-variable\">$n<\/span> - <span class=\"hljs-number\">1<\/span>) + <span class=\"hljs-variable\">$fibonacci<\/span>(<span class=\"hljs-variable\">$n<\/span> - <span class=\"hljs-number\">2<\/span>);\r\n});\r\n<\/code><\/pre>\n<p>3) Can cookies be stolen ?<br \/>\nYes cookies can be stolen. XSS is one way to do it.<br \/>\n<a href=\"https:\/\/www.go4expert.com\/articles\/stealing-cookie-xss-t17066\/\">https:\/\/www.go4expert.com\/articles\/stealing-cookie-xss-t17066\/<\/a><\/p>\n<p>4) Interface vs Abstract class<br \/>\n5) When does the page on screen get refreshed ?<br \/>\nRepainting and Reflowing<br \/>\n<a href=\"http:\/\/frontendbabel.info\/articles\/webpage-rendering-101\/\">http:\/\/frontendbabel.info\/articles\/webpage-rendering-101\/<\/a><br \/>\n<a href=\"http:\/\/www.phpied.com\/rendering-repaint-reflowrelayout-restyle\/\">http:\/\/www.phpied.com\/rendering-repaint-reflowrelayout-restyle\/<\/a><br \/>\n<a href=\"http:\/\/taligarsiel.com\/Projects\/howbrowserswork1.htm\">http:\/\/taligarsiel.com\/Projects\/howbrowserswork1.htm<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1) Write a function to accept seconds and print time in format hh:mm:ss 2) If the same function is to be used multiple times how would we optimize the code so that we don&#8217;t have to calculate it each time. (hint: use static) function formatTime($seconds) { if !($seconds) { return &#8220;&#8221;; } static $timeArray = &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=798\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Yahoo Phone Interview<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-798","post","type-post","status-publish","format-standard","hentry","category-interview-questions"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=798"}],"version-history":[{"count":6,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/798\/revisions"}],"predecessor-version":[{"id":1079,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/798\/revisions\/1079"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=798"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}