<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Alex 👨🏼‍💻FullStack.Cafe</title>
    <description>The latest articles on Forem by Alex 👨🏼‍💻FullStack.Cafe (@aershov24).</description>
    <link>https://forem.com/aershov24</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F84431%2F873fcec3-9abd-4f12-9bec-cc0f04b35fe0.jpeg</url>
      <title>Forem: Alex 👨🏼‍💻FullStack.Cafe</title>
      <link>https://forem.com/aershov24</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aershov24"/>
    <language>en</language>
    <item>
      <title>🔴👨‍💻 FullStack.Café - 🪓 Kill Your Tech &amp; Coding Interview + BLKFRD 50% OFF DEAL</title>
      <dc:creator>Alex 👨🏼‍💻FullStack.Cafe</dc:creator>
      <pubDate>Sun, 22 Nov 2020 06:41:41 +0000</pubDate>
      <link>https://forem.com/aershov24/fullstack-cafe-kill-your-tech-coding-interview-blkfrd-50-off-deal-mpi</link>
      <guid>https://forem.com/aershov24/fullstack-cafe-kill-your-tech-coding-interview-blkfrd-50-off-deal-mpi</guid>
      <description>&lt;p&gt;Hi, I'm Alex, developer of FullStack.Cafe aka FSC.&lt;br&gt;&lt;br&gt; &lt;/p&gt;

&lt;p&gt;Listen, I've just deployed the biggest release of FSC ever. That's what I've done:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;New &lt;b&gt;Coding and Data Structures Questions&lt;/b&gt; section - LL, DLL, Big-O, Graphs, Trees, Trie, DP and other shit you still don't know&lt;/li&gt;
  &lt;li&gt;New &lt;b&gt;System Design and Architecture Interview Questions&lt;/b&gt; section - CAP Theorem, DDD, Caching, Concurrency, NoSQL and etc.&lt;/li&gt;
  &lt;li&gt;Improved, updated and refined &lt;b&gt;Full-Stack Interview Questions&lt;/b&gt; section - including React, Angular, Vue, JS , .NET + 36 other topics&lt;/li&gt;
  &lt;li&gt;Plus codebase has been completely rewritten from Angular to React (-56% bundle size, 98/100 Google Page speed score) so that's kinda cool&lt;/li&gt;
&lt;/ul&gt;
&lt;br&gt;

&lt;b&gt;Why Bother?&lt;/b&gt;&lt;br&gt;&lt;br&gt;

Look, I hate Black Effing Friday shit but in case you got laid off due to COVID, despise your boss or already looking for a Dev Job, &lt;b&gt;"BLKFRD"&lt;/b&gt; Promocode gives you &lt;b&gt;50% OFF&lt;/b&gt; for Instant FSC Lifetime Access. Just apply &lt;b&gt;"BLKFRD"&lt;/b&gt; promo on checkout or use that link:&lt;br&gt;&lt;br&gt;

&lt;span&gt;&lt;a href="https://www.fullstack.cafe/?promocode=BLKFRD&amp;amp;utm_source=dev&amp;amp;utm_medium=post" rel="noopener"&gt;&amp;gt;&amp;gt; Get Up To 50% OFF - Apply BLKFRD Promo&lt;/a&gt;&lt;/span&gt;&lt;br&gt;&lt;br&gt;

Skeptical? I would be too... but for a 0.00001% of your future 6-figure salary you get:
&lt;ul&gt;
  &lt;li&gt;Instant Unlimited Access to more than 3800 Answers INCLUDING new Coding and System Design sections&lt;/li&gt;
  &lt;li&gt;All Updates Forever done manually by me (I did quit my job in September so FSC is now my full-time business)&lt;/li&gt;
  &lt;li&gt;Save yourself tons of time for interview prep (aka stop Googling Around and do real prep)&lt;/li&gt;
  &lt;li&gt;Outperform 95% Devs on tech screening (sorry slackers)&lt;/li&gt;
  &lt;li&gt;Staying employed in 2020-2021 (if you can...)&lt;/li&gt;
  &lt;li&gt;Get FREE Job-Winning A/B Tested Full-Stack Resume Template I did design and was using for years here in Australia&lt;/li&gt;
  &lt;li&gt;Make heap of money doing what you love most (amen)&lt;/li&gt;
  &lt;li&gt;Need more real reviews? Check FSC on &lt;a href="https://au.trustpilot.com/review/fullstack.cafe"&gt;Trustpilot (3.8/5)&lt;/a&gt; and &lt;a href="https://github.com/aershov24/full-stack-interview-questions"&gt;GitHub (176 ⭐)&lt;/a&gt;. It worked for others (±2000 Devs worldwide) and will work for you.&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;&lt;b&gt;P.S.&lt;/b&gt;&lt;br&gt; &lt;a href="https://www.fullstack.cafe/?promocode=BLKFRD&amp;amp;utm_source=dev&amp;amp;utm_medium=post" rel="noopener"&gt;BLKFRD&lt;/a&gt; won't last long. I rise prices soon cause of time invested to FSC. Demand is huge. People competing for dev jobs as never before (due COVID).&lt;br&gt;&lt;br&gt;
Good luck &amp;amp; stay safe 🙏 Only you can do it.&lt;br&gt;&lt;br&gt;&lt;/p&gt;

</description>
      <category>career</category>
    </item>
    <item>
      <title>12 Common Dynamic Programming Interview Questions and Problems (SOLVED)</title>
      <dc:creator>Alex 👨🏼‍💻FullStack.Cafe</dc:creator>
      <pubDate>Fri, 23 Oct 2020 07:53:15 +0000</pubDate>
      <link>https://forem.com/aershov24/12-common-dynamic-programming-interview-questions-and-problems-solved-31mk</link>
      <guid>https://forem.com/aershov24/12-common-dynamic-programming-interview-questions-and-problems-solved-31mk</guid>
      <description>&lt;p&gt;Dynamic programming is all about ordering your computations in a way that avoids recalculating duplicate work. More specifically, Dynamic Programming is a technique used to avoid computing multiple times the same subproblem in a recursive algorithm. DP algorithms could be implemented with recursion, but they don't have to be. Follow along and learn 12 Most Common Dynamic Programming Interview Questions and Answers to nail your next coding interview.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🔴 Originally published on &lt;a href="https://www.fullstack.cafe/blog/dynamic-programming-interview-questions?utm_source=dev&amp;amp;utm_medium=blog"&gt;FullStack.Cafe - Kill Your Next Tech Interview&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q1:&lt;/em&gt; What is Dynamic Programming? ☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Dynamic Programming&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Dynamic programming&lt;/strong&gt; is all about ordering your computations in a way that avoids recalculating duplicate work. More specifically, Dynamic Programming is a technique used to avoid computing multiple times the same &lt;em&gt;subproblem&lt;/em&gt; in a recursive algorithm. DP algorithms could be implemented with recursion, but they don't have to be.&lt;/p&gt;

&lt;p&gt;With dynamic programming, you store your results in some sort of table generally. When you need the answer to a problem, you reference the table and see if you already know what it is. If not, you use the data in your table to give yourself a stepping stone towards the answer.&lt;/p&gt;

&lt;p&gt;There are two approaches to apply Dynamic Programming:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;top-down or memoization&lt;/strong&gt;. When the recursion does a lot of unecessary calculation, an easy way to solve this is to cache the results and to check before executing the call if the result is already in the cache.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;bottom-up or tabulation approach&lt;/strong&gt;. A better way to do this is to get rid of the recursion all-together by evaluating the results in the right order and building the array as we iterate. The partial results are available when needed if the iteration is done in the right order.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;TOP&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;tree&lt;/span&gt;
&lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)......................&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;).........&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)...........&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;              &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;BOTTOM&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;tree&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/1065433/what-is-dynamic-programming"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q2:&lt;/em&gt; How Dynamic Programming is different from Recursion and Memoization? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Dynamic Programming Recursion&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memoization&lt;/strong&gt; is when you store previous results of a function call (a real function always returns the same thing, given the same inputs). It doesn't make a difference for algorithmic complexity before the results are stored.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recursion&lt;/strong&gt; is the method of a function calling itself, usually with a smaller dataset. Since most recursive functions can be converted to similar iterative functions, this doesn't make a difference for algorithmic complexity either.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic programming&lt;/strong&gt; is the process of solving easier-to-solve sub-problems and building up the answer from that. Most DP algorithms will be in the running times between a Greedy algorithm (if one exists) and an exponential (enumerate all possibilities and find the best one) algorithm.

&lt;ul&gt;
&lt;li&gt;DP algorithms could be implemented with recursion, but they don't have to be.&lt;/li&gt;
&lt;li&gt;DP algorithms can't be sped up by memoization, since each sub-problem is only ever solved (or the "solve" function called) once.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/1065433/what-is-dynamic-programming"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q3:&lt;/em&gt; What are pros and cons of Memoization or Top-Down approach? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Dynamic Programming&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Memoization is very easy to code (you can generally* write a "memoizer" annotation or wrapper function that automatically does it for you), and should be your first line of approach. It feels more natural. You can take a recursive function and memoize it by a mechanical process (first lookup answer in cache and return it if possible, otherwise compute it recursively and then before returning, you save the calculation in the cache for future use), whereas doing bottom up dynamic programming requires you to encode an order in which solutions are calculated.&lt;/li&gt;
&lt;li&gt;Top-down only solves sub-problems used by your solution whereas bottom-up might waste time on redundant sub-problems.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With memoization, if the tree is very deep (e.g. &lt;code&gt;fib(10&lt;sup&gt;6&lt;/sup&gt;)&lt;/code&gt;), you will run out of stack space, because each delayed computation must be put on the stack, and you will have &lt;code&gt;10&lt;sup&gt;6&lt;/sup&gt;&lt;/code&gt; of them.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/6164629/what-is-the-difference-between-bottom-up-and-top-down"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q4:&lt;/em&gt; What are some characteristics of Dynamic Programming?  ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Dynamic Programming&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;The key idea of DP is to save answers of overlapping smaller sub-problems to avoid recomputation. For that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An instance is solved using the solutions for smaller instances.&lt;/li&gt;
&lt;li&gt;The solutions for a smaller instance might be needed multiple times, so store their results in a table.&lt;/li&gt;
&lt;li&gt;Thus each smaller instance is solved only once.&lt;/li&gt;
&lt;li&gt;Additional space is used to save time.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/1065433/what-is-dynamic-programming"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q5:&lt;/em&gt; LIS: Find length of the longest increasing subsequence (LIS) in the array. Solve using DP. ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Dynamic Programming&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;longest increasing subsequence problem&lt;/strong&gt; is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Consider:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the first 16 terms of the binary Van der Corput sequence&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;a longest increasing subsequence is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This subsequence has length six; &lt;br&gt;
the input sequence has no seven-member increasing subsequences. &lt;br&gt;
The longest increasing subsequence in this example is not unique: for &lt;br&gt;
instance,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="nx"&gt;or&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="nx"&gt;or&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;are other increasing subsequences of equal length in the same &lt;br&gt;
input sequence.&lt;/p&gt;
&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Let's assume the indices of the array are from 0 to N - 1.&lt;/li&gt;
&lt;li&gt;Let's define &lt;code&gt;DP[i]&lt;/code&gt; to be the length of the LIS (Longest increasing subsequence) which is ending at element with index &lt;code&gt;i&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;DP&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// length of LIS for the first element is always 1&lt;/span&gt;
&lt;span class="nx"&gt;int&lt;/span&gt; &lt;span class="nx"&gt;maxLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;To compute &lt;code&gt;DP[i]&lt;/code&gt; for each &lt;code&gt;i &amp;gt; 0&lt;/code&gt; we look at all indices &lt;code&gt;j &amp;lt; i&lt;/code&gt; and check both: 

&lt;ul&gt;
&lt;li&gt;if &lt;code&gt;DP[j] + 1 &amp;gt; DP[i]&lt;/code&gt; and &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;array[j] &amp;lt; array[i]&lt;/code&gt; (we want it to be increasing). &lt;/li&gt;
&lt;li&gt;If this is &lt;code&gt;true&lt;/code&gt; we can update the current &lt;em&gt;optimum&lt;/em&gt; for &lt;code&gt;DP[i]&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;int&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;N&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;DP&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
       &lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

       &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;int&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;DP&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;DP&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt;
             &lt;span class="nx"&gt;DP&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;DP&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
             &lt;span class="nx"&gt;prev&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;

       &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;DP&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;maxLength&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
       &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;bestEnd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="nx"&gt;maxLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;DP&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;To find the global &lt;em&gt;optimum&lt;/em&gt; for the array you can take the maximum value from DP &lt;code&gt;[0...N - 1]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Use the array &lt;code&gt;prev&lt;/code&gt; to be able later to find the actual sequence not only its length. Just go back recursively from &lt;code&gt;bestEnd&lt;/code&gt; in a loop using &lt;code&gt;prev[bestEnd]&lt;/code&gt;. The &lt;code&gt;-1&lt;/code&gt; value is a sign to stop.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Complexity Analysis:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(n^2)&lt;br&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: O(n^2)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Time complexity : &lt;code&gt;O(n^2)&lt;/code&gt;. Two loops of &lt;code&gt;n&lt;/code&gt; are there.&lt;/li&gt;
&lt;li&gt;Space complexity : &lt;code&gt;O(n)&lt;/code&gt;. DP array of size &lt;code&gt;n&lt;/code&gt; is used.
### Implementation:
#### &lt;em&gt;JS&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Dynamic programming approach to find longest increasing subsequence.
 * Complexity: O(n * n)
 *
 * @param {number[]} sequence
 * @return {number}
 */&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;dpLongestIncreasingSubsequence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sequence&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Create array with longest increasing substrings length and&lt;/span&gt;
  &lt;span class="c1"&gt;// fill it with 1-s that would mean that each element of the sequence&lt;/span&gt;
  &lt;span class="c1"&gt;// is itself a minimum increasing subsequence.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;lengthsArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sequence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;previousElementIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentElementIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentElementIndex&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;sequence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sequence&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;previousElementIndex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;sequence&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentElementIndex&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// If current element is bigger then the previous one then&lt;/span&gt;
      &lt;span class="c1"&gt;// current element is a part of increasing subsequence which&lt;/span&gt;
      &lt;span class="c1"&gt;// length is by one bigger then the length of increasing subsequence&lt;/span&gt;
      &lt;span class="c1"&gt;// for previous element.&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;lengthsArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;previousElementIndex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newLength&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;lengthsArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentElementIndex&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Increase only if previous element would give us bigger subsequence length&lt;/span&gt;
        &lt;span class="c1"&gt;// then we already have for current element.&lt;/span&gt;
        &lt;span class="nx"&gt;lengthsArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentElementIndex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newLength&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Move previous element index right.&lt;/span&gt;
    &lt;span class="nx"&gt;previousElementIndex&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// If previous element index equals to current element index then&lt;/span&gt;
    &lt;span class="c1"&gt;// shift current element right and reset previous element index to zero.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;previousElementIndex&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;currentElementIndex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;currentElementIndex&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;previousElementIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Find the biggest element in lengthsArray.&lt;/span&gt;
  &lt;span class="c1"&gt;// This number is the biggest length of increasing subsequence.&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;longestIncreasingLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;lengthsArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lengthsArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;longestIncreasingLength&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;longestIncreasingLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;lengthsArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;longestIncreasingLength&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;longestIncreasingSubsequence&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;PY&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;Traditional DP solution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Time:  O(n^2)
# Space: O(n)
# Traditional DP solution.
&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SolutionDP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lengthOfLIS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="s"&gt;"""
        :type nums: List[int]
        :rtype: int
        """&lt;/span&gt;
        &lt;span class="n"&gt;dp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;  &lt;span class="c1"&gt;# dp[i]: the length of LIS ends with nums[i]
&lt;/span&gt;        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;xrange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
            &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;xrange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
                    &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or in 5 lines of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;L&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;L&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;L&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="p"&gt;[[]],&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;L&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;inp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"List of integers: "&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inp&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/2631726/how-to-determine-the-longest-increasing-subsequence-using-dynamic-programming/2631810#2631810"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q6:&lt;/em&gt; Provide an example of Dynamic Program but without Recursion ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Dynamic Programming&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;The following would be considered DP, but without recursion (using &lt;strong&gt;bottom-up&lt;/strong&gt; or &lt;strong&gt;tabulation&lt;/strong&gt; DP approach).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;fibresult&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setup_fib&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fibresult&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;fibresult&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt;
       &lt;span class="n"&gt;fibresult&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fibresult&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fibresult&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;fib&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fibresult&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way may be described as "eager", "precaching" or "iterative". Its faster overall but we have to manually figure out the order the subproblems need to be calculated in. This is easy for fibonacci, but for more complex DP problems it gets harder, and so we fall back to the lazy recursive method if it is fast enough.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/12133754/whats-the-difference-between-recursion-memoization-dynamic-programming"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q7:&lt;/em&gt; What are some pros and cons of Tabulation or Bottom-Up approach? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Dynamic Programming&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you are doing an extremely complicated problems, you might have no choice but to do tabulation (or at least take a more active role in steering the memoization where you want it to go). Also if you are in a situation where optimization is absolutely critical and you must optimize, tabulation will allow you to do optimizations which memoization would not otherwise let you do in a sane way.

&lt;ul&gt;
&lt;li&gt;Why? Because with memoization, if the tree is very deep (e.g. fib(10^6)), you will run out of stack space, because each delayed computation must be put on the stack, and you will have 10^6 of them.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;In many applications the bottom-up approach is slightly faster because of the overhead of recursive calls.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The downside of tabulation is that you have to come up with an ordering. You must pick, ahead of time, the exact order in which you will do your computations. &lt;/li&gt;
&lt;li&gt;Top-down only solves sub-problems used by your solution whereas bottom-up might waste time on redundant sub-problems.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/6164629/what-is-the-difference-between-bottom-up-and-top-down"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q8:&lt;/em&gt; What is the difference between Divide and Conquer and Dynamic Programming Algorithms? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Divide &amp;amp; Conquer Dynamic Programming&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Dynamic programming&lt;/strong&gt; is an extension of &lt;strong&gt;Divide and Conquer&lt;/strong&gt; paradigm. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They both work by recursively breaking down a problem into two or more sub-problems. The solutions to the sub-problems are then combined to give a solution to the original problem.&lt;/li&gt;
&lt;li&gt;In Divide and conquer the sub-problems are &lt;em&gt;independent&lt;/em&gt; of each other. Mergesort is a classic example of divide and conquer. The main difference between this example and the Fibonacci example is that in a mergesort, the division can (theoretically) be arbitrary, and no matter how you slice it up, you are still merging and sorting.&lt;/li&gt;
&lt;li&gt;In dynamic programming the sub-problem are not independent. So to calculate new Fib number you have to know two previous values. For Merge sort you don't need to know the sorting order of previously sorted sub-array to sort another one.&lt;/li&gt;
&lt;li&gt;Dynamic programming approach may be applied to the problem only if the problem has certain restrictions or prerequisites:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Optimal substructure&lt;/strong&gt; — optimal solution can be constructed from optimal solutions of its subproblems&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overlapping sub-problems&lt;/strong&gt; — problem can be broken down into subproblems which are &lt;em&gt;reused several times&lt;/em&gt; or a recursive algorithm for the problem solves the same subproblem over and over rather than always generating new subproblems.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Dynamic programming approach extends divide and conquer approach with two techniques:

&lt;ul&gt;
&lt;li&gt;memoization (top-down cache filling)&lt;/li&gt;
&lt;li&gt; tabulation (bottom-up cache filling)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MF04j2Qq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ABwuDAdImyK_nZpb-H8h3SA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MF04j2Qq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ABwuDAdImyK_nZpb-H8h3SA.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/13538459/difference-between-divide-and-conquer-algo-and-dynamic-programming"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q9:&lt;/em&gt; What should you consider when choosing between Top-Down vs Bottom-Up solutions for the same problem? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Dynamic Programming&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;Two things to consider when deciding which algorithm to use&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity.&lt;/strong&gt; Both approaches have the same time complexity in general, but because for loop is cheaper than recursive function calls, bottom-up can be faster if measured in &lt;strong&gt;machine time&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;. (without considering extra call stack allocations during top-down) Usually both approaches need to build a table for all sub-solutions, but bottom-up is following a topological order, its cost of auxiliary space can be sometimes reduced to the size of problem's immediate dependencies. For example: &lt;code&gt;fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)&lt;/code&gt;, we only need to store the past two calculations&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That being said, bottom-up is not always the best choice, I will try to illustrate with examples:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Top-down only solves sub-problems used by your solution whereas bottom-up might waste time on redundant sub-problems. A silly example would be 0-1 knapsack with 1 item...run time difference is &lt;code&gt;O(1)&lt;/code&gt; vs &lt;code&gt;O(weight)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;you might need to perform extra work to get topological order for bottm-up. In Longest Increasing Path in Matrix if we want to do sub-problems after their dependencies, we would have to sort all entries of the matrix in descending order, that's extra &lt;code&gt;nmlog(nm)&lt;/code&gt; pre-processing time before DP&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/13731612/dynamic-programming-top-down-versus-bottom-up-comparison/47669816#47669816"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q10:&lt;/em&gt; Compare Greedy vs Divide &amp;amp; Conquer vs Dynamic Programming Algorithms ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Divide &amp;amp; Conquer Dynamic Programming Greedy Algorithms&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;Consider:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Greedy&lt;/th&gt;
&lt;th&gt;Divide &amp;amp; Conquer&lt;/th&gt;
&lt;th&gt;Dynamic Programming&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Optimises by making the best choice at the moment&lt;/td&gt;
&lt;td&gt;Optimises by breaking down a subproblem into simpler versions of itself and using multi-threading &amp;amp; recursion to solve&lt;/td&gt;
&lt;td&gt;Same as Divide and Conquer, but optimises by caching the answers to each subproblem as not to repeat the calculation twice.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Doesn't always find the optimal solution, but is very fast&lt;/td&gt;
&lt;td&gt;Always finds the optimal solution, but is slower than Greedy&lt;/td&gt;
&lt;td&gt;Always finds the optimal solution, but could be pointless on small datasets.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Requires almost no memory&lt;/td&gt;
&lt;td&gt;Requires some memory to remember recursive calls&lt;/td&gt;
&lt;td&gt;Requires a lot of memory for memoisation / tabulation&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://skerritt.blog/greedy-algorithms/"&gt;skerritt.blog&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q11:&lt;/em&gt; How to use Memoization for N-th Fibonacci number?  ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Dynamic Programming Fibonacci Series Recursion&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;Why? Any problems you may face with that solution?&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;Yes. It's called &lt;strong&gt;Memoization&lt;/strong&gt;. &lt;strong&gt;Memoization&lt;/strong&gt; is an optimization technique used primarily to speed up computer programs by &lt;em&gt;storing the results of expensive function calls&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Let’s look at the diagram that will help you understand what’s going on here with the rest of our code. Function fib is called with argument 5. Can you see that we calculate the &lt;code&gt;fib(2)&lt;/code&gt; results 3(!) times?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AYFlGGFS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ik.imagekit.io/jclivyrqwx/85218649-ba646d00-b3ce-11ea-81a9-f8fccf10f155_yEHz3Zx4I.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AYFlGGFS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ik.imagekit.io/jclivyrqwx/85218649-ba646d00-b3ce-11ea-81a9-f8fccf10f155_yEHz3Zx4I.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically, if we just store the value of each index in a &lt;strong&gt;hash&lt;/strong&gt;, we will avoid the computational time of that value for the next &lt;code&gt;N&lt;/code&gt; times. This change will increase the space complexity of our new algorithm to &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt; but will dramatically decrease the time complexity to &lt;code&gt;2N&lt;/code&gt; which will resolve to linear time since 2 is a constant &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There’s just &lt;strong&gt;one problem&lt;/strong&gt;: With an infinite series, the memo array will have unbounded growth. Eventually, you’re going to run into heap size limits, and that will crash the JS engine. No worries though. With Fibonacci, you’ll run into the maximum exact JavaScript integer size first, which is &lt;code&gt;9007199254740991&lt;/code&gt;. That’s over 9 quadrillion, which is a big number, but Fibonacci isn’t impressed. Fibonacci grows &lt;strong&gt;fast&lt;/strong&gt;. You’ll burst that barrier after generating only 79 numbers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity Analysis:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(n)&lt;br&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: O(n)&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;JS&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;memo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;fibMemo&lt;/span&gt;&lt;span class="o"&gt;[];&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;fibByRecMemo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fibMemo&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fibMemo&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fibMemo&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fibMemo&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fibByRecMemo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fibByRecMemo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fibMemo&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fibMemo&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://medium.com/developers-writing/fibonacci-sequence-algorithm-in-javascript-b253dc7e320e"&gt;medium.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q12:&lt;/em&gt; Is Dijkstra's algorithm a Greedy or Dynamic Programming algorithm? ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Greedy Algorithms Dynamic Programming&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;Both. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's greedy because you always mark the &lt;strong&gt;closest&lt;/strong&gt; vertex. &lt;/li&gt;
&lt;li&gt;It's dynamic because distances are updated using &lt;strong&gt;previously&lt;/strong&gt; calculated values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But would say it's definitely &lt;strong&gt;closer to dynamic programming&lt;/strong&gt; than to a greedy algorithm. To find the shortest distance from A to B, it does not decide which way to go step by step. Instead, it finds all places that one can go from A, and marks the distance to the nearest place. Marking that place, however, does not mean you'll go there. It only means that distance can no longer be made shorter assuming all edges of the graph are positive. The algorithm itself does not have a good sense of direction as to which way will get you to place B faster. The optimal decisions are not made greedily, but are made by exhausting all possible routes that can make a distance shorter. Therefore, it's a dynamic programming algorithm, the only variation being that the stages are not known in advance, but are dynamically determined during the course of the algorithm. You can call it a "dynamic" dynamic programming algorithm, if you like, to tell it apart from other dynamic programming algorithms with predetermined stages of decision making to go through&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/14038011/dijkstras-algorithm-a-greedy-or-dynamic-programming-algorithm"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Thanks 🙌 for reading and good luck on your interview!&lt;/em&gt; &lt;em&gt;Please share this article with your fellow Devs if you like it!&lt;/em&gt; &lt;em&gt;Check more FullStack Interview Questions &amp;amp; Answers on 👉 &lt;a href="https://www.fullstack.cafe"&gt;www.fullstack.cafe&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>dynamicprogramming</category>
      <category>career</category>
      <category>interview</category>
    </item>
    <item>
      <title>25 NoSQL Interview Questions (ANSWERED) You Must Know In 2020</title>
      <dc:creator>Alex 👨🏼‍💻FullStack.Cafe</dc:creator>
      <pubDate>Wed, 14 Oct 2020 05:32:52 +0000</pubDate>
      <link>https://forem.com/fullstackcafe/25-nosql-interview-questions-answered-you-must-know-in-2020-1kh9</link>
      <guid>https://forem.com/fullstackcafe/25-nosql-interview-questions-answered-you-must-know-in-2020-1kh9</guid>
      <description>&lt;p&gt;Paradoxically the main reason behind the popularity of NoSQL data stores is the fact that their lack of ability to do advanced queries (joins, groupings, ranking and analytics) that allows these data stores to be scaled much, much easier than any RDBMS, which is a very valuable feature in todays world of massively distributed systems. Follow along and refresh your knowledge about 25 top most advanced NoSQL Interview Questions and Answers you should learn for your next developer interview in 2020.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🔴 Originally published on &lt;a href="https://www.fullstack.cafe/blog/nosql-interview-questions"&gt;FullStack.Cafe - Kill Your Next Tech Interview&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q1:&lt;/em&gt; What are NoSQL databases? What are the different types of NoSQL databases? ☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; NoSQL&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;A NoSQL database provides a mechanism for storage and retrieval of data that is modeled in means other than the tabular relations used in relational databases (like SQL, Oracle, etc.).&lt;/p&gt;

&lt;p&gt;Types of NoSQL databases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Document Oriented&lt;/li&gt;
&lt;li&gt;Key Value&lt;/li&gt;
&lt;li&gt;Graph&lt;/li&gt;
&lt;li&gt;Column Oriented&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://interviewbubble.com/advance-topics/mongodb-interview-questions-and-answers/"&gt;interviewbubble.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q2:&lt;/em&gt; What do you understand by NoSQL databases? Explain. ☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; NoSQL&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;At the present time, the internet is loaded with big data, big users, big complexity etc. and also becoming more complex day by day. NoSQL is answer of all these problems; It is not a traditional database management system, not even a relational database management system (RDBMS). NoSQL stands for “Not Only SQL”. NoSQL is a type of database that can handle and sort all type of unstructured, messy and complicated data. It is just a new way to think about the database.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://medium.com/@hub4tech/list-of-top-10-mongodb-interview-questions-for-beginners-and-professionals-ee385b9c501b"&gt;medium.com/@hub4tech&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q3:&lt;/em&gt; Explain difference between scaling horizontally and vertically for databases ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; NoSQL&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Horizontal scaling means that you scale by adding more machines into your pool of resources whereas &lt;/li&gt;
&lt;li&gt;Vertical scaling means that you scale by adding more power (CPU, RAM) to an existing machine.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a database world horizontal-scaling is often based on the partitioning of the data i.e. each node contains only part of the data, in vertical-scaling the data resides on a single node and scaling is done through multi-core i.e. spreading the load between the CPU and RAM resources of that machine.&lt;/p&gt;

&lt;p&gt;Good examples of horizontal scaling are Cassandra, MongoDB, Google Cloud Spanner. and a good example of vertical scaling is MySQL - Amazon RDS (The cloud version of MySQL).&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/11707879/difference-between-scaling-horizontally-and-vertically-for-databases"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q4:&lt;/em&gt; What are the advantages of NoSQL over traditional RDBMS? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; NoSQL Databases&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;NoSQL is better&lt;/strong&gt; than RDBMS because of the following reasons/properities of NoSQL:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It supports semi-structured data and volatile data&lt;/li&gt;
&lt;li&gt;It does not have schema&lt;/li&gt;
&lt;li&gt;Read/Write throughput is very high&lt;/li&gt;
&lt;li&gt;Horizontal &lt;strong&gt;scalability&lt;/strong&gt; can be achieved easily&lt;/li&gt;
&lt;li&gt;Will support Bigdata in volumes of Terra Bytes &amp;amp; Peta Bytes&lt;/li&gt;
&lt;li&gt;Provides good support for Analytic tools on top of Bigdata&lt;/li&gt;
&lt;li&gt;Can be hosted in cheaper hardware machines&lt;/li&gt;
&lt;li&gt;In-memory caching option is available to increase the performance of queries&lt;/li&gt;
&lt;li&gt;Faster development life cycles for developers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Still, &lt;strong&gt;RDBMS is better&lt;/strong&gt; than NoSQL for the following reasons/properties of RDBMS:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transactions with &lt;strong&gt;ACID&lt;/strong&gt; properties - Atomicity, Consistency, Isolation &amp;amp; Durability&lt;/li&gt;
&lt;li&gt;Adherence to &lt;strong&gt;Strong Schema&lt;/strong&gt; of data being written/read&lt;/li&gt;
&lt;li&gt;Real time query management ( in case of data size &amp;lt; 10 Tera bytes )&lt;/li&gt;
&lt;li&gt;Execution of complex queries involving &lt;strong&gt;join&lt;/strong&gt; &amp;amp; &lt;strong&gt;group by&lt;/strong&gt; clauses&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/4160732/nosql-vs-relational-database"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q5:&lt;/em&gt; When should we embed one document within another in MongoDB? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; MongoDB&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;You should consider embedding documents for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;contains&lt;/em&gt; relationships between entities&lt;/li&gt;
&lt;li&gt;One-to-many relationships&lt;/li&gt;
&lt;li&gt;Performance reasons&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.tutorialspoint.com/mongodb/mongodb_interview_questions.htm"&gt;tutorialspoint.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q6:&lt;/em&gt; Define ACID Properties ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; SQL Databases&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Atomicity&lt;/strong&gt;: It ensures all-or-none rule for database modifications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consistency&lt;/strong&gt;: Data values are consistent across the database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Isolation&lt;/strong&gt;: Two transactions are said to be independent of one another.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Durability&lt;/strong&gt;: Data is not lost even at the time of server failure.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://github.com/chetansomani/SQL-Interview-Questions"&gt;github.com/chetansomani&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q7:&lt;/em&gt; Does MongoDB support ACID transaction management and locking functionalities? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; MongoDB&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;ACID stands that any update is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Atomic&lt;/strong&gt;: it either fully completes or it does not&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consistent&lt;/strong&gt;: no reader will see a "partially applied" update&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Isolated&lt;/strong&gt;: no reader will see a "dirty" read&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Durable&lt;/strong&gt;: (with the appropriate write concern)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Historically MongoDB does not support default multi-document ACID transactions (multiple-document updates that can be rolled back and are ACID-compliant). However, MongoDB provides atomic operation on a single document. MongoDB 4.0 &lt;strong&gt;will add support for multi-document transactions&lt;/strong&gt;, making it the only database to combine the speed, flexibility, and power of the document model with ACID data integrity guarantees.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.tutorialspoint.com/mongodb/mongodb_interview_questions.htm"&gt;tutorialspoint.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q8:&lt;/em&gt; Explain advantages of BSON over JSON in MongoDB? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; MongoDB JSON&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;BSON&lt;/strong&gt; is designed to be efficient in space, but in some cases is not much more efficient than JSON. In some cases BSON uses even more space than JSON. The reason for this is another of the BSON design goals: traversability. BSON adds some "extra" information to documents, like length of strings and subobjects. This makes traversal faster.&lt;/li&gt;
&lt;li&gt;BSON is also designed to be fast to encode and decode. For example, integers are stored as 32 (or 64) bit integers, so they don't need to be parsed to and from text. This uses more space than JSON for small integers, but is much faster to parse.&lt;/li&gt;
&lt;li&gt;In addition to compactness, BSON adds additional data types unavailable in JSON, notably the BinData and Date data types.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/12438280/what-is-bson-and-exactly-how-is-it-different-from-json"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q9:&lt;/em&gt; How can you achieve primary key - foreign key relationships in MongoDB? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; MongoDB&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;By default MongoDB does not support such primary key - foreign key relationships. However, we can achieve this concept by embedding one document inside another (aka subdocuments). Foe e.g. an address document can be embedded inside customer document.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.tutorialspoint.com/mongodb/mongodb_interview_questions.htm"&gt;tutorialspoint.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q10:&lt;/em&gt; How do I perform the SQL JOIN equivalent in MongoDB? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; MongoDB&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;Mongo is not a relational database, and the devs are being careful to recommend specific use cases for $lookup, but at least as of 3.2 doing join is now possible with MongoDB. The new $lookup operator added to the aggregation pipeline is essentially identical to a left outer join:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;$lookup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
     &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="na"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;localField&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;field&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;documents&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;foreignField&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;field&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;documents&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;from&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;as&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="nx"&gt;field&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/2350495/how-do-i-perform-the-sql-join-equivalent-in-mongodb"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q11:&lt;/em&gt; How does column-oriented NoSQL differ from document-oriented? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; NoSQL&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;The main difference is that &lt;strong&gt;document stores&lt;/strong&gt; (e.g. MongoDB and CouchDB) allow arbitrarily complex documents, i.e. subdocuments within subdocuments, lists with documents, etc. whereas &lt;strong&gt;column stores&lt;/strong&gt; (e.g. Cassandra and HBase) only allow a fixed format, e.g. strict one-level or two-level dictionaries.&lt;/p&gt;

&lt;p&gt;For example a document-oriented database (like MongoDB) inserts whole documents (typically JSON), whereas in Cassandra (column-oriented db) you can address individual columns or supercolumns, and update these individually, i.e. they work at a different level of granularity. Each column has its own separate timestamp/version (used to reconcile updates across the distributed cluster).&lt;/p&gt;

&lt;p&gt;The Cassandra column values are just bytes, but can be typed as ASCII, UTF8 text, numbers, dates etc. You could use Cassandra as a primitive document store by inserting columns containing JSON - but you wouldn't get all the features of a real document-oriented store.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/7565012/how-does-column-oriented-nosql-differ-from-document-oriented"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q12:&lt;/em&gt; What does Document-oriented vs. Key-Value mean in context of NoSQL? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; NoSQL&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;key-value&lt;/strong&gt; store provides the simplest possible data model and is exactly what the name suggests: it's a storage system that stores values indexed by a key. You're limited to query by key and the values are opaque, the store doesn't know anything about them. This allows very fast read and write operations (a simple disk access) and I see this model as a kind of non volatile cache (i.e. well suited if you need fast accesses by key to long-lived data).&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;document-oriented&lt;/strong&gt; database extends the previous model and values are stored in a structured format (a document, hence the name) that the database can understand. For example, a document could be a blog post and the comments and the tags stored in a denormalized way. Since the data are transparent, the store can do more work (like indexing fields of the document) and you're not limited to query by key. As I hinted, such databases allows to fetch an entire page's data with a single query and are well suited for content oriented applications (which is why big sites like Facebook or Amazon like them).&lt;/p&gt;

&lt;p&gt;Other kinds of NoSQL databases include column-oriented stores, graph databases and even object databases. &lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/4126811/how-do-you-track-record-relations-in-nosql"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q13:&lt;/em&gt; What is Denormalization? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; SQL Databases&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;It is the process of improving the performance of the database by &lt;em&gt;adding&lt;/em&gt; redundant data.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://github.com/dhaval1406/SQL"&gt;github.com/dhaval1406&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q14:&lt;/em&gt; What is Sharding in MongoDB? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; MongoDB&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Sharding&lt;/em&gt; is a method for storing data across multiple machines. MongoDB uses sharding to support deployments with very large data sets and high throughput operations.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.tutorialspoint.com/mongodb/mongodb_interview_questions.htm"&gt;tutorialspoint.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q15:&lt;/em&gt; When should I use a NoSQL database instead of a relational database? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; NoSQL Databases&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Relational databases&lt;/strong&gt; enforces ACID. So, you will have schema based transaction oriented data stores. It's proven and suitable for 99% of the real world applications. You can practically do anything with relational databases.&lt;/p&gt;

&lt;p&gt;But, there are limitations on speed and scaling when it comes to massive high availability data stores. For example, Google and Amazon have terabytes of data stored in big data centers. Querying and inserting is not performant in these scenarios because of the blocking/schema/transaction nature of the RDBMs. That's the reason they have implemented their own databases (actually, &lt;strong&gt;key-value stores&lt;/strong&gt;) for massive performance gain and scalability.&lt;/p&gt;

&lt;p&gt;If you need a NoSQL db you usually know about it, possible reasons are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;client wants 99.999% availability on a high traffic site.&lt;/li&gt;
&lt;li&gt;your data makes no sense in SQL, you find yourself doing multiple JOIN queries for accessing some piece of information.&lt;/li&gt;
&lt;li&gt;you are breaking the relational model, you have CLOBs that store denormalized data and you generate external indexes to search that data.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/3713313/when-should-i-use-a-nosql-database-instead-of-a-relational-database-is-it-okay"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q16:&lt;/em&gt; When would you use NoSQL? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; NoSQL&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;It depends from some general points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NoSQL is typically good for unstructured/"schemaless" data - usually, you don't need to explicitly define your schema up front and can just include new fields without any ceremony&lt;/li&gt;
&lt;li&gt;NoSQL typically favours a denormalised schema due to no support for JOINs per the RDBMS world. So you would usually have a flattened, denormalized representation of your data.&lt;/li&gt;
&lt;li&gt;Using NoSQL doesn't mean you could lose data. Different DBs have different strategies. e.g. MongoDB - you can essentially choose what level to trade off performance vs potential for data loss - best performance = greater scope for data loss.&lt;/li&gt;
&lt;li&gt;It's often very easy to scale out NoSQL solutions. Adding more nodes to replicate data to is one way to a) offer more scalability and b) offer more protection against data loss if one node goes down. But again, depends on the NoSQL DB/configuration. NoSQL does not necessarily mean "data loss" like you infer.&lt;/li&gt;
&lt;li&gt;IMHO, complex/dynamic queries/reporting are best served from an RDBMS. Often the query functionality for a NoSQL DB is limited.&lt;/li&gt;
&lt;li&gt;It doesn't have to be a 1 or the other choice. My experience has been using RDBMS in conjunction with NoSQL for certain use cases.&lt;/li&gt;
&lt;li&gt;NoSQL DBs often lack the ability to perform atomic operations across multiple "tables".&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/10553860/nosql-use-case-scenarios-or-when-to-use-nosql"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q17:&lt;/em&gt; Explain BASE terminology in a context of NoSQL ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; NoSQL&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;BASE&lt;/strong&gt; acronym is used to describe the properties of certain databases, usually NoSQL databases. It's often referred to as the opposite of ACID. The BASE acronym was defined by Eric Brewer, who is also known for formulating the CAP theorem.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;CAP&lt;/strong&gt; theorem states that a distributed computer system cannot guarantee all of the following three properties at the same time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Consistency&lt;/li&gt;
&lt;li&gt;Availability&lt;/li&gt;
&lt;li&gt;Partition tolerance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A &lt;strong&gt;BASE&lt;/strong&gt; system gives up on &lt;em&gt;consistency&lt;/em&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basically &lt;em&gt;available&lt;/em&gt; indicates that the system does guarantee availability, in terms of the CAP theorem.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Soft state&lt;/em&gt; indicates that the state of the system may change over time, even without input. This is because of the eventual consistency model.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Eventual consistency&lt;/em&gt; indicates that the system will become consistent over time, given that the system doesn't receive input during that time.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/3342497/explanation-of-base-terminology"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q18:&lt;/em&gt; Explain eventual consistency in context of NoSQL ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; NoSQL&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;Think about &lt;strong&gt;Eventual consistency&lt;/strong&gt; (as opposed to Strict Consistency/ACID compliance) as:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Your data is replicated on multiple servers&lt;/li&gt;
&lt;li&gt;Your clients can access any of the servers to retrieve the data&lt;/li&gt;
&lt;li&gt;Someone writes a piece of data to one of the servers, but it wasn't yet copied to the rest&lt;/li&gt;
&lt;li&gt;A client accesses the server with the data, and gets the most up-to-date copy&lt;/li&gt;
&lt;li&gt;A different client (or even the same client) accesses a different server (one which didn't get the new copy yet), and gets the old copy&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Basically, because it takes time to replicate the data across multiple servers, requests to read the data might go to a server with a new copy, and then go to a server with an old copy. The term "eventual" means that eventually the data will be replicated to all the servers, and thus they will all have the up-to-date copy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Eventual consistency&lt;/strong&gt; is a must if you want low latency reads, since the responding server must return its own copy of the data, and doesn't have time to consult other servers and reach a mutual agreement on the content of the data.&lt;/p&gt;

&lt;p&gt;The reason why so many NoSQL systems have &lt;strong&gt;eventual consistency&lt;/strong&gt; is that virtually all of them are designed to be distributed, and with fully distributed systems there is super-linear overhead to maintaining strict consistency (meaning you can only scale so far before things start to slow down, and when they do you need to throw exponentially more hardware at the problem to keep scaling).&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/10078540/eventual-consistency-in-plain-english"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q19:&lt;/em&gt; Explain how would you keep document change history in NoSQL DB? ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; NoSQL&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;There are some solution for that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Create a new version of the document on each change&lt;/strong&gt; - Add a version number to each document on change. The major drawback is that the entire document is duplicated on each change, which will result in a lot of duplicate content being stored when you're dealing with large documents. This approach is fine though when you're dealing with small-sized documents and/or don't update documents very often.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Only store changes in a new version&lt;/strong&gt; - For that store only the changed fields in a new version. Then you can 'flatten' your history to reconstruct any version of the document. This is rather complex though, as you need to track changes in your model and store updates and deletes in a way that your application can reconstruct the up-to-date document. This might be tricky, as you're dealing with structured documents rather than flat SQL tables.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Store changes within the document&lt;/strong&gt; - Each field can also have an individual history. Reconstructing documents to a given version is much easier this way. In your application you don't have to explicitly track changes, but just create a new version of the property when you change its value.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4c6b9456f61f000000007ba6&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello world&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Is this thing on?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What should I write?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is the new body&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="nx"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;test&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;trivial&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;test&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="nx"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;joe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Unversioned field&lt;/span&gt;
      &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Something cool&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;xxx&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Spam&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;deleted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;jim&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Not bad&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Not bad at all&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Variation on Store changes within the document&lt;/strong&gt; - Instead of storing versions against each key pair, the current key pairs in the document always represents the most recent state and a 'log' of changes is stored within a history array. Only those keys which have changed since creation will have an entry in the log.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4c6b9456f61f000000007ba6&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Is this thing on?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;test&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;trivial&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="nx"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;joe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Something cool&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;xxx&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Spam&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;deleted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;jim&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Not bad at all&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="na"&gt;who&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;joe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;when&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20160101&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;what&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What should I write?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="na"&gt;who&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;jim&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;when&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20160105&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;what&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;test&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;test2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="na"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Not baaad at all&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/3507624/mongodb-nosql-keeping-document-change-history"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q20:&lt;/em&gt; Explain use of transactions in NoSQL ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; NoSQL&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;NoSQL covers a diverse set of tools and services, including key-value-, document, graph and wide-column stores. They usually try improving scalability of the data store, usually by distributing data processing. Transactions require ACID properties of how DBs perform user operations. ACID restricts how scalability can be improved: most of the NoSQL tools relax consistency criteria of the operatioins to get fault-tolerance and availability for scaling, which makes implementing ACID transactions very hard.&lt;/p&gt;

&lt;p&gt;A commonly cited theoretical reasoning of distributed data stores is the CAP theorem: consistency, availability and partition tolerance cannot be achieved at the same time. &lt;/p&gt;

&lt;p&gt;A new, weaker set of requirements replacing ACID is BASE ("basically avalilable, soft state, eventual consistency"). However, eventually consistent tools ("eventually all accesses to an item will return the last updated value") are hardly acceptable in transactional applications like banking.&lt;/p&gt;

&lt;p&gt;Generally speaking, NoSQL solutions have lighter weight transactional semantics than relational databases, but still have facilities for atomic operations at some level. Generally, the ones which do master-master replication provide less in the way of consistency, and more availability. So one should choose the right tool for the right problem.&lt;/p&gt;

&lt;p&gt;Many offer transactions at the single document (or row etc.) level. For example with MongoDB there is atomicity at the single document - but documents can be fairly rich so this usually works.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/2212230/transactions-in-nosql"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q21:&lt;/em&gt; How do you track record relations in NoSQL? ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; NoSQL&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;All the answers for how to store many-to-many associations in the "NoSQL way" reduce to the same thing: &lt;strong&gt;storing data redundantly&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In NoSQL, you don't design your database based on the relationships between data entities. You design your database based on the queries you will run against it. Use the same criteria you would use to denormalize a relational database: if it's more important for data to have cohesion (think of values in a comma-separated list instead of a normalized table), then do it that way.&lt;/p&gt;

&lt;p&gt;But this inevitably optimizes for one type of query (e.g. comments by any user for a given article) at the expense of other types of queries (comments for any article by a given user). If your application has the need for both types of queries to be equally optimized, you should not denormalize. And likewise, you should not use a NoSQL solution if you need to use the data in a relational way.&lt;/p&gt;

&lt;p&gt;There is a risk with denormalization and redundancy that redundant sets of data will get out of sync with one another. This is called an anomaly. When you use a normalized relational database, the RDBMS can prevent anomalies. In a denormalized database or in NoSQL, it becomes your responsibility to write application code to prevent anomalies.&lt;/p&gt;

&lt;p&gt;One might think that it'd be great for a NoSQL database to do the hard work of preventing anomalies for you. There is a paradigm that can do this - the relational paradigm.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/4126811/how-do-you-track-record-relations-in-nosql"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q22:&lt;/em&gt; How does MongoDB ensure high availability? ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; MongoDB&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;MongoDB automatically maintains replica sets, multiple copies of data that are distributed across servers, racks and data centers. Replica sets help prevent database downtime using native replication and automatic failover.&lt;/p&gt;

&lt;p&gt;A replica set consists of multiple replica set members. At any given time one member acts as the primary member, and the other members act as secondary members. If the primary member fails for any reason (e.g., hardware failure), one of the secondary members is automatically elected to primary and begins to process all reads and writes.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.mongodb.com/faq"&gt;mongodb.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q23:&lt;/em&gt; MongoDB relationships. What to use - embed or reference? ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; MongoDB&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;I want to design a question structure with some comments, but I don't know which relationship to use for comments: embed or reference? Explain me pros and cons of both solutions? &lt;/p&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;In general, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;embed&lt;/strong&gt; is good if you have one-to-one or one-to-many relationships between entities, and &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;reference&lt;/strong&gt; is good if you have many-to-many relationships.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also consider as a general rule, if you have a lot of [child documents] or if they are large, a separate collection might be best. Smaller and/or fewer documents tend to be a natural fit for embedding.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/5373198/mongodb-relationships-embed-or-reference"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q24:&lt;/em&gt; Explain the differences in conceptual data design with NoSQL databases? ☆☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; NoSQL Databases&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;What's easier, what's harder, what can't be done at all?&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;I'm answering this with MongoDB in the back of my mind, but I would presume most would be true for other DBs also.&lt;/p&gt;

&lt;p&gt;Harder:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Consistency&lt;/em&gt; is not handled by the database but must be dealt with in the application. Less guarantees means easier migration, fail-over and better scalability at the cost of a more complicated application. An application has to deal with conflicts and inconsistencies.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Links&lt;/em&gt; which cross documents (or key/value) have to be dealt with on application level also.&lt;/li&gt;
&lt;li&gt;SQL type of databases have IDEs which are much more mature. You get a lot of support libraries (although the layering of those libraries make things much more complex than needed for SQL).&lt;/li&gt;
&lt;li&gt;Keep related data together in the same document could be tricky, since there is nothing corresponding to a &lt;em&gt;join&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Map/reduce as a means of querying a database is unfamiliar, and requires a lot more thinking than writing SQL.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Easier:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Faster if you know your &lt;em&gt;data access patterns&lt;/em&gt; (views or specific queries).&lt;/li&gt;
&lt;li&gt;Migration / Fail-over is easier for the database since no promises are made to you as an application programmer. Although you get eventual consistency.&lt;/li&gt;
&lt;li&gt;One key / value is much easier to understand than one row from a table. All the (tree) relations are already in, and complete objects can be recognized.&lt;/li&gt;
&lt;li&gt;No designing DB tables&lt;/li&gt;
&lt;li&gt;No ODBC/JDBC intermediate layer, all queries and transactions over http&lt;/li&gt;
&lt;li&gt;Simple DB-to-object mapping from JSON, which is almost trivial compared to the same in SQL&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/4126811/how-do-you-track-record-relations-in-nosql"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q25:&lt;/em&gt; Where does MongoDB stand in the CAP theorem? ☆☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; MongoDB&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;MongoDB is strongly consistent by default - if you do a write and then do a read, assuming the write was successful you will always be able to read the result of the write you just read. This is because MongoDB is a single-master system and all reads go to the primary by default.&lt;/p&gt;

&lt;p&gt;On the other hand you can't just say that MongoDB is CP/AP/CA, because it actually is a trade-off between C, A and P, depending on both database/driver configuration and type of disaster: here's a visual recap, and below a more detailed explanation.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;Main Focus&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;No partition&lt;/td&gt;
&lt;td&gt;CA&lt;/td&gt;
&lt;td&gt;The system is available&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;and provides strong consistency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;---------------------------&lt;/td&gt;
&lt;td&gt;------------&lt;/td&gt;
&lt;td&gt;------------------------------------&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;partition,&lt;/td&gt;
&lt;td&gt;AP&lt;/td&gt;
&lt;td&gt;Not synchronized writes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;majority connected&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;from the old primary are ignored&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;---------------------------&lt;/td&gt;
&lt;td&gt;------------&lt;/td&gt;
&lt;td&gt;------------------------------------&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;partition,&lt;/td&gt;
&lt;td&gt;CP&lt;/td&gt;
&lt;td&gt;only read access is provided&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;majority not connected&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;to avoid separated and inconsistent systems&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Consistency&lt;/strong&gt; -&lt;br&gt;
MongoDB is strongly consistent when you use a single connection or the correct Write/Read Concern Level (Which will cost you execution speed). As soon as you don't meet those conditions (especially when you are reading from a secondary-replica) MongoDB becomes Eventually Consistent. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Availability&lt;/strong&gt; -&lt;br&gt;
MongoDB gets high availability through Replica-Sets. As soon as the primary goes down or gets unavailable else, then the secondaries will determine a new primary to become available again. There is an disadvantage to this: Every write that was performed by the old primary, but not synchronized to the secondaries will be rolled back and saved to a rollback-file, as soon as it reconnects to the set(the old primary is a secondary now). So in this case some consistency is sacrificed for the sake of availability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Partition Tolerance&lt;/strong&gt; -&lt;br&gt;
Through the use of said Replica-Sets MongoDB also achieves the partition tolerance: As long as more than half of the servers of a Replica-Set is connected to each other, a new primary can be chosen. Why? To ensure two separated networks can not both choose a new primary. When not enough secondaries are connected to each other you can still read from them (but consistency is not ensured), but not write. The set is practically unavailable for the sake of consistency. &lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/4126811/how-do-you-track-record-relations-in-nosql"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Thanks 🙌 for reading and good luck on your interview!&lt;/em&gt; &lt;em&gt;Please share this article with your fellow Devs if you like it!&lt;/em&gt; &lt;em&gt;Check more FullStack Interview Questions &amp;amp; Answers on 👉 &lt;a href="https://www.fullstack.cafe"&gt;www.fullstack.cafe&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>mongodb</category>
      <category>nosql</category>
      <category>career</category>
      <category>interview</category>
    </item>
    <item>
      <title>32 Linked List Interview Questions (ANSWERED) To Nail Your Coding Interview</title>
      <dc:creator>Alex 👨🏼‍💻FullStack.Cafe</dc:creator>
      <pubDate>Tue, 13 Oct 2020 02:02:38 +0000</pubDate>
      <link>https://forem.com/aershov24/32-linked-list-interview-questions-answered-to-nail-your-coding-interview-4nb4</link>
      <guid>https://forem.com/aershov24/32-linked-list-interview-questions-answered-to-nail-your-coding-interview-4nb4</guid>
      <description>&lt;p&gt;Linked lists are among the simplest and most common data structures. They can be used to implement several other common abstract data types, including lists, stacks, queues, associative arrays and etc. Follow along and check 43 most common Linked List Interview Questions with Answers and Solutions to stay prepare for your next coding interview.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Originally published on &lt;a href="https://www.fullstack.cafe/blog/linked-list-interview-questions?utm_source=dev&amp;amp;utm_medium=blog"&gt;FullStack.Cafe - Kill Your Next Tech Interview&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q1:&lt;/em&gt; Name some advantages of Linked List ☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;There are some:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Linked Lists are &lt;strong&gt;Dynamic Data Structure&lt;/strong&gt; -  it can grow and shrink at runtime by allocating and deallocating memory. So there is no need to give initial size of linked list.&lt;/li&gt;
&lt;li&gt;Insertion and Deletion are &lt;strong&gt;simple to implement&lt;/strong&gt; - Unlike array here we don’t have to shift elements after insertion or deletion of an element. In linked list we just have to update the address present in next pointer of a node.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficient Memory Allocation/No Memory Wastage&lt;/strong&gt; - In case of array there is lot of memory wastage, like if we declare an array of size 10 and store only 6 elements in it then space of 4 elements are wasted. There is no such problem in linked list as memory is allocated only when required.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.thecrazyprogrammer.com/2016/11/advantages-disadvantages-linked-list.html"&gt;www.thecrazyprogrammer.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q2:&lt;/em&gt; Convert a Single Linked List to a Double Linked List ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;A doubly linked list is simply a linked list where every element has both next and prev mebers, pointing at the elements before and after it, not just the one after it in a single linked list.&lt;/p&gt;

&lt;p&gt;so to convert your list to a doubly linked list, just change your node to be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Picture&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;pNext&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;pPrev&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and when iterating the list, on each new node add a reference to the previous node.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/9542154/converting-a-single-linked-list-to-a-double-linked-list/9542187#9542187"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q3:&lt;/em&gt; Convert a Singly Linked List to Circular Linked List ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;To convert a singly linked list to circular linked list, we will set next pointer of tail node to head pointer.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Create a copy of head pointer, let's say &lt;code&gt;temp&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  Using a loop, traverse linked list till tail node (last node) using temp pointer.&lt;/li&gt;
&lt;li&gt;  Now set the next pointer of tail node to head node. &lt;code&gt;temp\-&amp;gt;next = head&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;PY&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;convertTocircular&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# declare a node variable
&lt;/span&gt;    &lt;span class="c1"&gt;# start and assign head
&lt;/span&gt;    &lt;span class="c1"&gt;# node into start node.
&lt;/span&gt;    &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;

    &lt;span class="c1"&gt;# check that
&lt;/span&gt;    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
    &lt;span class="c1"&gt;# not equal to null then head
&lt;/span&gt;    &lt;span class="c1"&gt;# points to next node.
&lt;/span&gt;    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
      &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;

    &lt;span class="c1"&gt;#
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="n"&gt;points&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt;
    &lt;span class="c1"&gt;# then start assign to the
&lt;/span&gt;    &lt;span class="c1"&gt;# head.next node.
&lt;/span&gt;    &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.techcrashcourse.com/2016/06/program-convert-singly-linked-list-to-circular-linked-list.html"&gt;www.techcrashcourse.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q4:&lt;/em&gt; Detect if a List is Cyclic using Hash Table ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists Hash Tables&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;To detect if a list is cyclic, we can check whether a node had been visited before. A natural way is to use a hash table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We go through each node one by one and record each node's reference (or memory address) in a hash table. If the current node is &lt;code&gt;null&lt;/code&gt;, we have reached the end of the list and it must not be cyclic. If current node’s reference is in the hash table, then return true.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity Analysis:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(n)&lt;br&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: O(n)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Time complexity : &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt; . We visit each of the &lt;code&gt;n&lt;/code&gt; elements in the list at most once. Adding a node to the hash table costs only &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt; time.&lt;/li&gt;
&lt;li&gt;  Space complexity: &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt; . The space depends on the number of elements added to the hash table, which contains at most &lt;code&gt;n&lt;/code&gt; elements.
### Implementation:
#### &lt;em&gt;Java&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;hasCycle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nodesSeen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nodesSeen&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;nodesSeen&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://leetcode.com/problems/linked-list-cycle/solution/"&gt;leetcode.com&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;em&gt;Q5:&lt;/em&gt; How to reverse a singly Linked List using only two pointers? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;
&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;Nothing faster than &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt; can be done. You need to traverse the list and alter pointers on every node, so time will be proportional to the number of elements.&lt;/p&gt;
&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// move the head to next node&lt;/span&gt;
        &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// break the link to the next node and assign it to previous&lt;/span&gt;
        &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// we are done with previous, move it to next node&lt;/span&gt;
        &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// current moves along with head&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;//for last node&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/1801549/how-to-reverse-a-singly-linked-list-using-only-two-pointers"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;em&gt;Q6:&lt;/em&gt; Insert an item in a sorted Linked List maintaining order ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Sorting Linked Lists&lt;/p&gt;
&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;add()&lt;/code&gt; method below walks down the list until it finds the appropriate position. Then, it splices in the new node and updates the &lt;code&gt;start&lt;/code&gt;, &lt;code&gt;prev&lt;/code&gt;, and &lt;code&gt;curr&lt;/code&gt; pointers where applicable.&lt;/p&gt;

&lt;p&gt;Note that the reverse operation, namely &lt;em&gt;removing&lt;/em&gt; elements, doesn't need to change, because you are simply throwing things away which would not change any order in the list.&lt;/p&gt;
&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;newNode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;info&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// case: start is null; just assign start to the new node and return&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newNode&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// prev is null, hence not formally assigned here&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// case: new node to be inserted comes before the current start;&lt;/span&gt;
    &lt;span class="c1"&gt;//       in this case, point the new node to start, update pointers, and return&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compareTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;info&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;newNode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;link&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newNode&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// again we leave prev undefined, as it is null&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// otherwise walk down the list until reaching either the end of the list&lt;/span&gt;
    &lt;span class="c1"&gt;// or the first position whose element is greater than the node to be&lt;/span&gt;
    &lt;span class="c1"&gt;// inserted; then insert the node and update the pointers&lt;/span&gt;
    &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compareTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;info&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;link&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// splice in the new node and update the curr pointer (prev already correct)&lt;/span&gt;
    &lt;span class="n"&gt;newNode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;link&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;link&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;link&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newNode&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newNode&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/41959004/keeping-a-linkedlist-always-sorted"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;em&gt;Q7:&lt;/em&gt; Under what circumstances are Linked Lists useful? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists Data Structures&lt;/p&gt;
&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;Linked lists are very useful when you need :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;to do a lot of insertions and removals, but not too much searching, on a list of arbitrary (unknown at compile-time) length.&lt;/li&gt;
&lt;li&gt;splitting and joining (bidirectionally-linked) lists is very efficient.&lt;/li&gt;
&lt;li&gt;You can also combine linked lists - e.g. tree structures can be implemented as "vertical" linked lists (parent/child relationships) connecting together horizontal linked lists (siblings).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using an array based list for these purposes has severe limitations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Adding a new item means the array must be reallocated (or you must allocate more space than you need to allow for future growth and reduce the number of reallocations)&lt;/li&gt;
&lt;li&gt;  Removing items leaves wasted space or requires a reallocation&lt;/li&gt;
&lt;li&gt;  inserting items anywhere except the end involves (possibly reallocating and) copying lots of the data up one position&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/2429217/under-what-circumstances-are-linked-lists-useful"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;em&gt;Q8:&lt;/em&gt; What are some types of Linked List? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists Data Structures&lt;/p&gt;
&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;singly linked list&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PMT1jwiR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.cs.cmu.edu/%257Eadamchik/15-121/lectures/Linked%2520Lists/pix/linkedlist.bmp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PMT1jwiR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.cs.cmu.edu/%257Eadamchik/15-121/lectures/Linked%2520Lists/pix/linkedlist.bmp" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;doubly linked list&lt;/strong&gt; is a list that has two references, one to the next node and another to previous node.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dbjXzUct--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.cs.cmu.edu/%257Eadamchik/15-121/lectures/Linked%2520Lists/pix/doubly.bmp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dbjXzUct--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.cs.cmu.edu/%257Eadamchik/15-121/lectures/Linked%2520Lists/pix/doubly.bmp" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;multiply linked list&lt;/strong&gt; - each node contains two or more link fields, each field being used to connect the same set of data records in a different order of same set(e.g., by name, by department, by date of birth, etc.).&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;circular linked list&lt;/strong&gt; - where last node of the list points back to the first node (or the head) of the list.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--szFIZyzM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/algorithms.tutorialhorizon.com/files/2016/03/Circular-Linked-List.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--szFIZyzM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/algorithms.tutorialhorizon.com/files/2016/03/Circular-Linked-List.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/linked%20lists.html"&gt;www.cs.cmu.edu&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;em&gt;Q9:&lt;/em&gt; What is a cycle/loop in the singly-linked list? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;
&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;cycle/loop&lt;/strong&gt; occurs when a node’s next points &lt;em&gt;back&lt;/em&gt; to a &lt;em&gt;previous node&lt;/em&gt; in the list. The linked list is no longer linear with a beginning and end—instead, it cycles through a loop of nodes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3HETOCCE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.programcreek.com/wp-content/uploads/2012/12/linked-list-cycle-300x211.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3HETOCCE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.programcreek.com/wp-content/uploads/2012/12/linked-list-cycle-300x211.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.interviewcake.com/question/java/linked-list-cycles"&gt;www.interviewcake.com&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;em&gt;Q10:&lt;/em&gt; What is complexity of &lt;code&gt;push&lt;/code&gt; and &lt;code&gt;pop&lt;/code&gt; for a Stack implemented using a LinkedList? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists Stacks Big-O Notation&lt;/p&gt;
&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt;. Note, you don't have to insert at the end of the list. If you insert at the front of a (singly-linked) list, they are both &lt;code&gt;O(1)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Stack contains 1,2,3:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Push 5:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// returning 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/6537150/time-complexity-of-a-stack-adt-implemented-using-a-linked-list#:~:text=For%20a%20doubly%20linked%20list,operations%20of%20enqueue%20and%20dequeue."&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q11:&lt;/em&gt; What is time complexity of Linked List operations? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A linked list can typically only be accessed via its head node. From there you can only traverse from node to node until you reach the node you seek. Thus &lt;strong&gt;access is &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Searching for a given value in a linked list similarly requires traversing all the elements until you find that value. Thus &lt;strong&gt;search is &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Inserting into a linked list requires re-pointing the previous node (the node before the insertion point) to the inserted node, and pointing the newly-inserted node to the next node. Thus &lt;strong&gt;insertion is &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Deleting from a linked list requires re-pointing the previous node (the node before the deleted node) to the next node (the node after the deleted node). Thus &lt;strong&gt;deletion is &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://github.com/tim-hr/stuff/wiki/Time-complexity:-linked-lists"&gt;github.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q12:&lt;/em&gt;  Split the Linked List into &lt;code&gt;k&lt;/code&gt; consecutive linked list "parts" ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;Given a (singly) linked list with head node root, write a function to split the linked list into k consecutive linked list "parts".&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The length of each part should be as equal as possible: no two parts should have a size differing by more than 1. This may lead to some parts being null.&lt;/li&gt;
&lt;li&gt;The parts should be in order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal parts occurring later.&lt;/li&gt;
&lt;li&gt;Return a List of ListNode's representing the linked list parts that are formed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="nx"&gt;ans&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If there are &lt;code&gt;N&lt;/code&gt; nodes in the linked list &lt;code&gt;root&lt;/code&gt;, then there are &lt;code&gt;N/k&lt;/code&gt; items in each part, plus the first &lt;code&gt;N%k&lt;/code&gt; parts have an extra item. We can count &lt;code&gt;N&lt;/code&gt; with a simple loop.&lt;/li&gt;
&lt;li&gt;Now for each part, we have calculated how many nodes that part will have: &lt;code&gt;width + (i &amp;lt; remainder ? 1 : 0)&lt;/code&gt;. We create a new list and write the part to that list.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;splitListToParts&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;ans&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
  &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="n"&gt;write&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;rem&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;write&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;ans&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ans&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;PY&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;splitListToParts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;N&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;xrange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1001&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;
        &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
    &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;remainder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;divmod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;ans&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;xrange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;write&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;xrange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;remainder&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
            &lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;write&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
        &lt;span class="n"&gt;ans&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ans&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://leetcode.com/problems/split-linked-list-in-parts/solution/"&gt;leetcode.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q13:&lt;/em&gt; Compare Array based vs Linked List stack implementations ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Arrays Linked Lists Stacks&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;The following implementations are most common:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Stack backed by a &lt;strong&gt;singly-linked list&lt;/strong&gt;. Because a singly-linked list supports &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt; time prepend and delete-first, the cost to push or pop into a linked-list-backed stack is also &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt; worst-case. However, each new element added requires a new allocation, and allocations can be expensive compared to other operations. The &lt;em&gt;locality&lt;/em&gt; of the linked list is not as good as the locality of the dynamic array since there is no guarantee that the linked list cells will be stored contiguously in memory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stack backed by a &lt;strong&gt;dynamic array&lt;/strong&gt;. Pushing onto the stack can be implemented by appending a new element to the dynamic array, which takes &lt;strong&gt;amortised&lt;/strong&gt; &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt; time and worst-case &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt; time. Popping from the stack can be implemented by just removing the last element, which runs in worst-case &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt; (or amortised &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt; if you want to try to reclaim unused space). In other words, the most common implementation has best-case &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt; push and pop, worst-case &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt; push and &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt; pop, and amortised &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt; push and &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt; pop.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/7477181/array-based-vs-list-based-stacks-and-queues"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q14:&lt;/em&gt; Convert a Binary Tree to a Doubly Linked List ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Binary Tree Divide &amp;amp; Conquer Linked Lists Recursion Data Structures&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;This can be achieved by traversing the tree in the &lt;strong&gt;in-order&lt;/strong&gt; manner that is, left the &lt;code&gt;child -&amp;gt; root -&amp;gt;right&lt;/code&gt; node. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eaHRK4Ab--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.techiedelight.com/wp-content/uploads/Inorder-Traversal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eaHRK4Ab--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.techiedelight.com/wp-content/uploads/Inorder-Traversal.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In an in-order traversal, first the left sub-tree is traversed, then the root is visited, and finally the right sub-tree is traversed.&lt;/p&gt;

&lt;p&gt;One simple way of solving this problem is to start with an empty doubly linked list. While doing the in-order traversal of the binary tree, keep inserting each element output into the doubly linked list. But, if we look at the question carefully, the interviewer wants us to convert the binary tree to a doubly linked list &lt;strong&gt;in-place&lt;/strong&gt; i.e. we should not create new nodes for the doubly linked list.&lt;/p&gt;

&lt;p&gt;This problem can be solved recursively using a divide and conquer approach. Below is the algorithm specified.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Start with the root node and solve left and right sub-trees recursively&lt;/li&gt;
&lt;li&gt;  At each step, once left and right sub-trees have been processed:

&lt;ul&gt;
&lt;li&gt;  fuse output of left subtree with root to make the intermediate result&lt;/li&gt;
&lt;li&gt;  fuse intermediate result (built in the previous step) with output from the right sub-tree to make the final result of the current recursive call&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity Analysis:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(n)&lt;br&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: O(n)&lt;/p&gt;

&lt;p&gt;Recursive solution has &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;h&lt;/i&gt;)&lt;/code&gt; memory complexity as it will consume memory on the stack up to the height of binary tree &lt;code&gt;h&lt;/code&gt;. It will be &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;log n&lt;/i&gt;)&lt;/code&gt; for balanced tree and in worst case can be &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;BinaryTreeToDLL&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;BinaryTreeToDLL&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// first node in list&lt;/span&gt;
        &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;BinaryTreeToDLL&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// last node in list&lt;/span&gt;
        &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;PY&lt;/em&gt;
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;BinaryTreeToDLL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;    
    &lt;span class="c1"&gt;#Checks whether node is None    
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;    
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    

    &lt;span class="c1"&gt;#Convert left subtree to doubly linked list    
&lt;/span&gt;    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BinaryTreeToDLL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    

    &lt;span class="c1"&gt;#If list is empty, add node as head of the list    
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;    
        &lt;span class="c1"&gt;#Both head and tail will point to node    
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
    &lt;span class="c1"&gt;#Otherwise, add node to the end of the list    
&lt;/span&gt;    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    
        &lt;span class="c1"&gt;#node will be added after tail such that tail's right will point to node    
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
        &lt;span class="c1"&gt;#node's left will point to tail    
&lt;/span&gt;        &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
        &lt;span class="c1"&gt;#node will become new tail    
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    

    &lt;span class="c1"&gt;#Convert right subtree to doubly linked list    
&lt;/span&gt;    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BinaryTreeToDLL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.educative.io/m/convert-binary-tree-to-doubly-linked-list"&gt;www.educative.io&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;em&gt;Q15:&lt;/em&gt; Find similar elements from two Linked Lists and return the result as a Linked List ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists Hash Tables&lt;/p&gt;
&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;Consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="nx"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;list3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create a hash table or set&lt;/li&gt;
&lt;li&gt;Go through &lt;code&gt;list1&lt;/code&gt;, mark entries as you visit them. &lt;em&gt;&lt;code&gt;O(N)&lt;/code&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Go through &lt;code&gt;list2&lt;/code&gt;, mark entries (with different flag) as you visit them. &lt;em&gt;&lt;code&gt;O(M)&lt;/code&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Traverse hash table and find all the entries with both flags. Create &lt;code&gt;list3&lt;/code&gt; as you find entries. &lt;em&gt;&lt;code&gt;O(H)&lt;/code&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Total Complexity: &lt;em&gt;&lt;code&gt;O(N)+ O(M) + O(Max(N,H,M)) =&amp;gt; O(N)&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity Analysis:
&lt;/h3&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Time Complexity&lt;/strong&gt;: O(n)
&lt;/h2&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/4447675/interview-find-similar-elements-from-two-linked-lists-and-return-the-result-as"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q16:&lt;/em&gt; Floyd's Cycle Detect Algorithm: Explain how to find a starting node of a Cycle in a Linked List? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;This is &lt;strong&gt;Floyd's algorithm for cycle detection&lt;/strong&gt;. Once you've found a node that's part of a cycle, how does one find the start of the cycle?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the first part of Floyd's algorithm, the hare moves two steps for every step of the tortoise. If the tortoise and hare ever meet, there is a cycle, and the meeting point is part of the cycle, but not necessarily the first node in the cycle. &lt;/li&gt;
&lt;li&gt;Once tortoise and hare meet, let's put tortoise back to the beginning of the list and keep hare where they met (which is k steps away from the cycle beginning). The &lt;a href="http://hrishikeshmishra.com/how-to-find-the-starting-point-of-a-loop-in-a-linked-list/"&gt;hypothesis (see math explanation)&lt;/a&gt; is that if we let them move at the same speed (1 step for both), &lt;strong&gt;the first time they ever meet again will be the cycle beginning&lt;/strong&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another solution to mention is &lt;strong&gt;Hash Map&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Traverse the nodes list. &lt;/li&gt;
&lt;li&gt;For each node encountered, add it to the identity hash map&lt;/li&gt;
&lt;li&gt;If the node already existed in the identity map then the list has a circular link and the node which was prior to this conflict is known (either check before moving or keep the "last node")&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="nf"&gt;getLoopStartingPoint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;isLoopExists&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;slowPtr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fastPtr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isNull&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fastPtr&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isNull&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fastPtr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getNext&lt;/span&gt;&lt;span class="o"&gt;()))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;slowPtr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;slowPtr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getNext&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;fastPtr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fastPtr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getNext&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getNext&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slowPtr&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;fastPtr&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;isLoopExists&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;isLoopExists&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;slowPtr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slowPtr&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;fastPtr&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;slowPtr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;slowPtr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getNext&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;fastPtr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fastPtr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getNext&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;slowPtr&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/2936213/explain-how-finding-cycle-start-node-in-cycle-linked-list-work"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q17:&lt;/em&gt; Floyd's Cycle Detect Algorithm: Remove Cycle (Loop) from a Linked List ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Floyd's cycle detect algorithm, also called the tortoise and hare algorithm&lt;/strong&gt; as it involves using two pointers/references that move at different speeds, is one way of detecting the cycle. If there is a cycle, the two pointers (say &lt;code&gt;slow&lt;/code&gt; and &lt;code&gt;fast&lt;/code&gt;) will end up pointing to the same element after a finite number of steps. Interestingly, it can be proved that the element at which they meet will be &lt;em&gt;the same distance to the start of the **loop&lt;/em&gt;* (continuing to traverse the list in the same, forward direction) as the start of the loop is to the &lt;strong&gt;head&lt;/strong&gt; of the list*. That is, if the linear part of the list has &lt;code&gt;k&lt;/code&gt; elements, the two pointers will meet inside the loop of length &lt;code&gt;m&lt;/code&gt; at a point &lt;code&gt;m-k&lt;/code&gt; from the start of the loop or &lt;code&gt;k&lt;/code&gt; elements to the 'end' of the loop (of course, it's a loop so it has no 'end' - it's just the 'start' once again). And that gives us a way to find the start of the loop:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once a cycle has been detected, let &lt;code&gt;fast&lt;/code&gt; remain pointing to the element where the loop for the step above terminated but reset &lt;code&gt;slow&lt;/code&gt; so that it's pointing back to the head of the list. Now, move each pointer one element at a time. Since &lt;code&gt;fast&lt;/code&gt; began inside the loop, it will continue looping. After &lt;code&gt;k&lt;/code&gt; steps (equal to the distance of the start of the loop from the head of the list), &lt;code&gt;slow&lt;/code&gt; and &lt;code&gt;fast&lt;/code&gt; will meet again. This will give you a reference to the start of the loop.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It is now easy to set &lt;code&gt;slow&lt;/code&gt; (or &lt;code&gt;fast&lt;/code&gt;) to point to the element starting the loop and traverse the loop until &lt;code&gt;slow&lt;/code&gt; ends up pointing back to the starting element. At this point &lt;code&gt;slow&lt;/code&gt; is referencing the 'last' element list and it's next pointer can be set to &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;LinkedListNode&lt;/span&gt; &lt;span class="nf"&gt;lastNodeOfTheLoop&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LinkedListNode&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;LinkedListNode&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;LinkedListNode&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 

    &lt;span class="c1"&gt;// find meeting point using Tortoise and Hare algorithm&lt;/span&gt;
    &lt;span class="c1"&gt;// this is just Floyd's cycle detection algorithm&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
        &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
        &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Error check - there is no meeting point, and therefore no loop&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
    &lt;span class="c1"&gt;// Until both the references are one short of the common element which is the start of the loop&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
        &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
        &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// Now fast points to the start of the loop (inside cycle).&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;LinkedListNode&lt;/span&gt; &lt;span class="n"&gt;lastNodeOfTheLoop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;findStartOfLoop&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;lastNodeOfTheLoop&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// or lastNodeOfTheLoop.next = head;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/5607292/interview-remove-loop-in-linked-list-java/5607646#5607646"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q18:&lt;/em&gt; How to find &lt;em&gt;&lt;code&gt;Nth&lt;/code&gt;&lt;/em&gt; element from the end of a singly Linked List? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;Use &lt;strong&gt;lockstep&lt;/strong&gt; solution. The key to this algorithm is to set two pointers &lt;code&gt;p1&lt;/code&gt; and &lt;code&gt;p2&lt;/code&gt; apart by &lt;code&gt;n-1&lt;/code&gt; nodes initially so we want &lt;code&gt;p2&lt;/code&gt; to point to the &lt;code&gt;(n-1)th&lt;/code&gt; node from the start of the list then we move &lt;code&gt;p2&lt;/code&gt; till it reaches the &lt;code&gt;last&lt;/code&gt; node of the list. Once &lt;code&gt;p2&lt;/code&gt; reaches end of the list &lt;code&gt;p1&lt;/code&gt; will be pointing to the &lt;em&gt;&lt;code&gt;Nth&lt;/code&gt;&lt;/em&gt; node from the end of the list.&lt;/p&gt;

&lt;p&gt;This &lt;strong&gt;lockstep&lt;/strong&gt; approach will generally give better cache utilization, because a node hit by the front pointer may still be in cache when the rear pointer reaches it. In a language implementation using tracing garbage collection, this approach also avoids unnecessarily keeping the beginning (thus entire) list live for the duration of the operation. &lt;/p&gt;

&lt;p&gt;Another solution is to use &lt;strong&gt;circular buffer&lt;/strong&gt;. Keep a circular buffer of size &lt;code&gt;x&lt;/code&gt; and add the nodes to it as you traverse the list. When you reach the end of the list, the x'th one from the tail is equal to the next entry in the circular buffer. &lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity Analysis:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(n)&lt;br&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: O(n)&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;lockstep&lt;/strong&gt; approach moves all array elements in every step, resulting in a &lt;i&gt;&lt;code&gt;O(n + x&lt;sup&gt;2&lt;/sup&gt;)&lt;/code&gt;&lt;/i&gt; running time, whereas &lt;strong&gt;circular buffer **approach uses a circular array and runs in &lt;code&gt;O(n)&lt;/code&gt;. The **circular buffer&lt;/strong&gt; solution requires an additional &lt;code&gt;x&lt;/code&gt; in memory.&lt;/p&gt;
&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Function to return the nth node from the end of a linked list.&lt;/span&gt;
&lt;span class="c1"&gt;// Takes the head pointer to the list and n as input&lt;/span&gt;
&lt;span class="c1"&gt;// Returns the nth node from the end if one exists else returns NULL.&lt;/span&gt;
&lt;span class="nc"&gt;LinkedListNode&lt;/span&gt; &lt;span class="nf"&gt;nthToLast&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LinkedListNode&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// If list does not exist or if there are no elements in the list,return NULL&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// make pointers p1 and p2 point to the start of the list.&lt;/span&gt;
  &lt;span class="nc"&gt;LinkedListNode&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="nc"&gt;LinkedListNode&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// The key to this algorithm is to set p1 and p2 apart by n-1 nodes initially&lt;/span&gt;
  &lt;span class="c1"&gt;// so we want p2 to point to the (n-1)th node from the start of the list&lt;/span&gt;
  &lt;span class="c1"&gt;// then we move p2 till it reaches the last node of the list. &lt;/span&gt;
  &lt;span class="c1"&gt;// Once p2 reaches end of the list p1 will be pointing to the nth node &lt;/span&gt;
  &lt;span class="c1"&gt;// from the end of the list.&lt;/span&gt;

  &lt;span class="c1"&gt;// loop to move p2.&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
   &lt;span class="c1"&gt;// while moving p2 check if it becomes NULL, that is if it reaches the end&lt;/span&gt;
   &lt;span class="c1"&gt;// of the list. That would mean the list has less than n nodes, so its not &lt;/span&gt;
   &lt;span class="c1"&gt;// possible to find nth from last, so return NULL.&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
   &lt;span class="o"&gt;}&lt;/span&gt;
   &lt;span class="c1"&gt;// move p2 forward.&lt;/span&gt;
   &lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// at this point p2 is (n-1) nodes ahead of p1. Now keep moving both forward&lt;/span&gt;
  &lt;span class="c1"&gt;// till p2 reaches the last node in the list.&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="c1"&gt;// at this point p2 has reached the last node in the list and p1 will be&lt;/span&gt;
   &lt;span class="c1"&gt;// pointing to the nth node from the last..so return it.&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
 &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Circular buffer approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="nx"&gt;getNodeFromTail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Circular buffer with current index of of iteration.&lt;/span&gt;
  &lt;span class="nx"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="nx"&gt;int&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Place the current head in its position in the buffer and increment&lt;/span&gt;
    &lt;span class="c1"&gt;// the head and the index, continuing if necessary.&lt;/span&gt;
    &lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// If we haven't reached x nodes, return NULL, otherwise the next item in the&lt;/span&gt;
  &lt;span class="c1"&gt;// circular buffer holds the item from x heads ago.&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;NULL&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/2598348/how-to-find-nth-element-from-the-end-of-a-singly-linked-list"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q19:&lt;/em&gt; Implement Double Linked List from Stack with min complexity ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists Stacks&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;Imagine all items are organized into two stacks, draw them facing each other where face is where you put and peek:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&amp;lt;-&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now you can move 4 to 5:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&amp;lt;-&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and 3 to 4:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&amp;lt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and you can go backwards.&lt;/p&gt;

&lt;p&gt;You &lt;em&gt;may&lt;/em&gt; also need another two stacks so that you can traverse List via the tail:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="c1"&gt;// first pair of stacks&lt;/span&gt;
&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// second pair of stacks kept in reverse order&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;once current stack runs out&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;empty&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&amp;lt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="c1"&gt;// first pair of stacks is currently being traversed&lt;/span&gt;
&lt;span class="nx"&gt;empty&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&amp;lt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// second pair of stacks&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;simply switch to the second pair of stacks!&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Stack&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DoubleLinkedList&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Stack&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Stack&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;DoubleLinkedList&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
        &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Stack&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Stack&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;insertInBeginning&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
            &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pop&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;insertAtEnd&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
            &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pop&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;moveForward&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pop&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;moveBackward&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pop&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
            &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pop&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;peek&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
                &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pop&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pop&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;deleteFirst&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pop&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;peek&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;deleteLast&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pop&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;peek&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.careercup.com/question?id=5964523655659520"&gt;www.careercup.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q20:&lt;/em&gt; Merge two sorted singly Linked Lists without creating new nodes ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;You have two singly linked lists that are already sorted, you have to merge them and return a the head of the new list without creating any new extra nodes. The returned list should be sorted as well.&lt;/p&gt;

&lt;p&gt;The method signature is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="nx"&gt;MergeLists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="nx"&gt;list1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="nx"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Node class is below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
   &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="nx"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;Here is the algorithm on how to merge two sorted linked lists A and B:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt; &lt;span class="nx"&gt;not&lt;/span&gt; &lt;span class="nx"&gt;empty&lt;/span&gt; &lt;span class="nx"&gt;or&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt; &lt;span class="nx"&gt;not&lt;/span&gt; &lt;span class="nx"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="nx"&gt;remove&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt;
      &lt;span class="nx"&gt;insert&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="nx"&gt;into&lt;/span&gt; &lt;span class="nx"&gt;C&lt;/span&gt;
   &lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;
   &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="nx"&gt;remove&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt;
      &lt;span class="nx"&gt;insert&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="nx"&gt;into&lt;/span&gt; &lt;span class="nx"&gt;C&lt;/span&gt;
&lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here C will be the output list.&lt;/p&gt;

&lt;p&gt;There are two solutions possible:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Recursive approach&lt;/strong&gt; is&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Of the two nodes passed, keep the node with smaller value&lt;/li&gt;
&lt;li&gt;Point the smaller node's next to the next smaller value of remaining of the two linked lists&lt;/li&gt;
&lt;li&gt;Return the current node (which was smaller of the two nodes passed to the method)&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Iterative approach&lt;/strong&gt; is better for all practical purposes as scales as size of lists swells up:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Treat one of the sorted linked lists as list, and treat the other sorted list as 'bag of nodes'. &lt;/li&gt;
&lt;li&gt;Then we lookup for correct place for given node (from bag of nodes) in the list.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity Analysis:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(n)&lt;/p&gt;

&lt;p&gt;The run time complexity for the recursive and iterative solution here or the variant is &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;Recursive solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="nf"&gt;MergeLists&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;list2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;list2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;list2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MergeLists&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;list2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;list2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MergeLists&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;list2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Recursion should not be needed to avoid allocating a new node:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="nf"&gt;MergeLists&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;list2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;list2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;list2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;list2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;list2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;list1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;list2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;tmp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;list2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;list2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tmp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;list1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; 
  &lt;span class="n"&gt;list1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;list2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;PY&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;Recursive:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;merge_lists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;h1&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;h2&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;h2&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;h1&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;merge_lists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;h1&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;merge_lists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;h2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Iterative:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;merge_lists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;head2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;head1&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;head2&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;head2&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;head1&lt;/span&gt;

    &lt;span class="c1"&gt;# create dummy node to avoid additional checks in loop
&lt;/span&gt;    &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head1&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;head2&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;head1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;head2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# remember current low-node
&lt;/span&gt;            &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head1&lt;/span&gt;
            &lt;span class="c1"&gt;# follow -&amp;gt;next
&lt;/span&gt;            &lt;span class="n"&gt;head1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# remember current low-node
&lt;/span&gt;            &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head2&lt;/span&gt;
            &lt;span class="c1"&gt;# follow -&amp;gt;next
&lt;/span&gt;            &lt;span class="n"&gt;head2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;

        &lt;span class="c1"&gt;# only mutate the node AFTER we have followed -&amp;gt;next
&lt;/span&gt;        &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;          
        &lt;span class="c1"&gt;# and make sure we also advance the temp
&lt;/span&gt;        &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;

    &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head1&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;head2&lt;/span&gt;

    &lt;span class="c1"&gt;# return tail of dummy node
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/10707352/interview-question-merge-two-sorted-singly-linked-lists-without-creating-new-no"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q21:&lt;/em&gt; Sum two numbers represented as Linked Lists ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;You are given two &lt;strong&gt;non-empty&lt;/strong&gt; linked lists representing two non-negative integers. The digits are stored in &lt;strong&gt;reverse order&lt;/strong&gt; and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.&lt;/p&gt;

&lt;p&gt;You may assume the two numbers do not contain any leading zero, except the number 0 itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;
&lt;span class="n"&gt;Explanation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;342&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;465&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;807.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;You can add two numbers represented using LinkedLists in the same way you add two numbers by hand.&lt;/p&gt;

&lt;p&gt;Iterate over the linked lists, add their corresponding elements, keep a carry just the way you do while adding numbers by hand, add the carry-over from the previous addition to the current sum and so on.&lt;/p&gt;

&lt;p&gt;One of the trickiest parts of this problem is the issue of the carried number - if every pair of nodes added to a number less than 10, then there wouldn't be a concern of 'carrying' digits over to the next node. However, adding numbers like 4 and 6 produces a carry.&lt;/p&gt;

&lt;p&gt;If one list is longer than the other, we still will want to add the longer list's nodes to the solution, so we'll have to make sure we continue to check so long as the nodes aren't null. That means the while loop should keep going as long as list 1 isn't &lt;code&gt;null&lt;/code&gt; OR list 2 isn't &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="nf"&gt;addTwoNumbers&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;l1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;carry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l1&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;carry&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;resVal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l1&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;l1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;val&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l2&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;val&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;carry&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resVal&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;carry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;resVal&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;l1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;l1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;l1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;l2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;PY&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="c1"&gt;# @return a ListNode
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;addTwoNumbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;carry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;l1&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;carry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;v1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;l1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;v1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;
            &lt;span class="n"&gt;l1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;v2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;
            &lt;span class="n"&gt;l2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
        &lt;span class="n"&gt;carry&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;divmod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;v2&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;carry&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ListNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://dev.to/alisabaj/add-two-numbers-problems-how-to-sum-two-linked-lists-7oe"&gt;dev.to&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q22:&lt;/em&gt; When to use a Linked List over an Array/Array List? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Arrays Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Linked lists are preferable over arrays when:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;you need constant-time insertions/deletions from the list (such as in real-time computing where time predictability is absolutely critical)&lt;/li&gt;
&lt;li&gt;you don't know how many items will be in the list.  With arrays, you may need to re-declare and copy memory if the array grows too big&lt;/li&gt;
&lt;li&gt;you don't need random access to any elements&lt;/li&gt;
&lt;li&gt;you want to be able to insert items in the middle of the list (such as a priority queue)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Arrays are preferable when:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;you need indexed/random access to elements&lt;/li&gt;
&lt;li&gt;you know the number of elements in the array ahead of time so that you can allocate the  correct amount of memory for the array&lt;/li&gt;
&lt;li&gt;you need speed when iterating through all the elements in sequence.  You can use pointer math on the array to access each element, whereas you need to lookup the node based on the pointer for each element in linked list, which may result in page faults which may result in performance hits.&lt;/li&gt;
&lt;li&gt;memory is a concern.  Filled arrays take up less memory than linked lists.  Each element in the array is just the data.  Each linked list node requires the data as well as one (or more) pointers to the other elements in the linked list.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Array Lists (like those in .Net) give you the benefits of arrays, but dynamically allocate  resources for you so that you don't need to worry too much about list size and you can delete items at any index without any effort or re-shuffling elements around.  Performance-wise, arraylists are slower than raw arrays.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/393556/when-to-use-a-linked-list-over-an-array-array-list"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q23:&lt;/em&gt; Why does linked list delete and insert operation have complexity of &lt;em&gt;&lt;code&gt;O(1)&lt;/code&gt;&lt;/em&gt;? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;When you are inserting a node in the middle of a linked list, the &lt;em&gt;assumption taken&lt;/em&gt; is that you are already at the address where you have to insert the node. &lt;em&gt;Indexing&lt;/em&gt; is considered as a separate operation. So insertion is itself &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt;, but getting to that middle node is &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt;. Thus adding to a linked list &lt;em&gt;does not require a traversal&lt;/em&gt;, as long as you know a reference. &lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/42849486/why-does-linked-list-delete-and-insert-operation-have-complexity-of-o1-shoul"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q24:&lt;/em&gt; Find Merge (Intersection) Point of Two Linked Lists ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;Say, there are two lists of different lengths, &lt;strong&gt;merging at a point&lt;/strong&gt;; how do we know where the merging point is?&lt;/p&gt;

&lt;p&gt;Conditions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; We don't know the length&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZheY3DPy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.stack.imgur.com/xjn0S.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZheY3DPy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.stack.imgur.com/xjn0S.png" alt="Example of two merged linked lists."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;This arguably violates the "parse each list only once" condition, but &lt;strong&gt;implement the tortoise and hare algorithm&lt;/strong&gt; (used to find the merge point and cycle length of a cyclic list) so you start at List 1, and when you reach the NULL at the end you pretend it's a pointer to the beginning of List 2, thus creating the appearance of a cyclic list. The algorithm will then tell you exactly how far down List 1 the merge is. &lt;/p&gt;

&lt;p&gt;Algorithm steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make an interating pointer like this: 

&lt;ul&gt;
&lt;li&gt;it goes forward every time till the end, and then &lt;/li&gt;
&lt;li&gt;jumps to the beginning of the opposite list, and so on. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Create two of these, pointing to two heads. &lt;/li&gt;
&lt;li&gt;Advance each of the pointers by 1 every time, until they meet in intersection point (&lt;code&gt;IP&lt;/code&gt;).  This will happen after either one or two passes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To understand it count the number of nodes traveled from &lt;code&gt;head1-&amp;gt; tail1 -&amp;gt; head2 -&amp;gt; intersection point&lt;/code&gt; and &lt;code&gt;head2 -&amp;gt; tail2-&amp;gt; head1 -&amp;gt; intersection point&lt;/code&gt;. Both will be equal (Draw diff types of linked lists to verify this). Reason is both pointers have to travel same distances &lt;code&gt;head1-&amp;gt; IP + head2-&amp;gt;IP&lt;/code&gt; before reaching &lt;code&gt;IP&lt;/code&gt; again. So by the time it reaches &lt;code&gt;IP&lt;/code&gt;, both pointers will be equal and we have the merging point.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;CS&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;FindMergeNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;headA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;headB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;currentA&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;headA&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;currentB&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;headB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;//Do till the two nodes are the same&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;currentA&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;currentB&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="c1"&gt;//If you reached the end of one list start at the beginning of the other one&lt;/span&gt;
        &lt;span class="c1"&gt;//currentA&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;currentA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="n"&gt;currentA&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;headB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;currentA&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;currentA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;//currentB&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;currentB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="n"&gt;currentB&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;headA&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;currentB&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;currentB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;currentB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="nf"&gt;getIntersectionNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;headA&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;headB&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//boundary check&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;headA&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;headB&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;headA&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;headB&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;//if a &amp;amp; b have different len, then we will stop the loop after second iteration&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="c1"&gt;//for the end of first iteration, we just reset the pointer to the head of another linkedlist&lt;/span&gt;
        &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;headB&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;headA&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;    
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;PY&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# the idea is if you switch head, the possible difference between length would be countered. 
# On the second traversal, they either hit or miss. 
# if they meet, pa or pb would be the node we are looking for, 
# if they didn't meet, they will hit the end at the same iteration, pa == pb == None, return either one of them is the same,None
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# @param two ListNodes
&lt;/span&gt;    &lt;span class="c1"&gt;# @return the intersected ListNode
&lt;/span&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;getIntersectionNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headB&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;headA&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;headB&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

        &lt;span class="n"&gt;pa&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;headA&lt;/span&gt; &lt;span class="c1"&gt;# 2 pointers
&lt;/span&gt;        &lt;span class="n"&gt;pb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;headB&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;pa&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;pb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# if either pointer hits the end, switch head and continue the second traversal, 
&lt;/span&gt;            &lt;span class="c1"&gt;# if not hit the end, just move on to next
&lt;/span&gt;            &lt;span class="n"&gt;pa&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;headB&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;pa&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;pa&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
            &lt;span class="n"&gt;pb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;headA&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;pb&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;pb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;pa&lt;/span&gt; &lt;span class="c1"&gt;# only 2 ways to get out of the loop, they meet or the both hit the end=None
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/1594061/check-if-two-linked-lists-merge-if-so-where"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q25:&lt;/em&gt; Find the length of a Linked List which contains cycle (loop) ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;To find the length of loop apply &lt;strong&gt;Tortoise and Hare&lt;/strong&gt; algorithm:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Pointer &lt;strong&gt;A&lt;/strong&gt; advances one (1) node a time. &lt;/li&gt;
&lt;li&gt;Pointer &lt;strong&gt;B&lt;/strong&gt; advanced two (2) node a time. &lt;/li&gt;
&lt;li&gt;Start from head, they advanced at the same time until B hit &lt;code&gt;null&lt;/code&gt; (no loop) or &lt;strong&gt;A&lt;/strong&gt; and &lt;strong&gt;B&lt;/strong&gt; point to the same node.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Now, has &lt;strong&gt;A&lt;/strong&gt; advances only, &lt;strong&gt;A&lt;/strong&gt; would meet &lt;strong&gt;B&lt;/strong&gt; again. From that you can find the length of the loop say &lt;em&gt;L&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Start from head again, this time have a pointer &lt;strong&gt;C&lt;/strong&gt; advances &lt;em&gt;L&lt;/em&gt; nodes first, followed by pointer &lt;strong&gt;D&lt;/strong&gt; behind it, each advances one node at a time. &lt;/li&gt;
&lt;li&gt;When they meet, the number of nodes &lt;strong&gt;D&lt;/strong&gt; traveled &lt;em&gt;L0&lt;/em&gt; plus &lt;em&gt;L&lt;/em&gt; is equal to the &lt;strong&gt;length of the linked list with a loop&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.farenda.tutorials.algorithms.lists&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FloydCycleDetector&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;cycle&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tortoise&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hare&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;detect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;tortoise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hare&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;cycle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;detectCycle&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cycle&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Found cycle."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;position&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;findPosition&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cycleLength&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"No cycle."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;position&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;hasCycle&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cycle&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;length&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;position&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;detectCycle&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tortoise&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;tortoise&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hare&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;hare&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"(%d, %d),"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tortoise&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hare&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;tortoise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tortoise&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;hare&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hare&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tortoise&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;hare&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"turtle(%d) == hare(%d)%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                        &lt;span class="n"&gt;tortoise&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hare&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;findPosition&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;tortoise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"(%d, %d),"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tortoise&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hare&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tortoise&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;hare&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;tortoise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tortoise&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;hare&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hare&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;cycleLength&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;hare&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hare&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tortoise&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;hare&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;PY&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;nodes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;rest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;next_next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;meet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;steps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;steps&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;meet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;meet_result&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;meet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;meet_count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;meet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_cycle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;cycle_meet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;meet_result&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next_next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;cycle_root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;meet_result&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cycle_meet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;cycle_length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;meet_count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cycle_root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cycle_root&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cycle_meet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cycle_root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cycle_length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.careercup.com/question?id=68277"&gt;www.careercup.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q26:&lt;/em&gt; Given a singly Linked List, determine if it is a Palindrome ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;Could you do it in &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt; time and &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;1&lt;/i&gt;)&lt;/code&gt; space?&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;Solution: is &lt;strong&gt;Reversed first half **== **Second half&lt;/strong&gt;? &lt;/p&gt;

&lt;p&gt;Let's look to an example &lt;code&gt;[1, 1, 2, 1]&lt;/code&gt;. In the beginning, set two pointers &lt;code&gt;fast&lt;/code&gt; and &lt;code&gt;slow&lt;/code&gt; starting at the head.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 -&amp;gt; 1 -&amp;gt; 2 -&amp;gt; 1 -&amp;gt; null
sf

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(1) &lt;strong&gt;Move:&lt;/strong&gt; &lt;code&gt;fast&lt;/code&gt; pointer goes to the end, and &lt;code&gt;slow&lt;/code&gt; goes to the middle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 -&amp;gt; 1 -&amp;gt; 2 -&amp;gt; 1 -&amp;gt; null
          s          f

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(2) &lt;strong&gt;Reverse:&lt;/strong&gt; the right half is reversed, and &lt;code&gt;slow&lt;/code&gt; pointer becomes the 2nd head.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 -&amp;gt; 1    null &amp;lt;- 2 &amp;lt;- 1
h                      s

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(3) &lt;strong&gt;Compare:&lt;/strong&gt; run the two pointers &lt;code&gt;head&lt;/code&gt; and &lt;code&gt;slow&lt;/code&gt; together and compare.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 -&amp;gt; 1    null &amp;lt;- 2 &amp;lt;- 1
     h            s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isPalindrome&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// odd nodes: let right half smaller&lt;/span&gt;
        &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;val&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;PY&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Note: while comparing the two halves, restore the list to its original 
# state by reversing the first half back.
&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;isPalindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;rev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
    &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
        &lt;span class="n"&gt;rev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
    &lt;span class="n"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;
    &lt;span class="n"&gt;isPali&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;rev&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;isPali&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;isPali&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;rev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;
        &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rev&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rev&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
        &lt;span class="n"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;isPali&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/4202364/single-linked-list-interview-question"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q27:&lt;/em&gt; How is it possible to do Binary Search on a Doubly-Linked List in &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt; time? ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Searching Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;I've heard that it's possible to implement binary search over a doubly-linked list in  &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt; time. Accessing a random element of a doubly-linked list takes  &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt; time, and binary search accesses &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;log n&lt;/i&gt;)&lt;/code&gt; different elements, so shouldn't the runtime be  &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;log n&lt;/i&gt;)&lt;/code&gt;) instead?&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;It's technically correct to say that the runtime of binary search on a doubly-linked list is &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;log n&lt;/i&gt;)&lt;/code&gt;), but that's not a tight upper bound. Using a slightly better implementation of binary search and a more clever analysis, it's possible to get binary search to run in time &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The basic idea behind binary search is the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the list is empty, the element being searched for doesn't exist.&lt;/li&gt;
&lt;li&gt;Otherwise:

&lt;ul&gt;
&lt;li&gt;Look at the middle element.&lt;/li&gt;
&lt;li&gt;If it matches the element in question, return it.&lt;/li&gt;
&lt;li&gt;If it's bigger than the element in question, discard the back half of the list.&lt;/li&gt;
&lt;li&gt;If it's smaller than the element in question, discard the front half of the list.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A &lt;strong&gt;naive implementation&lt;/strong&gt; of binary search on a doubly-linked list would work by computing the indices to look up on each iteration (just like in the array case), then access each one by starting at the front of the list and scanning forward the appropriate number of steps. This is indeed very slow.&lt;/p&gt;

&lt;p&gt;We can &lt;strong&gt;speed this up&lt;/strong&gt; by a factor of &lt;em&gt;&lt;code&gt;Θ(log n)&lt;/code&gt;&lt;/em&gt; by being more clever with our approach. The reason the previous algorithm is slow is that every time we need to look up an element, we start the search from the beginning of the array. However, we don't need to do this. After looking up the middle element the first time, we're already in the middle of the array, and we know that the next lookup we're going to make will be either at position n / 4 or 3n / 4, which is only distance n / 4 from where we left off (compared to n / 4 or 3n / 4 if we start from the beginning of the array). What if we just trekked out from our stopping position (n / 2) to the next position, rather than restarting at the front of the list?&lt;/p&gt;

&lt;p&gt;Here's our new algorithm. Begin by scanning to the middle of the array, which requires n / 2 steps. Then, determine whether to visit the element at the middle of the first half of the array or the middle of the second half of the array. Reaching there from position n / 2 only requires n / 4 total steps. From there, going to the midpoint of the quarter of the array containing the element takes only n / 8 steps, and going from there to the midpoint of the eighth of the array containing the element takes only n / 16 steps, etc. This means that the total number of steps made is given by&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;n / 2 + n / 4 + n / 8 + n / 16 + ...&lt;/p&gt;

&lt;p&gt;= n (1/2 + 1/4 + 1/8 + ...)&lt;/p&gt;

&lt;p&gt;≤ n&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This follows from the fact that the sum of the infinite geometric series 1/2 + 1/4 + 1/8 + ... is 1. Therefore, the total work done in the worst case only &lt;em&gt;&lt;code&gt;Θ(n)&lt;/code&gt;&lt;/em&gt;, which is much better than the &lt;em&gt;&lt;code&gt;Θ(n log n)&lt;/code&gt;&lt;/em&gt; worst case from before.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/19554431/how-is-it-possible-to-do-binary-search-on-a-doubly-linked-list-in-on-time"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q28:&lt;/em&gt; How to apply Binary Search &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;log n&lt;/i&gt;)&lt;/code&gt; on a sorted Linked List? ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Searching Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;Sorted singly linked list is given and we have to search one element from this list. Time complexity should not be more than &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;log n&lt;/i&gt;)&lt;/code&gt;. How would you apply binary search on a sorted linked list?&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;It is certainly &lt;strong&gt;not possible&lt;/strong&gt; with a plain singly-linked list. As linked list does not provide &lt;strong&gt;random access&lt;/strong&gt; if we try to apply binary search algorithm it will reach &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt; as we need to find length of the list and go to the middle. To avoid this problem you need to use &lt;strong&gt;skip list&lt;/strong&gt;. &lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.fullstack.cafe"&gt;FullStack.Cafe&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q29:&lt;/em&gt; How to recursively reverse a Linked List? ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists Recursion&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;Start from the bottom up by asking and answering tiny questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is the reverse of &lt;code&gt;null&lt;/code&gt; (the empty list)? &lt;code&gt;null&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;What is the reverse of a one element list? the element.&lt;/li&gt;
&lt;li&gt;What is the reverse of an n element list? the reverse of the rest of the list followed by the first element.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;P.S.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;How to understand that part?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think about the origin link list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1-&amp;gt;2-&amp;gt;3-&amp;gt;4-&amp;gt;5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now assume that the last node has been reversed. Just like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1-&amp;gt;2-&amp;gt;3-&amp;gt;4&amp;lt;-5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this time you are at the node 3 , you want to change &lt;code&gt;3-&amp;gt;4&lt;/code&gt; to &lt;code&gt;3&amp;lt;-4&lt;/code&gt; , means let &lt;code&gt;3-&amp;gt;next-&amp;gt;next&lt;/code&gt; = &lt;code&gt;3&lt;/code&gt; (as &lt;code&gt;3-&amp;gt;next&lt;/code&gt; is &lt;code&gt;4&lt;/code&gt; and &lt;code&gt;4-&amp;gt;next&lt;/code&gt; = &lt;code&gt;3&lt;/code&gt; is to reverse it)&lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity Analysis:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(n)&lt;br&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: O(n)&lt;/p&gt;

&lt;p&gt;Assume that &lt;code&gt;n&lt;/code&gt; is the list's length, the time complexity is &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt;. The extra space comes from implicit stack space due to recursion. The recursion could go up to &lt;code&gt;n&lt;/code&gt; levels deep then space complexity is &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;JS&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;reverseList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;pre&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pre&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;tmp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;tmp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// if head is null or only one node, it's reverse of itself.&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// reverse the sub-list leaving the head node.&lt;/span&gt;
  &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// head.next still points to the last element of reversed sub-list.&lt;/span&gt;
  &lt;span class="c1"&gt;// so move the head to end.&lt;/span&gt;
  &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// point last node to nil, (get rid of cycles)&lt;/span&gt;
  &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;PY&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;reverseList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;  &lt;span class="c1"&gt;# Recursive
&lt;/span&gt;        &lt;span class="s"&gt;"""
        :type head: ListNode
        :rtype: ListNode
        """&lt;/span&gt;     
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;
        &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reverseList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;
        &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/354875/reversing-a-linked-list-in-java-recursively"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q30:&lt;/em&gt; How would you traverse a Linked List in &lt;i&gt;&lt;code&gt;O(n&lt;sup&gt;1/2&lt;/sup&gt;)&lt;/code&gt;&lt;/i&gt;? ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Traversal&lt;/em&gt; more efficient than &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt; is not possible, since "traversal" requires accessing &lt;em&gt;each node in turn&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;It is possible to make &lt;em&gt;random access&lt;/em&gt; faster than &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt; though, by maintaining a second linked list that retains links to intermediate nodes; insertion, deletion, and appending cost will go up though, due to the increased complexity of maintenance of the second list.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/6472082/how-would-you-traverse-a-linked-list-in-on0-5"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q31:&lt;/em&gt; Why is Merge sort preferred over Quick sort for sorting Linked Lists? ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists Sorting&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Quicksort depends on being able to &lt;em&gt;index&lt;/em&gt; (or &lt;em&gt;random&lt;/em&gt; access) into an array or similar structure. When that's possible, it's hard to beat Quicksort.&lt;/li&gt;
&lt;li&gt;You can't index directly into a linked list very quickly.  That is, if &lt;code&gt;myList&lt;/code&gt; is a linked list, then &lt;code&gt;myList[x]&lt;/code&gt;, were it possible to write such syntax, would involve starting at the head of the list and following the first &lt;code&gt;x&lt;/code&gt; links.  That would have to be done twice for every comparison that Quicksort makes, and that would get expensive real quick.&lt;/li&gt;
&lt;li&gt;Same thing on disk -  Quicksort would have to seek and read every item it wants to compare.&lt;/li&gt;
&lt;li&gt;Merge sort is faster in these situations because it reads the items sequentially, typically making &lt;code&gt;log(n)&lt;/code&gt; passes over the data.  There is much less I/O involved, and much less time spent following links in a linked list.&lt;/li&gt;
&lt;li&gt;Quicksort is fast when the data fits into memory and can be addressed directly.  Mergesort is faster when data won't fit into memory or when it's expensive to get to an item.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/5222730/why-is-merge-sort-preferred-over-quick-sort-for-sorting-linked-lists/5223158#5223158"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q32:&lt;/em&gt; Do you know any better than than Floyd's algorithm for cycle detection? ☆☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;Richard Brent described an &lt;strong&gt;alternative cycle detection algorithm&lt;/strong&gt;, which is pretty much like the hare and the tortoise &lt;strong&gt;Floyd's cycle&lt;/strong&gt; except that, the slow node here doesn't move, but is later "teleported" to the position of the fast node at fixed intervals. &lt;/p&gt;

&lt;p&gt;Brent claims that his algorithm is &lt;strong&gt;24%&lt;/strong&gt; to &lt;strong&gt;36%&lt;/strong&gt; faster than the Floyd's cycle algorithm. &lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity Analysis:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(n)&lt;br&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: O(n)&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;hasLoop&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;taken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;taken&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;taken&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;taken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;limit&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// equivalent to limit *= 2;&lt;/span&gt;
            &lt;span class="n"&gt;slow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fast&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// teleporting the turtle (to the hare's position) &lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/2663115/how-to-detect-a-loop-in-a-linked-list"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Thanks 🙌 for reading and good luck on your interview!&lt;/em&gt; &lt;em&gt;Please share this article with your fellow Devs if you like it!&lt;/em&gt; &lt;em&gt;Check more FullStack Interview Questions &amp;amp; Answers on 👉 &lt;a href="https://www.fullstack.cafe"&gt;www.fullstack.cafe&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>linkedlist</category>
      <category>career</category>
      <category>interview</category>
      <category>coding</category>
    </item>
    <item>
      <title>12 Recursion Interview Questions (SOLVED) Devs Have To Nail</title>
      <dc:creator>Alex 👨🏼‍💻FullStack.Cafe</dc:creator>
      <pubDate>Mon, 05 Oct 2020 13:10:08 +0000</pubDate>
      <link>https://forem.com/fullstackcafe/12-recursion-interview-questions-solved-devs-have-to-nail-3h0g</link>
      <guid>https://forem.com/fullstackcafe/12-recursion-interview-questions-solved-devs-have-to-nail-3h0g</guid>
      <description>&lt;p&gt;Recursion is the method of a function calling itself, usually with a smaller dataset. Recursion is important because it often allows a breathtakingly simple algorithmic solution to certain problems that would otherwise be practically unobtainable with an iterative algorithm. Come along to check 11 Most Common Recursion Interview Questions with solutions for your next coding interview. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Originally published on &lt;a href="https://www.fullstack.cafe/blog/recursion-interview-questions"&gt;FullStack.Cafe - Kill Your Next Tech Interview&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q1:&lt;/em&gt; Can you convert this Recursion function into a loop? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Recursion&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;Can you convert this recursion function into a loop?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;A&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;Any recursive function can be made to iterate (into a loop) but you need to use a stack yourself to keep the state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;A&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/531668/which-recursive-functions-cannot-be-rewritten-using-loops"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q2:&lt;/em&gt; How Dynamic Programming is different from Recursion and Memoization? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Dynamic Programming Recursion&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memoization&lt;/strong&gt; is when you store previous results of a function call (a real function always returns the same thing, given the same inputs). It doesn't make a difference for algorithmic complexity before the results are stored.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recursion&lt;/strong&gt; is the method of a function calling itself, usually with a smaller dataset. Since most recursive functions can be converted to similar iterative functions, this doesn't make a difference for algorithmic complexity either.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic programming&lt;/strong&gt; is the process of solving easier-to-solve sub-problems and building up the answer from that. Most DP algorithms will be in the running times between a Greedy algorithm (if one exists) and an exponential (enumerate all possibilities and find the best one) algorithm.

&lt;ul&gt;
&lt;li&gt;DP algorithms could be implemented with recursion, but they don't have to be.&lt;/li&gt;
&lt;li&gt;DP algorithms can't be sped up by memoization, since each sub-problem is only ever solved (or the "solve" function called) once.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/1065433/what-is-dynamic-programming"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q3:&lt;/em&gt; Return the N-th value of the Fibonacci sequence Recursively ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Fibonacci Series&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;Recursive solution looks pretty simple (see code).&lt;/p&gt;

&lt;p&gt;Let’s look at the diagram that will help you understand what’s going on here with the rest of our code. Function fib is called with argument 5:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qpoM8jEp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1400/1%2ALNBBacuaBFOVZXUV6VgEEg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qpoM8jEp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1400/1%2ALNBBacuaBFOVZXUV6VgEEg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically our &lt;strong&gt;fib&lt;/strong&gt; function will continue to recursively call itself creating more and more branches of the tree until it hits the base case, from which it will start summing up each branch’s return values bottom up, until it finally sums them all up and returns an integer equal to 5.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity Analysis:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(2^n)&lt;/p&gt;

&lt;p&gt;In case of recursion the solution take &lt;strong&gt;exponential&lt;/strong&gt; time, that can be explained by the fact that the size of the tree exponentially grows when n increases. So for every additional element in the Fibonacci sequence we get an increase in function calls. Big O in this case is equal to &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;2&lt;/i&gt;&lt;sup&gt;n&lt;/sup&gt;)&lt;/code&gt;. Exponential Time complexity denotes an algorithm whose growth doubles with each addition to the input data set. &lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;JS&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;PY&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;F&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://medium.com/developers-writing/fibonacci-sequence-algorithm-in-javascript-b253dc7e320e"&gt;medium.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q4:&lt;/em&gt; What is a good example of Recursion (other than generating a Fibonacci sequence)? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Recursion&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;There are some:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The binary tree search&lt;/li&gt;
&lt;li&gt;Check for a palyndrome&lt;/li&gt;
&lt;li&gt;Finding factorial&lt;/li&gt;
&lt;li&gt;Traversing the folder hierarchy of a directory tree as part of a file system&lt;/li&gt;
&lt;li&gt;Towers of Hanoi&lt;/li&gt;
&lt;li&gt;Merge sort &lt;/li&gt;
&lt;li&gt;Catalan numbers&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/4945128/what-is-a-good-example-of-recursion-other-than-generating-a-fibonacci-sequence"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q5:&lt;/em&gt; What is the difference between Backtracking and Recursion? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Backtracking Recursion&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Recursion&lt;/strong&gt; describes the calling of the &lt;em&gt;same function&lt;/em&gt; that you are in. The typical example of a recursive function is the factorial. You always need a condition that makes recursion stop (base case). &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backtracking&lt;/strong&gt; is when the algorithm makes an opportunistic decision&lt;sup&gt;&lt;em&gt;&lt;/em&gt;&lt;/sup&gt;, which may come up to be wrong. If the decision was wrong then the backtracking algorithm restores the state before the decision. It builds candidates for the solution and abandons those which cannot fulfill the conditions. A typical example for a task to solve would be the &lt;em&gt;Eight Queens Puzzle&lt;/em&gt;. Backtracking is also commonly used within &lt;em&gt;Neuronal Networks&lt;/em&gt;. Many times backtracking is not implemented recursively. If backtracking uses recursion its called **Recursive Backtracking*&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;P.S. &lt;strong&gt;Opportunistic decision&lt;/strong&gt; making refers to a process where a person or group assesses alternative actions made possible by the favorable convergence of immediate circumstances recognized &lt;strong&gt;without&lt;/strong&gt; reference to any &lt;strong&gt;general plan&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.quora.com/What-is-the-difference-between-backtracking-and-recursion-What-are-some-examples"&gt;www.quora.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q6:&lt;/em&gt; Convert a Binary Tree to a Doubly Linked List ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Binary Tree Linked Lists Recursion Divide &amp;amp; Conquer&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;This can be achieved by traversing the tree in the &lt;strong&gt;in-order&lt;/strong&gt; manner that is, left the &lt;code&gt;child -&amp;gt; root -&amp;gt;right&lt;/code&gt; node. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eaHRK4Ab--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.techiedelight.com/wp-content/uploads/Inorder-Traversal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eaHRK4Ab--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.techiedelight.com/wp-content/uploads/Inorder-Traversal.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In an in-order traversal, first the left sub-tree is traversed, then the root is visited, and finally the right sub-tree is traversed.&lt;/p&gt;

&lt;p&gt;One simple way of solving this problem is to start with an empty doubly linked list. While doing the in-order traversal of the binary tree, keep inserting each element output into the doubly linked list. But, if we look at the question carefully, the interviewer wants us to convert the binary tree to a doubly linked list &lt;strong&gt;in-place&lt;/strong&gt; i.e. we should not create new nodes for the doubly linked list.&lt;/p&gt;

&lt;p&gt;This problem can be solved recursively using a divide and conquer approach. Below is the algorithm specified.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Start with the root node and solve left and right sub-trees recursively&lt;/li&gt;
&lt;li&gt;  At each step, once left and right sub-trees have been processed:

&lt;ul&gt;
&lt;li&gt;  fuse output of left subtree with root to make the intermediate result&lt;/li&gt;
&lt;li&gt;  fuse intermediate result (built in the previous step) with output from the right sub-tree to make the final result of the current recursive call&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity Analysis:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(n)&lt;br&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: O(n)&lt;/p&gt;

&lt;p&gt;Recursive solution has &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;h&lt;/i&gt;)&lt;/code&gt; memory complexity as it will consume memory on the stack up to the height of binary tree &lt;code&gt;h&lt;/code&gt;. It will be &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;log n&lt;/i&gt;)&lt;/code&gt; for balanced tree and in worst case can be &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;BinaryTreeToDLL&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;BinaryTreeToDLL&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// first node in list&lt;/span&gt;
        &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;BinaryTreeToDLL&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// last node in list&lt;/span&gt;
        &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;prev&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;PY&lt;/em&gt;
&lt;/h4&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;BinaryTreeToDLL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;    
    &lt;span class="c1"&gt;#Checks whether node is None    
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;    
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    

    &lt;span class="c1"&gt;#Convert left subtree to doubly linked list    
&lt;/span&gt;    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BinaryTreeToDLL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    

    &lt;span class="c1"&gt;#If list is empty, add node as head of the list    
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;    
        &lt;span class="c1"&gt;#Both head and tail will point to node    
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
    &lt;span class="c1"&gt;#Otherwise, add node to the end of the list    
&lt;/span&gt;    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    
        &lt;span class="c1"&gt;#node will be added after tail such that tail's right will point to node    
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
        &lt;span class="c1"&gt;#node's left will point to tail    
&lt;/span&gt;        &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
        &lt;span class="c1"&gt;#node will become new tail    
&lt;/span&gt;        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    

    &lt;span class="c1"&gt;#Convert right subtree to doubly linked list    
&lt;/span&gt;    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BinaryTreeToDLL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.educative.io/m/convert-binary-tree-to-doubly-linked-list"&gt;www.educative.io&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;em&gt;Q7:&lt;/em&gt; Explain what is DFS (Depth First Search) algorithm for a Graph and how does it work? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Backtracking Graph Theory Recursion&lt;/p&gt;
&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Depth First Traversal&lt;/strong&gt; or &lt;strong&gt;Depth First Search&lt;/strong&gt; is a &lt;strong&gt;edge based&lt;/strong&gt; &lt;em&gt;recursive&lt;/em&gt; algorithm for traversing (visiting) all the vertices of a graph or tree data structure using a &lt;strong&gt;stack&lt;/strong&gt;. The purpose of the algorithm is to mark each vertex as visited while &lt;em&gt;avoiding&lt;/em&gt; cycles. DFS traverse/visit each vertex exactly once and each edge is inspected exactly twice. DFS is a genuinely recursive algorithm that uses stack for &lt;em&gt;backtracking&lt;/em&gt; purposes, not for storing the vertex discovery "front" (as is the case in BFS).&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;DFS&lt;/strong&gt; algorithm works as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start by putting any one of the graph's vertices on top of a &lt;strong&gt;stack&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Take the top item of the stack and add it to the &lt;strong&gt;visited list&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to the top of the stack.&lt;/li&gt;
&lt;li&gt;Keep repeating steps 2 and 3 until the stack is empty.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;DFS example step-by-step&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Considering A as the starting vertex which is explored and stored in the stack.&lt;/li&gt;
&lt;li&gt;B successor vertex of A is stored in the stack.&lt;/li&gt;
&lt;li&gt;Vertex B have two successors E and F, among them alphabetically E is explored first and stored in the stack.&lt;/li&gt;
&lt;li&gt;The successor of vertex E, i.e., G is stored in the stack.&lt;/li&gt;
&lt;li&gt;Vertex G have two connected vertices, and both are already visited, so G is popped out from the stack.&lt;/li&gt;
&lt;li&gt;Similarly, E s also removed.&lt;/li&gt;
&lt;li&gt;Now, vertex B is at the top of the stack, its another node(vertex) F is explored and stored in the stack.&lt;/li&gt;
&lt;li&gt;Vertex F has two successor C and D, between them C is traversed first and stored in the stack.&lt;/li&gt;
&lt;li&gt;Vertex C only have one predecessor which is already visited, so it is removed from the stack.&lt;/li&gt;
&lt;li&gt;Now vertex D connected to F is visited and stored in the stack.&lt;/li&gt;
&lt;li&gt;As vertex D doesn’t have any unvisited nodes, therefore D is removed.&lt;/li&gt;
&lt;li&gt;Similarly, F, B and A are also popped.&lt;/li&gt;
&lt;li&gt;The generated output is – A, B, E, G, F, C, D.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NZ76F-fo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://techdifferences.com/wp-content/uploads/2017/10/Untitledeeg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NZ76F-fo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://techdifferences.com/wp-content/uploads/2017/10/Untitledeeg.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://techdifferences.com/difference-between-bfs-and-dfs.html"&gt;techdifferences.com&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;em&gt;Q8:&lt;/em&gt; Binet's formula: How to calculate Fibonacci numbers without Recursion or Iteration?  ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Fibonacci Series&lt;/p&gt;
&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;There is actually a simple mathematical formula called &lt;strong&gt;Binet's formula&lt;/strong&gt; for computing the nth Fibonacci number, which does not require the calculation of the preceding numbers:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uHNnMuAp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://latex.artofproblemsolving.com/8/6/d/86d486c560727727342090b432e23ba85ac098b1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uHNnMuAp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://latex.artofproblemsolving.com/8/6/d/86d486c560727727342090b432e23ba85ac098b1.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It features the &lt;strong&gt;Golden Ratio (φ)&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;φ&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;or&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tq2j0jCj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1200/1%2Auildf6DFEAKIwx6cUWXxKg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tq2j0jCj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1200/1%2Auildf6DFEAKIwx6cUWXxKg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The iterative approach scales linearly, while the formula approach is basically constant time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity Analysis:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(log n)&lt;br&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: O(log n)&lt;/p&gt;

&lt;p&gt;Assuming that the primitive mathematical operations (+, -, * and /) are &lt;code&gt;O(1)&lt;/code&gt; you can use this result to compute the &lt;code&gt;n&lt;/code&gt;th Fibonacci number in &lt;code&gt;O(log n)&lt;/code&gt; time (&lt;code&gt;O(log n)&lt;/code&gt; because of the exponentiation in the formula).&lt;/p&gt;
&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;CS&lt;/em&gt;
&lt;/h4&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;inverseSqrt5&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="n"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;phi&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="cm"&gt;/* should use 
   const double inverseSqrt5 = 0.44721359549995793928183473374626
   const double phi = 1.6180339887498948482045868343656
*/&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;Fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;phi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;inverseSqrt5&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="m"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;JS&lt;/em&gt;
&lt;/h4&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sqrt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fibCalc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(((&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;
    &lt;span class="nx"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(((&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt; &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
     &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;pha&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sqrt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;phb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;sqrt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;sqrt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="o"&gt;)((&lt;/span&gt;&lt;span class="n"&gt;pha&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;phb&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
 &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;PY&lt;/em&gt;
&lt;/h4&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fib_formula&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;golden_ratio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
    &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;golden_ratio&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;golden_ratio&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="http://pi3.sites.sheffield.ac.uk/tutorials/week-1-fibonacci"&gt;pi3.sites.sheffield.ac.uk&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;em&gt;Q9:&lt;/em&gt; Calculate n-th Fibonacci number using Tail Recursion ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Fibonacci Series&lt;/p&gt;
&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;In traditional recursion, you perform the recursive call, take the return value of that call and calculate the results. You get the results of your calculation only until you’ve returned from every recursive call, requiring a lot of space on the call stack:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="err"&gt;—&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="err"&gt;—&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In &lt;strong&gt;tail recursion&lt;/strong&gt;, you calculate the results first, and pass the results of your current call onto the next recursive call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, when you are ready to calculate the next call, you don’t need the current stack frame. You are calculating the results and passing them as parameters to your next call. No more worries about stack overflow!&lt;br&gt;
This tail recursive solution is constant &lt;code&gt;O(n)&lt;/code&gt; time and constant &lt;code&gt;O(n)&lt;/code&gt; space complexity.&lt;/p&gt;
&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;
&lt;h4&gt;
  
  
  &lt;em&gt;JS&lt;/em&gt;
&lt;/h4&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;FIB_0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;FIB_1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;calcFibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;FIB_0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;FIB_1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;calcFibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FIB_1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FIB_0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;calcFibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;previous&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;fibPrevious&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;fibPreviousMinusOne&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;previous&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;fibCurrent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fibPrevious&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fibPreviousMinusOne&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// If you want, print here / memoize for future calls&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fibCurrent&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;calcFibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fibCurrent&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fibPrevious&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://medium.com/@johanna.fulghum/write-the-fibonacci-sequence-in-every-computational-complexity-9adf5ef12775"&gt;medium.com&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;em&gt;Q10:&lt;/em&gt; How to recursively reverse a Linked List? ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Linked Lists Recursion&lt;/p&gt;
&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;Start from the bottom up by asking and answering tiny questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is the reverse of &lt;code&gt;null&lt;/code&gt; (the empty list)? &lt;code&gt;null&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;What is the reverse of a one element list? the element.&lt;/li&gt;
&lt;li&gt;What is the reverse of an n element list? the reverse of the rest of the list followed by the first element.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;P.S.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;How to understand that part?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Think about the origin link list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1-&amp;gt;2-&amp;gt;3-&amp;gt;4-&amp;gt;5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now assume that the last node has been reversed. Just like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1-&amp;gt;2-&amp;gt;3-&amp;gt;4&amp;lt;-5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And this time you are at the node 3 , you want to change &lt;code&gt;3-&amp;gt;4&lt;/code&gt; to &lt;code&gt;3&amp;lt;-4&lt;/code&gt; , means let &lt;code&gt;3-&amp;gt;next-&amp;gt;next&lt;/code&gt; = &lt;code&gt;3&lt;/code&gt; (as &lt;code&gt;3-&amp;gt;next&lt;/code&gt; is &lt;code&gt;4&lt;/code&gt; and &lt;code&gt;4-&amp;gt;next&lt;/code&gt; = &lt;code&gt;3&lt;/code&gt; is to reverse it)&lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity Analysis:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(n)&lt;br&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: O(n)&lt;/p&gt;

&lt;p&gt;Assume that &lt;code&gt;n&lt;/code&gt; is the list's length, the time complexity is &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt;. The extra space comes from implicit stack space due to recursion. The recursion could go up to &lt;code&gt;n&lt;/code&gt; levels deep then space complexity is &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;JS&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;reverseList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;pre&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pre&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;tmp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;tmp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// if head is null or only one node, it's reverse of itself.&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// reverse the sub-list leaving the head node.&lt;/span&gt;
  &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// head.next still points to the last element of reversed sub-list.&lt;/span&gt;
  &lt;span class="c1"&gt;// so move the head to end.&lt;/span&gt;
  &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// point last node to nil, (get rid of cycles)&lt;/span&gt;
  &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;PY&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;reverseList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;  &lt;span class="c1"&gt;# Recursive
&lt;/span&gt;        &lt;span class="s"&gt;"""
        :type head: ListNode
        :rtype: ListNode
        """&lt;/span&gt;     
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;
        &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reverseList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;
        &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/354875/reversing-a-linked-list-in-java-recursively"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q11:&lt;/em&gt; How to use Memoization for N-th Fibonacci number?  ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Dynamic Programming Fibonacci Series Recursion&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;Why? Any problems you may face with that solution?&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;Yes. It's called &lt;strong&gt;Memoization&lt;/strong&gt;. &lt;strong&gt;Memoization&lt;/strong&gt; is an optimization technique used primarily to speed up computer programs by &lt;em&gt;storing the results of expensive function calls&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Let’s look at the diagram that will help you understand what’s going on here with the rest of our code. Function fib is called with argument 5. Can you see that we calculate the &lt;code&gt;fib(2)&lt;/code&gt; results 3(!) times?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AYFlGGFS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ik.imagekit.io/jclivyrqwx/85218649-ba646d00-b3ce-11ea-81a9-f8fccf10f155_yEHz3Zx4I.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AYFlGGFS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://ik.imagekit.io/jclivyrqwx/85218649-ba646d00-b3ce-11ea-81a9-f8fccf10f155_yEHz3Zx4I.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically, if we just store the value of each index in a &lt;strong&gt;hash&lt;/strong&gt;, we will avoid the computational time of that value for the next &lt;code&gt;N&lt;/code&gt; times. This change will increase the space complexity of our new algorithm to &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt; but will dramatically decrease the time complexity to &lt;code&gt;2N&lt;/code&gt; which will resolve to linear time since 2 is a constant &lt;code&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There’s just &lt;strong&gt;one problem&lt;/strong&gt;: With an infinite series, the memo array will have unbounded growth. Eventually, you’re going to run into heap size limits, and that will crash the JS engine. No worries though. With Fibonacci, you’ll run into the maximum exact JavaScript integer size first, which is &lt;code&gt;9007199254740991&lt;/code&gt;. That’s over 9 quadrillion, which is a big number, but Fibonacci isn’t impressed. Fibonacci grows &lt;strong&gt;fast&lt;/strong&gt;. You’ll burst that barrier after generating only 79 numbers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity Analysis:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(n)&lt;br&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: O(n)&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;em&gt;JS&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;memo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;Java&lt;/em&gt;
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;fibMemo&lt;/span&gt;&lt;span class="o"&gt;[];&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;fibByRecMemo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fibMemo&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fibMemo&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fibMemo&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fibMemo&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fibByRecMemo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fibByRecMemo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fibMemo&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fibMemo&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://medium.com/developers-writing/fibonacci-sequence-algorithm-in-javascript-b253dc7e320e"&gt;medium.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q12:&lt;/em&gt; Is it always possible to write a non-recursive form for every Recursive function? ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Recursion&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;When you use a function &lt;strong&gt;recursively&lt;/strong&gt;, the compiler takes care of stack management for you, which is what makes recursion possible. Anything you can do recursively, you can do by managing a stack yourself (for indirect recursion, you just have to make sure your different functions share that stack).&lt;/p&gt;

&lt;p&gt;A simple formal proof is to show that both &lt;em&gt;µ recursion&lt;/em&gt; (general recursive function) and a non-recursive calculus such as GOTO are both &lt;strong&gt;Turing complete&lt;/strong&gt;. Since all Turing complete calculi are strictly equivalent in their expressive power, all recursive functions can be implemented by the non-recursive Turing-complete calculus.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/531668/which-recursive-functions-cannot-be-rewritten-using-loops"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Thanks 🙌 for reading and good luck on your interview!&lt;/em&gt; &lt;em&gt;Please share this article with your fellow Devs if you like it!&lt;/em&gt; &lt;em&gt;Check more FullStack Interview Questions &amp;amp; Answers on 👉 &lt;a href="https://www.fullstack.cafe"&gt;www.fullstack.cafe&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>recursion</category>
      <category>fibonacci</category>
      <category>career</category>
      <category>interview</category>
    </item>
    <item>
      <title>12 Concurrency Interview Questions To Know Before System Design Interview </title>
      <dc:creator>Alex 👨🏼‍💻FullStack.Cafe</dc:creator>
      <pubDate>Mon, 05 Oct 2020 12:46:51 +0000</pubDate>
      <link>https://forem.com/fullstackcafe/12-concurrency-interview-questions-to-know-before-system-design-interview-2cil</link>
      <guid>https://forem.com/fullstackcafe/12-concurrency-interview-questions-to-know-before-system-design-interview-2cil</guid>
      <description>&lt;p&gt;Concurrency and multithreading are some of the most advanced topics brought up in interviews. Concurrency happens when two or more tasks can start, run, and complete in overlapping time periods. It doesn't necessarily mean they'll ever both be running at the same instant. Follow along to clarify 12 Most Common Concurrency Interview Questions To Know Before Your Next System Design Interview.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Originally published on &lt;a href="https://www.fullstack.cafe/blog/concurrency-interview-questions"&gt;FullStack.Cafe - Kill Your Next Tech Interview&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q1:&lt;/em&gt; Explain the difference between Asynchronous and Parallel programming? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Concurrency&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;When you run something &lt;strong&gt;asynchronously&lt;/strong&gt; it means it is non-blocking, you execute it without waiting for it to complete and carry on with other things. &lt;br&gt;
&lt;strong&gt;Parallelism&lt;/strong&gt; means to run multiple things at the same time, in parallel. Parallelism works well when you can separate tasks into independent pieces of work. Async and Callbacks are generally a way (tool or mechanism) to express concurrency i.e. a set of entities possibly talking to each other and sharing resources. &lt;/p&gt;

&lt;p&gt;Take for example rendering frames of a 3D animation. To render the animation takes a long time so if you were to launch that render from within your animation editing software you would make sure it was running &lt;em&gt;asynchronously&lt;/em&gt; so it didn't lock up your UI and you could continue doing other things. Now, each frame of that animation can also be considered as an individual task. If we have multiple CPUs/Cores or multiple machines available, we can render multiple frames in &lt;em&gt;parallel&lt;/em&gt; to speed up the overall workload.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/6133574/how-to-articulate-the-difference-between-asynchronous-and-parallel-programming#:~:text=13%20Answers&amp;amp;text=When%20you%20run%20something%20asynchronously,into%20independent%20pieces%20of%20work."&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q2:&lt;/em&gt; What is a Deadlock? ☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Concurrency&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;lock&lt;/strong&gt; occurs when multiple processes try to access the same resource at the same time. One process loses out and must wait for the other to finish.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;deadlock&lt;/strong&gt; occurs when the waiting process is still holding on to another resource that the first needs before it can finish.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--i4Mfp7z5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://encrypted-tbn0.gstatic.com/images%3Fq%3Dtbn%253AANd9GcRLSs8hUDWsPkEkRsoaLbmQF2FHkNL9jPXjeg%26usqp%3DCAU" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i4Mfp7z5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://encrypted-tbn0.gstatic.com/images%3Fq%3Dtbn%253AANd9GcRLSs8hUDWsPkEkRsoaLbmQF2FHkNL9jPXjeg%26usqp%3DCAU" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, an example:&lt;/p&gt;

&lt;p&gt;Resource A and resource B are used by process X and process Y&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  X starts to use A.&lt;/li&gt;
&lt;li&gt;  X and Y try to start using B&lt;/li&gt;
&lt;li&gt;  Y 'wins' and gets B first&lt;/li&gt;
&lt;li&gt;  now Y needs to use A&lt;/li&gt;
&lt;li&gt;  A is locked by X, which is waiting for Y
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Thread 1               Thread 2

Lock1-&amp;gt;Lock();         Lock2-&amp;gt;Lock();
WaitForLock2();        WaitForLock1();   &amp;lt;-- Oops!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The best way to avoid deadlocks is to avoid having processes cross over in this way. Reduce the need to lock anything as much as you can. In databases avoid making lots of changes to different tables in a single transaction, avoid triggers and switch to optimistic/dirty/nolock reads as much as possible.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/34512/what-is-a-deadlock"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q3:&lt;/em&gt; Explain Deadlock to 5 years old ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Concurrency&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;Jack and Jill share a sparse kitchen that has only one of everything. They both want to make a sandwich at the same time. Each needs a slice of bread and each needs a knife, so they both go to get the loaf of bread and the knife from the kitchen.&lt;/p&gt;

&lt;p&gt;Jack gets the knife first, while Jill gets the loaf of bread first. Now Jack tries to find the loaf of bread and Jill tries to find the knife, but both find that what they need to finish the task is already in use. If they both decide to wait until what they need is no longer in use, they will wait for each other forever. &lt;strong&gt;Deadlock&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/2143873/how-to-explain-the-deadlock-better"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q4:&lt;/em&gt; Is there any difference between a Binary Semaphore and Mutex? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Concurrency&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;mutex&lt;/strong&gt; (or Mutual Exclusion Semaphores) is a &lt;strong&gt;locking mechanism&lt;/strong&gt; used to synchronize &lt;em&gt;access&lt;/em&gt; to a resource. Only one task (can be a thread or process based on OS abstraction) can acquire the mutex. It means there will be ownership associated with mutex, and only the owner can release the lock (mutex).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Semaphore&lt;/strong&gt; (or Binary Semaphore) is &lt;strong&gt;signaling mechanism&lt;/strong&gt; (“I am done, you can carry on” kind of signal). For example, if you are listening songs (assume it as one task) on your mobile and at the same time your friend called you, an interrupt will be triggered upon which an interrupt service routine (ISR) will signal the call processing task to wakeup. A binary semaphore is NOT protecting a resource from access. Semaphores are more suitable for some synchronization problems like producer-consumer.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  A &lt;strong&gt;mutex&lt;/strong&gt; can be released only by &lt;strong&gt;the thread that had acquired it&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;  A &lt;strong&gt;binary semaphore&lt;/strong&gt; can be signaled &lt;strong&gt;by any thread&lt;/strong&gt; (or process).&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/62814/difference-between-binary-semaphore-and-mutex"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q5:&lt;/em&gt; What is a Race Condition? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Concurrency&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;A race condition is a situation on concurrent programming where two concurrent threads or processes compete for a resource and the resulting final state depends on who gets the resource first.&lt;/p&gt;

&lt;p&gt;Because the thread scheduling algorithm can swap between threads at any time, you don't know the order in which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are "racing" to access/change the data. &lt;/p&gt;

&lt;p&gt;Problems often occur when one thread does a "check-then-act" (e.g. "check" if the value is X, then "act" to do something that depends on the value being X) and another thread does something to the value in between the "check" and the "act". E.g:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (x == 5) // The "Check"
{
   y = x * 2; // The "Act"

   // If another thread changed x in between "if (x == 5)" and "y = x * 2" above,
   // y will not be equal to 10.
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The point being, y could be 10, or it could be anything, depending on whether another thread changed x in between the check and act. You have no real way of knowing.&lt;/p&gt;

&lt;p&gt;In order to prevent race conditions from occurring, you would typically put a lock (Mutex or Semaphores) around the shared data to ensure only one thread can access the data at a time. This would mean something like this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Obtain lock for x
if (x == 5)
{
   y = x * 2; // Now, nothing can change x until the lock is released. 
              // Therefore y = 10
}
// release lock for x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/34510/what-is-a-race-condition"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q6:&lt;/em&gt; What is the meaning of the term “Thread-Safe”? ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Concurrency&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;Does it mean that two threads can't change the underlying data simultaneously? Or does it mean that the given code segment will run with predictable results when multiple threads are executing that code segment?&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Thread-safe&lt;/strong&gt; code is code that will work even if many Threads are executing it simultaneously within the same process. This often means, that internal data-structures or operations that should run uninterrupted are protected against different modifications at the same time.&lt;/li&gt;
&lt;li&gt;Another definition may be like - a class is** thread-safe** if it behaves correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment, and with no additional synchronisation or other coordination on the part of the calling code.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/261683/what-is-the-meaning-of-the-term-thread-safe"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q7:&lt;/em&gt; Write a function that guarantees to never return the same value twice ☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Concurrency Brain Teasers&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;Write a function that is guaranteed to never return the same value twice. Assume that this function will be accessed by multiple machines &lt;em&gt;concurrently&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Just toss a simple (threadsafe) counter behind some communication endpoint:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MinValue&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nf"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Interlocked&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Increment&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ref&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Let interviewer be the one that follow up with those problems:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Does it need to survive reboots? &lt;/li&gt;
&lt;li&gt;What about hard drive failure? &lt;/li&gt;
&lt;li&gt;What about nuclear war? &lt;/li&gt;
&lt;li&gt;Does it need to be random? &lt;/li&gt;
&lt;li&gt;How random?

&lt;ol&gt;
&lt;li&gt;If they made it clear that it has to be unique across reboots and across different machines, I'd give them a function that calls into the standard mechanism for creating a new GUID, whatever that happens to be in the language being used. This is basically the problem that guids solve. Producing a duplicate Guid, no matter its format, is the most difficult lottery on the planet. &lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://softwareengineering.stackexchange.com/questions/263595/function-guaranteed-to-never-return-the-same-value-twice"&gt;softwareengineering.stackexchange.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q8:&lt;/em&gt; Two customers add a product to the basket in the same time whose the stock was only one (1). What will you do?  ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Concurrency Software Architecture&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;What is the best practice to manage the case where two customers add in the same time a product whose the stock was only 1?&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;There is no perfect answer for this question and all depends on details but you have some options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;As a first 'defense line' I would try to avoid such situations at all, by simply not selling out articles that low if any possible.&lt;/li&gt;
&lt;li&gt;You reserve an item for the customer for a fix time say (20 minutes) after they have added it to the basket – after they time they have to recheck the stock level or start again. This is often used to ticket to events or airline seats&lt;/li&gt;
&lt;li&gt;For small jobs the best way is to do a final check right before the payment, when the order is actually placed. In worst case you have to tell you customer that you where running out of stock right now and offer alternatives or discount coupon.&lt;/li&gt;
&lt;li&gt;Try to fulfil both orders later - just cause you don't have stock right now, doesn't mean you can't find it in an emergency. If you can't then someone has to contact the user who lucked out and apologise.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Side note:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The solution to do the double check when adding something to the basket isn't very good. People put a lot in baskets without ever actually placing an order. So this may block this article for a certain period of time.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://softwareengineering.stackexchange.com/questions/133925/best-practice-to-manage-concurrency-into-a-basket-in-a-e-commerce-website"&gt;softwareengineering.stackexchange.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q9:&lt;/em&gt; What is Starvation? ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Concurrency&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Starvation&lt;/strong&gt; describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. This happens when shared resources are made unavailable for long periods by "greedy" threads or threads with more "prioroty". For example, suppose an object provides a synchronized method that often takes a long time to return. If one thread invokes this method frequently, other threads that also need frequent synchronized access to the same object will often be blocked.&lt;/p&gt;

&lt;p&gt;One more real live example may be this one. Imagine you're in a queue to purchase food at a restaurant, for which pregnant women &lt;em&gt;have priority&lt;/em&gt;. And there's just a whole bunch of pregnant women arriving all the time. You'll soon be starving.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/starvelive.html"&gt;docs.oracle.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q10:&lt;/em&gt; What's the difference between Deadlock and Livelock? ☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Concurrency&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In concurrent computing, a &lt;strong&gt;deadlock&lt;/strong&gt; is a state in which each member of a group of actions, is waiting for some other member to release a lock&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;livelock&lt;/strong&gt; is similar to a deadlock, except that the states of the processes involved in the livelock constantly change with regard to one another, none progressing. Livelock is a special case of &lt;strong&gt;resource starvation&lt;/strong&gt;; the general definition only states that a specific process is not progressing.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Livelock is a risk with some algorithms that detect and recover from deadlock. If more than one process takes action, the deadlock detection algorithm can be repeatedly triggered. This can be avoided by ensuring that only one process (chosen randomly or by priority) takes action.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="http://en.wikipedia.org/wiki/Deadlock"&gt;en.wikipedia.org&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q11:&lt;/em&gt; What happens if you have a "race condition" on the lock itself? ☆☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Concurrency&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;p&gt;For example, two different threads, perhaps in the same application, but running on different processors, try to acquire a lock at the exact same time. What happens then? Is it impossible, or just plain unlikely?&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution:
&lt;/h3&gt;

&lt;p&gt;Have a "race condition" on the lock itself is impossible. It can be implemented in different ways, e.g., via the &lt;em&gt;Compare-and-swap&lt;/em&gt; where the hardware guarantees sequential execution. It can get a bit complicated in presence of multiple cores or even multiple sockets and needs a complicated protocol (MESI protocol) between the cores, but this is all taken care of in hardware.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compare-and-swap (CAS)&lt;/strong&gt; is an atomic instruction used in multithreading to achieve synchronization. It compares the contents of a memory location with a given value and, only if they are the same, modifies the contents of that memory location to a new given value. This is done as a single atomic operation. The atomicity guarantees that the new value is calculated based on up-to-date information; if the value had been updated by another thread in the meantime, the write would fail. &lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://softwareengineering.stackexchange.com/questions/245189/what-prevents-a-race-condition-on-a-lock"&gt;softwareengineering.stackexchange.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Q12:&lt;/em&gt; What is the difference between Race Condition and Data Races? Are they the same? ☆☆☆☆☆
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; Concurrency&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;data race&lt;/strong&gt; occurs when 2 instructions from different threads access the same memory location, at least one of these accesses is a write and there is no synchronization that is mandating any particular order among these accesses.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;race condition&lt;/strong&gt; is a &lt;em&gt;semantic error&lt;/em&gt;. It is a flaw that occurs in the timing or the ordering of events that leads to erroneous program behavior. Many race conditions can be caused by data races, but this is not necessary.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Race Condition and Data Races are not the same thing. They are not a subset of one another. They are also neither the necessary, nor the sufficient condition for one another.&lt;/p&gt;




&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/11276259/are-data-races-and-race-condition-actually-the-same-thing-in-context-of-conc/18049303#18049303"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Thanks 🙌 for reading and good luck on your interview!&lt;/em&gt; &lt;em&gt;Please share this article with your fellow Devs if you like it!&lt;/em&gt; &lt;em&gt;Check more FullStack Interview Questions &amp;amp; Answers on 👉 &lt;a href="https://www.fullstack.cafe"&gt;www.fullstack.cafe&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>concurrency</category>
      <category>career</category>
    </item>
    <item>
      <title>25 Redis Interview Questions (ANSWERED) for Full-Stack Devs</title>
      <dc:creator>Alex 👨🏼‍💻FullStack.Cafe</dc:creator>
      <pubDate>Wed, 23 Sep 2020 04:58:39 +0000</pubDate>
      <link>https://forem.com/fullstackcafe/25-redis-interview-questions-answered-for-full-stack-devs-3b1g</link>
      <guid>https://forem.com/fullstackcafe/25-redis-interview-questions-answered-for-full-stack-devs-3b1g</guid>
      <description>&lt;p&gt;Redis offers a great deal of support for developing efficient caching mechanisms. It takes only a couple of minutes to implement a cache mechanism and get it working with any application. Follow along to learn 25 most common Redis Interview Questions and Answers for your next senior web developer interview.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Originally published on &lt;a href="https://www.fullstack.cafe/blog/redis-interview-questions"&gt;FullStack.Cafe - Don't F*ck Up Your Next Tech Interview&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Q1: What is Redis?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Redis, which stands for &lt;strong&gt;Re&lt;/strong&gt;mote &lt;strong&gt;Di&lt;/strong&gt;ctionary &lt;strong&gt;S&lt;/strong&gt;erver, is a fast, open-source, in-memory key-value data store for use as a database, cache, message broker, and queue. &lt;/p&gt;

&lt;p&gt;You can run &lt;strong&gt;atomic&lt;/strong&gt; operations, like appending to a string; incrementing the value in a hash; pushing an element to a list; computing set intersection, union and difference; or getting the member with highest ranking in a sorted set.&lt;/p&gt;

&lt;p&gt;In order to achieve performance, Redis works with an in-memory dataset. Depending on your use case, you can persist it either by dumping the dataset to disk every once in a while, or by appending each command to a log. Persistence can be optionally disabled, if you just need a feature-rich, networked, in-memory cache.&lt;/p&gt;

&lt;p&gt;Redis is a popular choice for caching, session management, gaming, leaderboards, real-time analytics, geospatial, ride-hailing, chat/messaging, media streaming, and pub/sub apps.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://redis.io/topics/introduction"&gt;redis.io&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q2: Is Redis just a cache?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Like a cache Redis offers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  in memory key-value storage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But unlike a cash Redis:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Supports multiple datatypes (strings, hashes, lists, sets, sorted sets, bitmaps, and hyperloglogs)&lt;/li&gt;
&lt;li&gt;It provides an ability to store cache data into physical storage (if needed).&lt;/li&gt;
&lt;li&gt;Supports pub-sub model&lt;/li&gt;
&lt;li&gt;Redis cache provides replication for high availability (master/slave)&lt;/li&gt;
&lt;li&gt;Supports ultra-fast lua-scripts. Its execution time equals to C commands execution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/10137857/is-redis-just-a-cache"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q3: Does Redis persist data?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Redis supports so-called "snapshots". This means that it will do a complete copy of whats in memory at some points in time (e.g. every full hour). When you lose power between two snapshots, you will lose the data from the time between the last snapshot and the crash (doesn't have to be a power outage..). Redis trades data safety versus performance, like most NoSQL-DBs do.&lt;/p&gt;

&lt;p&gt;Redis saves data in one of the following cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;automatically from time to time&lt;/li&gt;
&lt;li&gt;when you manually call &lt;code&gt;BGSAVE&lt;/code&gt; command&lt;/li&gt;
&lt;li&gt;when redis is shutting down&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But data in redis is not really persistent, because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;crash of redis process means losing all changes since last save&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;BGSAVE&lt;/code&gt; operation can only be performed if you have enough free RAM (the amount of extra RAM is equal to the size of redis DB)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/25328317/does-redis-persist-data"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q4: What's the advantage of Redis vs using memory?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Redis is a &lt;em&gt;remote&lt;/em&gt; data structure server. It is certainly slower than just storing the data in local memory (since it involves socket roundtrips to fetch/store the data). However, it also brings some interesting properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Redis can be accessed by all the processes of your applications, possibly running on several nodes (something local memory cannot achieve).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Redis memory storage is quite efficient, and done in a separate process. If the application runs on a platform whose memory is garbage collected (node.js, java, etc ...), it allows handling a much bigger memory cache/store. In practice, very large heaps do not perform well with garbage collected languages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Redis can persist the data on disk if needed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Redis is a bit more than a simple cache: it provides various data structures, various item eviction policies, blocking queues, pub/sub, atomicity, Lua scripting, etc ...&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Redis can replicate its activity with a master/slave mechanism in order to implement high-availability.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Basically, if you need your application to &lt;strong&gt;scale on several nodes&lt;/strong&gt; sharing the same data, then something like Redis (or any other remote key/value store) will be required.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/19477821/redis-cache-vs-using-memory-directly"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q5: When to use Redis Lists data type?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Redis lists&lt;/strong&gt; are ordered collections of strings. They are optimized for inserting, reading, or removing values from the top or bottom (aka: left or right) of the list.&lt;/p&gt;

&lt;p&gt;Redis provides many commands for leveraging lists, including commands to push/pop items, push/pop between lists, truncate lists, perform range queries, etc.&lt;/p&gt;

&lt;p&gt;Lists make great durable, atomic, queues. These work great for job queues, logs, buffers, and many other use cases.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/10558465/memcached-vs-redis"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q6: When to use Redis Sets?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Sets&lt;/strong&gt; are unordered collections of unique values. They are optimized to let you quickly check if a value is in the set, quickly add/remove values, and to measure overlap with other sets.&lt;/p&gt;

&lt;p&gt;These are great for things like access control lists, unique visitor trackers, and many other things. Most programming languages have something similar (usually called a Set). This is like that, only distributed.&lt;/p&gt;

&lt;p&gt;Redis provides several commands to manage sets. Obvious ones like adding, removing, and checking the set are present. So are less obvious commands like popping/reading a random item and commands for performing unions and intersections with other sets.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/10558465/memcached-vs-redis"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q7: When to use Redis over MongoDB?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It depends on kind of dev team you are and your application needs but some notes when to use Redis is probably a good idea:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Caching&lt;/strong&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Caching using MongoDB simply doesn't make a lot of sense. It would be too slow.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  If you have enough time to think about your DB design. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can't simply throw in your documents into Redis. You have to think of the way you in which you want to store and organize your data. One example are hashes in Redis. They are quite different from "traditional", nested objects, which means you'll have to rethink the way you store nested documents. One solution would be to store a reference inside the hash to another hash (something like &lt;em&gt;key: [id of second hash]&lt;/em&gt;). Another idea would be to store it as JSON, which seems counter-intuitive to most people with a *SQL-background. Redis's non-traditional approach requires more effort to learn, but greater flexibility.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;If you need &lt;strong&gt;really&lt;/strong&gt; high performance.&lt;/p&gt;

&lt;p&gt;Beating the performance Redis provides is nearly impossible. Imagine you database being as fast as your cache. That's what it feels like using Redis as a &lt;em&gt;real&lt;/em&gt; database.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If you don't care &lt;em&gt;that&lt;/em&gt; much about scaling.&lt;/p&gt;

&lt;p&gt;Scaling Redis is not as hard as it used to be. For instance, you could use a kind of proxy server in order to distribute the data among multiple Redis instances. Master-slave replication is not &lt;em&gt;that&lt;/em&gt; complicated, but distributing you keys among multiple Redis-instances needs to be done on the application site (e.g. using a hash-function, Modulo etc.). Scaling MongoDB by comparison is much simpler.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/10558465/memcached-vs-redis"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q8: How are Redis pipelining and transaction different?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Pipelining&lt;/strong&gt; is primarily a network optimization. It essentially means the client buffers up a bunch of commands and ships them to the server in one go. The commands are not guaranteed to be executed in a transaction. The benefit here is saving network round trip time for every command.&lt;/p&gt;

&lt;p&gt;Redis is single threaded so an &lt;em&gt;individual&lt;/em&gt; command is always atomic, but two given commands from different clients can execute in sequence, alternating between them for example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Multi/exec&lt;/strong&gt;, however, ensures no other clients are executing commands in between the commands in the multi/exec sequence.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/29327544/pipelining-vs-transaction-in-redis"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q9: Does Redis support transactions?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;MULTI&lt;/strong&gt;, &lt;strong&gt;EXEC&lt;/strong&gt;, &lt;strong&gt;DISCARD&lt;/strong&gt; and &lt;strong&gt;WATCH&lt;/strong&gt; are the foundation of transactions in Redis. They allow the execution of a group of commands in a single step, with two important guarantees:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All the commands in a transaction are serialized and executed sequentially. It can never happen that a request issued by another client is served &lt;strong&gt;in the middle&lt;/strong&gt; of the execution of a Redis transaction. This guarantees that the commands are executed as a single isolated operation.&lt;/li&gt;
&lt;li&gt;Either all of the commands or none are processed, so a Redis transaction is also &lt;strong&gt;atomic&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/29327544/pipelining-vs-transaction-in-redis"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q10: How does Redis handle multiple threads (from different clients) updating the same data structure in Redis?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Redis is actually &lt;strong&gt;single-threaded&lt;/strong&gt;, which is how every command is guaranteed to be atomic. While one command is executing, &lt;strong&gt;no other command will run&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A single-threaded program can definitely provide concurrency at the I/O level by using an I/O (de)multiplexing mechanism and an event loop (which is what Redis does). The fact that Redis operations are atomic is simply a consequence of the single-threaded event loop. The interesting point is atomicity is provided at no extra cost (it does not require synchronization between threads).&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/17099222/are-redis-operations-on-data-structures-thread-safe"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q11: What is the difference between Redis replication and sharding?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sharding&lt;/strong&gt;, also known as partitioning, is splitting the data up by key. Sharding is useful to increase performance, reducing the hit and memory load on any one resource. &lt;/li&gt;
&lt;li&gt;While &lt;strong&gt;replication&lt;/strong&gt;, also known as mirroring, is to copy all data. Replication is useful for getting a high availability of reads. If you read from multiple replicas, you will also reduce the hit rate on all resources, but the memory requirement for all resources remains the same.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Any key-value store (of which Redis is only one example) supports sharding, though certain cross-key functions will no longer work. Redis supports replication out of the box.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/2139443/redis-replication-and-redis-sharding-cluster-difference"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q12: When to use Redis Hashes data type?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hashes are sort of like a key value store within a key value store. They map between string fields and string values. Field-&amp;gt;value maps using a hash are slightly more space efficient than key-&amp;gt;value maps using regular strings.&lt;/p&gt;

&lt;p&gt;Hashes are useful as a &lt;em&gt;namespace&lt;/em&gt;, or when you want to logically group many keys. With a hash you can grab all the members efficiently, expire all the members together, delete all the members together, etc. Great for any use case where you have several key/value pairs that need to grouped.&lt;/p&gt;

&lt;p&gt;One example use of a hash is for storing user profiles between applications. A redis hash stored with the user ID as the key will allow you to store as many bits of data about a user as needed while keeping them stored under a single key. The advantage of using a hash instead of serializing the profile into a string is that you can have different applications read/write different fields within the user profile without having to worry about one app overriding changes made by others (which can happen if you serialize stale data).&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/10558465/memcached-vs-redis"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q13: Explain a use case for Sorted Set in Redis
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Sorted Sets&lt;/strong&gt; are also collections of unique values. These ones, as the name implies, are ordered. They are ordered by a score, then lexicographically.&lt;/p&gt;

&lt;p&gt;This data type is optimized for quick lookups by score. Getting the highest, lowest, or any range of values in between is extremely fast.&lt;/p&gt;

&lt;p&gt;If you add users to a sorted set along with their high score, you have yourself a perfect leader-board. As new high scores come in, just add them to the set again with their high score and it will re-order your leader-board. Also great for keeping track of the last time users visited and who is active in your application.&lt;/p&gt;

&lt;p&gt;Storing values with the same score causes them to be ordered lexicographically (think alphabetically). This can be useful for things like auto-complete features.&lt;/p&gt;

&lt;p&gt;Many of the sorted set commands are similar to commands for sets, sometimes with an additional score parameter. Also included are commands for managing scores and querying by score.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/10558465/memcached-vs-redis"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q14: What is Pipelining in Redis and when to use one?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you have many redis commands you want to execute you can use &lt;strong&gt;pipelining&lt;/strong&gt; to send them to redis all-at-once instead of one-at-a-time.&lt;/p&gt;

&lt;p&gt;Normally when you execute a command, each command is a separate request/response cycle. With pipelining, Redis can buffer several commands and execute them all at once, responding with all of the responses to all of your commands in a single reply.&lt;/p&gt;

&lt;p&gt;This can allow you to achieve even greater throughput on &lt;em&gt;bulk importing&lt;/em&gt; or other actions that involve lots of commands.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/10558465/memcached-vs-redis"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q15: How would you efficiently store JSON in Redis?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are many ways to store an array of Objects in Redis:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Store the entire object as JSON-encoded string in a single key and keep track of all Objects using a set (or list, if more appropriate):
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;INCR&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;
&lt;span class="nx"&gt;SET&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{"name":"Fred","age":25}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;SADD&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Use when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you use most of the fields on most of your accesses.&lt;/li&gt;
&lt;li&gt;If there is variance on possible keys&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Store each Object's properties in a Redis hash:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;INCR&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;
&lt;span class="nx"&gt;HMSET&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Fred&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;
&lt;span class="nx"&gt;SADD&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Use when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you use just single fields on most of your accesses.&lt;/li&gt;
&lt;li&gt;If you always know which fields are available&lt;/li&gt;
&lt;li&gt;If there are a lot of fields in the Object&lt;/li&gt;
&lt;li&gt;Your Objects are not nested with other Objects&lt;/li&gt;
&lt;li&gt;You tend to only access a small subset of fields at a time

&lt;ol&gt;
&lt;li&gt;Store each Object as a JSON string in a Redis hash:
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;INCR&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;
&lt;span class="nx"&gt;HMSET&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{"name":"Fred","age":25}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Use when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;really care about not polluting the main key namespace

&lt;ol&gt;
&lt;li&gt;Store each property of each Object in a dedicated key:
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;INCR&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;
&lt;span class="nx"&gt;SET&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}:&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Fred&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;SET&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}:&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;
&lt;span class="nx"&gt;SADD&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Use when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;almost never preferred unless the property of the Object needs to have specific TTL or something &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Option 4 is generally not preferred. Options 1 and 2 are very similar, and they are both pretty common. Option 1 (generally speaking) may be most preferable because it allows you to store more complicated Objects (with multiple layers of nesting, etc.) Option 3 is used when you really care about not polluting the main key namespace (i.e. you don't want there to be a lot of keys in your database and you don't care about things like TTL, key sharding, or whatever).&lt;/p&gt;

&lt;p&gt;Also as a rule of the thumb, go for the option which requires fewer queries on most of your use cases.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/16375188/redis-strings-vs-redis-hashes-to-represent-json-efficiency"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q16: How can I exploit multiple CPU/cores for Redis?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First Redis is single threaded but it's not very frequent that CPU becomes your bottleneck with Redis, as usually Redis is either memory or network bound. &lt;/p&gt;

&lt;p&gt;For instance, using pipelining Redis running on an average Linux system can deliver even 1 million requests per second, so if your application mainly uses &lt;code&gt;O(N)&lt;/code&gt; or &lt;code&gt;O(log n)&lt;/code&gt; commands, it is hardly going to use too much CPU.&lt;/p&gt;

&lt;p&gt;However, to maximize CPU usage you can start multiple instances of Redis in the same box and treat them as different servers. At some point a single box may not be enough anyway, so if you want to use multiple CPUs you can start thinking of some way to shard earlier.&lt;/p&gt;

&lt;p&gt;With Redis 4.0 Redis team started to make Redis more threaded. For now this is limited to deleting objects in the background, and to blocking commands implemented via Redis modules. For future releases, the plan is to make Redis more and more threaded.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://redis.io/topics/introduction"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q17: What do the terms "CPU bound" and "I/O bound" mean in context of Redis?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A program is &lt;strong&gt;CPU bound&lt;/strong&gt; if it would go faster if the CPU were faster, i.e. it spends the majority of its time simply using the CPU (doing calculations). A program that computes new digits of π will typically be CPU-bound, it's just crunching numbers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A program is &lt;strong&gt;I/O bound&lt;/strong&gt; if it would go faster if the I/O subsystem was faster. Which exact I/O system is meant can vary; I typically associate it with disk, but of course networking or communication in general is common too. A program that looks through a huge file for some data might become I/O bound, since the bottleneck is then the reading of the data from disk (actually, this example is perhaps kind of old-fashioned these days with hundreds of MB/s coming in from SSDs).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Memory bound&lt;/strong&gt; means the rate at which a process progresses is limited by the amount memory available and the speed of that memory access. A task that processes large amounts of in memory data, for example multiplying large matrices, is likely to be Memory Bound.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cache bound&lt;/strong&gt; means the rate at which a process progress is limited by the amount and speed of the cache available. A task that simply processes more data than fits in the cache will be cache bound.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's not very frequent that CPU becomes your bottleneck with Redis, as usually Redis is either memory or network bound. &lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://redis.io/topics/introduction"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q18: Why Redis does not support roll backs?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Redis commands can fail during a transaction, but still Redis will execute the rest of the transaction instead of rolling back.&lt;/p&gt;

&lt;p&gt;There are good opinions for this behavior:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Redis commands can fail only if called with a &lt;strong&gt;wrong syntax&lt;/strong&gt; (and the problem is not detectable during the command queueing), or against keys holding the &lt;strong&gt;wrong data type&lt;/strong&gt;: this means that in practical terms a failing command is the result of a programming errors, and a kind of error that is very likely to be detected during development, and not in production.&lt;/li&gt;
&lt;li&gt;Redis is internally simplified and faster because it does not need the ability to roll back.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://redis.io/topics/transactions"&gt;redis.io&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q19: What is AOF persistence in Redis?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Redis AOF (Append Only Files) persistence&lt;/strong&gt; logs every write operation received by the server, that will be played again at server startup, reconstructing the original dataset.&lt;/p&gt;

&lt;p&gt;Redis must be explicitly configured for AOF persistence, if this is required, and this will result in a performance penalty as well as growing logs. It may suffice for relatively reliable persistence of a limited amount of data flow.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/25328317/does-redis-persist-data"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q20: If there's a way to check if a key already exists in a redis list?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Your options are as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Using &lt;code&gt;LREM&lt;/code&gt; and replacing it if it was found.&lt;/li&gt;
&lt;li&gt; Maintaining a separate &lt;code&gt;SET&lt;/code&gt; in conjunction with your &lt;code&gt;LIST&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt; Looping through the &lt;code&gt;LIST&lt;/code&gt; until you find the item or reach the end.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Redis lists are implemented as a, hence the limitations. I think your best option is maintaining a duplicate &lt;code&gt;SET&lt;/code&gt;. Regardless, make sure your actions are atomic with &lt;code&gt;MULTI&lt;/code&gt;-&lt;code&gt;EXEC&lt;/code&gt; or Lua scripts.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/9312838/checking-if-a-value-exists-in-a-list-already-redis"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q21: What are the underlying data structures used for Redis?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is the underlying implementation of every Redis data type.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Strings&lt;/strong&gt; are implemented using a C dynamic string library so that we don't pay (asymptotically speaking) for allocations in append operations. This way we have &lt;code&gt;O(N)&lt;/code&gt; appends, for instance, instead of having quadratic behavior.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Lists&lt;/strong&gt; are implemented with &lt;em&gt;linked lists&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Sets&lt;/strong&gt; and &lt;strong&gt;Hashes&lt;/strong&gt; are implemented with &lt;em&gt;hash tables&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Sorted sets&lt;/strong&gt; are implemented with &lt;a href="http://www.catonmat.net/blog/mit-introduction-to-algorithms-part-eight/"&gt;skip lists&lt;/a&gt; (a peculiar type of balanced trees).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Zip List&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Int Sets&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Zip Maps&lt;/strong&gt; (deprecated in favour of zip list since Redis 2.6)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It shall be also said that for every Redis operation you'll find the time complexity in the documentation so if you are not interested in Redis internals you should not care about how data types are implemented internally really.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/9625246/what-are-the-underlying-data-structures-used-for-redis"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q22: How much faster is Redis than mongoDB?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Rough results are &lt;strong&gt;2x write, 3x read&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;The general answer is that Redis 10 - 30% faster when the data set fits within working memory of a single machine. Once that amount of data is exceeded, Redis fails. &lt;/li&gt;
&lt;li&gt;But your best bet is to benchmark them yourself, in precisely the manner you are intending to use them. As a corollary you'll probably figure out the best way to make use of each.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/5252577/how-much-faster-is-redis-than-mongodb"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q23: What happens if Redis runs out of memory?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Redis will either be killed by the Linux kernel OOM killer, crash with an error, or will start to slow down. With modern operating systems malloc() returning NULL is not common, usually the server will start swapping (if some swap space is configured), and Redis performance will start to degrade, so you'll probably notice there is something wrong.&lt;/p&gt;

&lt;p&gt;Redis has built-in protections allowing the user to set a max limit to memory usage, using the &lt;code&gt;maxmemory&lt;/code&gt; option in the configuration file to put a limit to the memory Redis can use. If this limit is reached Redis will start to reply with an error to write commands (but will continue to accept read-only commands), or you can configure it to evict keys when the max memory limit is reached in the case where you are using Redis for caching.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://redis.io/topics/faq"&gt;redis.io&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q24: RDB and AOF, which one should I use?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The general indication is that you should &lt;strong&gt;use both&lt;/strong&gt; persistence methods if you want a degree of data safety comparable to what PostgreSQL can provide you.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you care a lot about your data, but still can live with a few minutes of data loss in case of disasters, you can simply &lt;strong&gt;use RDB&lt;/strong&gt; alone.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://topic.alibabacloud.com/a/the-difference-between-the-rdb-and-aof-persistence-for-redis-go_1_47_30059570.html"&gt;topic.alibabacloud.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q25: Is Redis a durable datastore ("D" from ACID)?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Redis&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Redis is not &lt;em&gt;usually&lt;/em&gt; deployed as a "durable" datastore (in the sense of the "D" in ACID.), even with journaling. Most use cases intentionally sacrifice a little durability in return for speed.&lt;/p&gt;

&lt;p&gt;However, the "append only file" storage mode can optionally be configured to operate in a durable manner, at the cost of performance. It will have to pay for an &lt;a href="http://en.wikipedia.org/wiki/Sync_%28Unix%29"&gt;fsync()&lt;/a&gt; on every modification.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/2449969/is-redis-a-durable-datastore"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Thanks 🙌 for reading and good luck on your interview!&lt;/em&gt; &lt;br&gt;&lt;em&gt;Please share this article with your fellow devs if you like it!&lt;/em&gt; &lt;br&gt;&lt;em&gt;Check more FullStack Interview Questions &amp;amp; Answers on 👉 &lt;a href="https://www.fullstack.cafe"&gt;www.fullstack.cafe&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>redis</category>
      <category>career</category>
    </item>
    <item>
      <title>Gold Prices API — How To Get Real Time &amp; Historical Gold &amp; Silver JSON Prices from GoldAPI.io in 30 secs</title>
      <dc:creator>Alex 👨🏼‍💻FullStack.Cafe</dc:creator>
      <pubDate>Tue, 08 Sep 2020 08:22:55 +0000</pubDate>
      <link>https://forem.com/aershov24/gold-prices-api-how-to-get-real-time-historical-gold-silver-json-prices-from-goldapi-io-in-30-secs-iad</link>
      <guid>https://forem.com/aershov24/gold-prices-api-how-to-get-real-time-historical-gold-silver-json-prices-from-goldapi-io-in-30-secs-iad</guid>
      <description>&lt;p&gt;A lot of businesses need precise Gold and Silver prices to operate. Unfortunately when it comes to get real time Gold and Silver prices for your automation your options are quite limited. You can even end up by scrapping html pages from Kitco or open trading account on one of the currency exchanges just to get access to gold or silver prices API. But finally there is a solution for developers who needs to get access to Gold and Silver spot and historical prices live feed.&lt;/p&gt;




&lt;p&gt;Meet &lt;a href="https://www.goldapi.io/"&gt;GoldAPI.io — Minimalistic Free Real-Time Gold and Silver Spot Prices REST JSON API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mNLLcoB---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/3524/1%2AUS09iCpSEg5SzPqrYki2_A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mNLLcoB---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/3524/1%2AUS09iCpSEg5SzPqrYki2_A.png" alt="Image for post"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GoldAPI.io provides 100% Real-Time &amp;amp; Historical Gold and Silver Spot Prices from FOREX and LBMA for Your Business.&lt;/p&gt;

&lt;p&gt;By using GoldAPI.io you get an instant Access To Live Feed of Precious Metals Spot Prices from one simple and free API as well as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  100% Real-Time Live Feed from FOREX, SAXO, OANDA and IDC Exchanges&lt;/li&gt;
&lt;li&gt;  Live Metal Spot Prices in USD, AUD, CAD, EUR and other currencies&lt;/li&gt;
&lt;li&gt;  Historical LBMA AM Fix Gold &amp;amp; Silver Prices Since 1968&lt;/li&gt;
&lt;li&gt;  Real-Time Gold/Silver (XAU/XAG) Ratio&lt;/li&gt;
&lt;li&gt;  Free Plan up to 500 requests per month&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can use GoldAPI.io in 30 Seconds. There is No Card Required and No Lock-in Contract for Unlimited plan.&lt;/p&gt;




&lt;p&gt;To use GoldAPI.io just log in with your Google account to get your free API Key. Once you have your API Key you can test it directly on GoldAPI.io dashboard to get latest Gold and Silver prices in any currency with just one simple GET request:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XCu5iLDn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/3436/1%2ADduiFbhtNMu8kWqK7MDNWA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XCu5iLDn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/3436/1%2ADduiFbhtNMu8kWqK7MDNWA.png" alt="Image for post"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even if GoldAPI.io is a relatively new service on the market it has been already trusted by many Fin Tech startups, individual investors and bullion diallers.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>showdev</category>
      <category>api</category>
    </item>
    <item>
      <title>Announcing CodeStack.Café ☕ Your Coding Interview Pain Killer  </title>
      <dc:creator>Alex 👨🏼‍💻FullStack.Cafe</dc:creator>
      <pubDate>Fri, 28 Aug 2020 08:17:35 +0000</pubDate>
      <link>https://forem.com/aershov24/announcing-codestack-cafe-your-coding-interview-pain-killer-ii9</link>
      <guid>https://forem.com/aershov24/announcing-codestack-cafe-your-coding-interview-pain-killer-ii9</guid>
      <description>&lt;p&gt;Hi my fellow Dev.to warriors! &lt;/p&gt;

&lt;p&gt;Today I soft-launching my sixth (yep you read it right, it is 6!) product - &lt;a href="https://www.codestack.cafe"&gt;CodeStack.Cafe - Your Coding Interview Pain Killer&lt;/a&gt;. CodeStack is my take on the most painful problem all software developers experienced in their lives: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Failed my Coding Interview. Again...."&lt;/strong&gt;&lt;br&gt;
— &lt;em&gt;From Random Dev Today&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;For the last 6 months I was storming through Google, StackOverflow, algo books and myriad of You tube videos just to produce the best, most comprehensive collection of Coding Interview Questions, Problems &amp;amp; Challenges so laser-focused on interview preparation that you would never fail your programming interview again. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;That is my promise - You Would Never Fail Your Coding Interview Again&lt;/strong&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;My job is not yet done but product is ready to deliver value for anyone who looking for a dev job right now. &lt;/p&gt;

&lt;p&gt;How is that different from a Cracking Coding Interview book and etc.? Well... the same way like a shot of coffee is different from a deep 12 hours sleep. Both give your energy, both are good, but CodeStack.Cafe can enrich your brain with a dose of essential fundamental knowledge in 5 days not 3 months. Exactly what you need before that next coding interview, heh?&lt;/p&gt;

&lt;p&gt;Build 100% on React and Node.js (only 87kb bundle size + 93 Google PageSpeed), fast as hell, ads-and-pixels free it gives you the best interview preparation environment you can desire. And yes, you can download PDF's for any QAS topics for your offline prep during morning hours commute.&lt;/p&gt;




&lt;p&gt;For all me fellow Devs I have a special promocode &lt;a href="https://www.codestack.cafe/?promo=DEVTO"&gt;DEVTO&lt;/a&gt; that will give you 50% OFF for the lifetime access to CodeStack.Cafe and all content updates for FREE forever. &lt;/p&gt;

&lt;p&gt;Good luck to your next coding interview and all the best!&lt;/p&gt;

</description>
      <category>career</category>
      <category>productivity</category>
      <category>codenewbie</category>
      <category>showdev</category>
    </item>
    <item>
      <title>GoldAPI.io - How to build Gold &amp; Silver Prices REST API and Dashboard using Node.js + React in 5 days</title>
      <dc:creator>Alex 👨🏼‍💻FullStack.Cafe</dc:creator>
      <pubDate>Thu, 28 May 2020 08:48:29 +0000</pubDate>
      <link>https://forem.com/aershov24/goldapi-io-how-to-build-gold-silver-prices-rest-api-using-node-js-react-in-5-days-3i2e</link>
      <guid>https://forem.com/aershov24/goldapi-io-how-to-build-gold-silver-prices-rest-api-using-node-js-react-in-5-days-3i2e</guid>
      <description>&lt;p&gt;Hi guys, I was away from DEV.TO for a long time growing FullStack.Cafe  but recently decided to take a break and work a bit on a new product to avoid tunnel vision and get some practice with React.&lt;/p&gt;

&lt;p&gt;The new service called &lt;a href="https://www.goldapi.io"&gt;GoldAPI.io&lt;/a&gt;. It provides historical daily Gold &amp;amp; Silver prices since 1968 (LBMA AM Fix rates in USD) as well as real-time Gold and Silver Spot Rates in 169 different currencies. Targeted audience - mobile/financial app devs, bullion dealers, wealth management firms, traders.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TABV3_JM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83119451-5073e300-a102-11ea-86e5-057b8c9f9643.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TABV3_JM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83119451-5073e300-a102-11ea-86e5-057b8c9f9643.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;UVP&lt;/strong&gt;: The only Gold &amp;amp; Silver Prices REST JSON API you need&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Value metric&lt;/strong&gt;: reqs per day. Pricing - free (100 reqs/daily), 19.99$ - 5000 reqs/daily, Enterprise - Unlimited, by request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hzV_kqSb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83119778-b7919780-a102-11ea-9fb9-04fa06469edd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hzV_kqSb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83119778-b7919780-a102-11ea-9fb9-04fa06469edd.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From development perspective React feels definitely more lightweight than Angular (I was using Angular for FullStack.Cafe and CodeStack.Cafe for 2.5 years so far). My current bundle size is 170kb (after Brottli compression). The best I could get from Angular was 260kb (including Material UI). React builds faster (1 min vs 3-4 mins for Angular 9). Can be improved even further with proper lazy loading (for routes) and hooks refactoring (P.S. see the refactoring results below). Having JS (and not TS) on frontend and backend helps with speed of development and staying in the same language context. Node (Express) rules for simple fast APIs. If you want to horizontally scale API across multiple CPUs use Throng. Cache everything.&lt;/p&gt;

&lt;p&gt;In overall I would recommend MERN (Mongo+Express+React+Node) stack for any  modern startup/side project. I don't separate frontend and backend solutions for deployment so it's kinda monolithic but that can be refactored easily. From hosting perspective I use Heroku + mLab (for Mongo). Billing - obviously Stripe is a king. CSS - still use Bootrstrap 4 but want to try Tailwind in the future. Deployment - Heroku Git as one simple push.&lt;/p&gt;

&lt;p&gt;And this is site speed results from GTMetrix:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4K9UZBXt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83210708-210ab800-a18e-11ea-8c46-64cb242f280c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4K9UZBXt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83210708-210ab800-a18e-11ea-8c46-64cb242f280c.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In overall I spent 5 days (idea came last Friday) on implementation reusing some code from my previous products (including API billing metering and Stripe integration). Launched yesterday, commented on Quora and in 3 hours got my first sign up. So far got 4 users for 24 hours with zero marketing.&lt;/p&gt;

&lt;p&gt;If you build APIs as products and interested how I developed and how I plan to market gimme a shout or comment there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;P.S.&lt;/strong&gt; This is results of refactored version with components lazy loading. BlogPost and Dashboard components are lazy loaded using routing. And this is awesome guys.&lt;/p&gt;

&lt;p&gt;Before (for Landing Page):&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aIVswRbd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83215933-a5633800-a19a-11ea-9a32-bd113c923b1e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aIVswRbd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83215933-a5633800-a19a-11ea-9a32-bd113c923b1e.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Gpg6oJqT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83215808-62a16000-a19a-11ea-84c0-0213aa68ba5b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Gpg6oJqT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83215808-62a16000-a19a-11ea-84c0-0213aa68ba5b.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fd1-Ce9L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83215647-0f2f1200-a19a-11ea-9fc6-b4848089fce4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fd1-Ce9L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83215647-0f2f1200-a19a-11ea-9fc6-b4848089fce4.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GTMetrix:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n1ZfVvOd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83215685-2110b500-a19a-11ea-88a8-cd5db9c5db88.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n1ZfVvOd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83215685-2110b500-a19a-11ea-88a8-cd5db9c5db88.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Google Site Speed:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q48S2qVE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83215556-d68f3880-a199-11ea-80d4-ec7c24f7e06a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q48S2qVE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/13550565/83215556-d68f3880-a199-11ea-80d4-ec7c24f7e06a.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Stay safe.&lt;br&gt;
Alex @ GAPI&lt;/p&gt;

</description>
      <category>api</category>
      <category>react</category>
      <category>node</category>
    </item>
    <item>
      <title>22 Dart Interview Questions (ANSWERED) Flutter Dev Must Know in 2020</title>
      <dc:creator>Alex 👨🏼‍💻FullStack.Cafe</dc:creator>
      <pubDate>Thu, 27 Feb 2020 07:44:49 +0000</pubDate>
      <link>https://forem.com/fullstackcafe/22-dart-interview-questions-answered-flutter-dev-must-know-in-2020-4i8o</link>
      <guid>https://forem.com/fullstackcafe/22-dart-interview-questions-answered-flutter-dev-must-know-in-2020-4i8o</guid>
      <description>&lt;p&gt;Dart is an open source, purely object-oriented, optionally typed, and a class-based language which has excellent support for functional as well as reactive programming. Dart was the fastest-growing language between 2018 and 2019, with usage up a massive 532%. Follow along and check 22 common Dart Interview Questions Flutter and Mobile developers should be prepared for in 2020.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Originally published on &lt;a href="https://www.fullstack.cafe/blog/redis-interview-questions"&gt;FullStack.Cafe - Don't F*ck Up Your Next Tech Interview&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Q1: What is Dart and why does Flutter use it?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Dart&lt;/strong&gt; is an &lt;em&gt;object-oriented&lt;/em&gt;, &lt;em&gt;garbage-collected&lt;/em&gt; programming language that you use to develop Flutter apps.&lt;br&gt;
It was also created by Google, but is open-source, and has community inside and outside Google.&lt;br&gt;
Dart was chosen as the language of &lt;strong&gt;Flutter&lt;/strong&gt; for the following reason: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dart is &lt;strong&gt;AOT&lt;/strong&gt; (Ahead Of Time) compiled to fast, predictable, native code, which allows almost all of Flutter to be written in Dart. This not only makes Flutter fast, virtually everything (including all the widgets) can be customized.&lt;/li&gt;
&lt;li&gt;Dart can also be &lt;strong&gt;JIT&lt;/strong&gt; (Just In Time) compiled for exceptionally fast development cycles and game-changing workflow (including Flutter’s popular sub-second stateful hot reload).&lt;/li&gt;
&lt;li&gt;Dart allows Flutter to avoid the need for a separate declarative layout language like &lt;em&gt;JSX&lt;/em&gt; or &lt;em&gt;XML&lt;/em&gt;, or separate visual interface builders, because Dart’s declarative, programmatic layout is easy to read and visualize. And with all the layout in one language and in one place, it is easy for Flutter to provide advanced tooling that makes layout a snap.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://hackernoon.com/why-flutter-uses-dart-dd635a054ebf"&gt;hackernoon.com&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Q2: What is Fat Arrow Notation in Dart and when do you use it?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The fat arrow syntax is simply a short hand for returning an expression and is similar to &lt;code&gt;(){ return expression; }&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The fat arrow is for returning a single line, braces are for returning a code block.&lt;/p&gt;

&lt;p&gt;Only an expression—not a statement—can appear between the arrow (&lt;code&gt;=&amp;gt;&lt;/code&gt;) and the semicolon (&lt;code&gt;;&lt;/code&gt;). For example, you can’t put an &lt;em&gt;if&lt;/em&gt; statement there, but you can use a &lt;em&gt;conditional&lt;/em&gt; expression&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Normal function&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;function1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'arg was 3'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'arg was not 3'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Arrow Function&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;function2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'arg was &lt;/span&gt;&lt;span class="si"&gt;${a == 3 ? '' : 'not '}&lt;/span&gt;&lt;span class="s"&gt;3'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/51868395/flutter-dart-difference-between-and/51869508"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q3: Differentiate between required and optional parameters in Dart
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Required Parameters&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dart required parameters are the arguments that are passed to a function and the function or method required all those parameters to complete its code block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;findVolume&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;breath&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
 &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'length = &lt;/span&gt;&lt;span class="si"&gt;$length&lt;/span&gt;&lt;span class="s"&gt;, breath = &lt;/span&gt;&lt;span class="si"&gt;$breath&lt;/span&gt;&lt;span class="s"&gt;, height = &lt;/span&gt;&lt;span class="si"&gt;$height&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;findVolume&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Optional Parameters&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Optional parameters are defined at the end of the parameter list, after any required parameters. &lt;/li&gt;
&lt;li&gt;In Flutter/Dart, there are 3 types of optional parameters:

&lt;ul&gt;
&lt;li&gt;Named 

&lt;ul&gt;
&lt;li&gt;Parameters wrapped by &lt;code&gt;{ }&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;eg. &lt;code&gt;getUrl(int color, [int favNum])&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Positional 

&lt;ul&gt;
&lt;li&gt;Parameters wrapped by &lt;code&gt;[ ]&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;eg. &lt;code&gt;getUrl(int color, {int favNum})&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Default 

&lt;ul&gt;
&lt;li&gt;Assigning a default value to a parameter. &lt;/li&gt;
&lt;li&gt;eg. &lt;code&gt;getUrl(int color, [int favNum = 6])&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/13264230/what-is-the-difference-between-named-and-positional-parameters-in-dart"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q4: Differentiate between named parameters and positional parameters in Dart?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Both named and positional parameters are part of optional parameter:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Optional Positional Parameters:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A parameter wrapped by &lt;code&gt;[ ]&lt;/code&gt; is a &lt;strong&gt;positional&lt;/strong&gt; optional parameter.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;getHttpUrl&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You can specify multiple positional parameters for a function:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;getHttpUrl&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;numRetries&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;In the above code, &lt;code&gt;port&lt;/code&gt; and &lt;code&gt;numRetries&lt;/code&gt; are optional and have default values of 80 and 3 respectively. You can call &lt;code&gt;getHttpUrl&lt;/code&gt; with or without the third parameter.  The optional parameters are  &lt;em&gt;positional&lt;/em&gt;  in that you can't omit  &lt;code&gt;port&lt;/code&gt;  if you want to specify  &lt;code&gt;numRetries&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Optional Named Parameters:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A parameter wrapped by &lt;code&gt;{ }&lt;/code&gt; is a &lt;strong&gt;named&lt;/strong&gt; optional parameter.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;getHttpUrl&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="o"&gt;})&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You can specify multiple named parameters for a function:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;getHttpUrl&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;numRetries&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;})&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;You can call &lt;code&gt;getHttpUrl&lt;/code&gt; with or without the third parameter. You &lt;strong&gt;must&lt;/strong&gt; use the parameter name when calling the function.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Because named parameters are referenced by name, they can be used in an order different from their declaration.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;getHttpUrl&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'example.com'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'/index.html'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;numRetries:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;port:&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;getHttpUrl&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'example.com'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'/index.html'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;numRetries:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/13264230/what-is-the-difference-between-named-and-positional-parameters-in-dart"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q5: What is Streams in Flutter/Dart?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Asynchronous programming in Dart is characterized by the &lt;code&gt;Future&lt;/code&gt; and &lt;code&gt;Stream&lt;/code&gt; classes.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;stream&lt;/strong&gt; is a sequence of &lt;em&gt;asynchronous&lt;/em&gt; events. It is like an &lt;em&gt;asynchronous Iterable&lt;/em&gt;—where, instead of getting the next event when you ask for it, the stream tells you that there is an event when it is ready.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;Streams&lt;/em&gt; can be created in many ways but they all are used in the same way; the &lt;em&gt;asynchronous for loop&lt;/em&gt;( &lt;strong&gt;await for&lt;/strong&gt;). E.g&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;sumStream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;async&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Streams provide an &lt;em&gt;asynchronous&lt;/em&gt; sequence of data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data sequences include user-generated events and data read from files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can process a stream using either  &lt;strong&gt;await for&lt;/strong&gt;  or  &lt;code&gt;listen()&lt;/code&gt;  from the &lt;em&gt;Stream API&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Streams provide a way to respond to errors.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There are two kinds of streams: &lt;strong&gt;single subscription&lt;/strong&gt; or &lt;strong&gt;broadcast&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://dart.dev/tutorials/language/streams"&gt;dart.dev&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q6: Explain the different types of Streams?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are two kinds of streams.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Single subscription streams&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The most common kind of stream.&lt;/li&gt;
&lt;li&gt;It contains a &lt;em&gt;sequence of events&lt;/em&gt; that are parts of a larger whole. Events need to be delivered in the correct order and without missing any of them. &lt;/li&gt;
&lt;li&gt;This is the kind of stream you get when you read a file or receive a web request.&lt;/li&gt;
&lt;li&gt;Such a stream can only be listened to once. Listening again later could mean missing out on initial events, and then the rest of the stream makes no sense. &lt;/li&gt;
&lt;li&gt;When you start listening, the data will be fetched and provided in chunks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Broadcast streams&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;It intended for individual messages that can be handled one at a time. This kind of stream can be used for mouse events in a browser, for example.&lt;/li&gt;
&lt;li&gt;You can start listening to such a stream at any time, and you get the events that are fired while you listen. &lt;/li&gt;
&lt;li&gt;More than one listener can listen at the same time, and you can listen again later after canceling a previous subscription.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://dart.dev/tutorials/language/streams"&gt;dart.dev&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q7: What are Null-aware operators?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Dart offers some handy operators for dealing with values that might be null. &lt;/li&gt;
&lt;li&gt;
&lt;p&gt;One is the &lt;strong&gt;??=&lt;/strong&gt; assignment operator, which assigns a value to a variable only if that variable is currently null:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// The initial value of a is null.&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;??=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- Prints 3.&lt;/span&gt;

&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;??=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- Still prints 3.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Another null-aware operator is ??, which returns the expression on its left unless that expression’s value is null, in which case it evaluates and returns the expression on its right:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- Prints 1.&lt;/span&gt;
&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- Prints 12.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://dart.dev/codelabs/dart-cheatsheet"&gt;dart.dev&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q8: How do you check if an async void method is completed in Dart?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Changing the return type to  &lt;code&gt;Future&amp;lt;void&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;save&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Folder&lt;/span&gt; &lt;span class="n"&gt;folder&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;async&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
   &lt;span class="o"&gt;.....&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then you can do  &lt;code&gt;await save(...);&lt;/code&gt;  or  &lt;code&gt;save().then(...);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/a/59864791"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q9: How to declare async function as a variable in Dart?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Async functions are normal functions with some sugar on top. The function variable type just needs to specify that it returns a Future:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="n"&gt;asyncFuncVar&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;asyncFunc&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="n"&gt;async&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Do async stuff...'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;Example&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;asyncFuncVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;asyncFunc&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;asyncFuncVar&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;then&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Hello'&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Example&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/a/59798754"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q10: How to duplicate repeating items inside a Dart list?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Consider the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Ball&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_ballList&lt;/span&gt; &lt;span class="o"&gt;=[&lt;/span&gt;&lt;span class="n"&gt;Ball&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;&lt;span class="n"&gt;Ball&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;&lt;span class="n"&gt;Ball&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;&lt;span class="n"&gt;Ball&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;&lt;span class="n"&gt;Ball&lt;/span&gt;&lt;span class="o"&gt;(),]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What can to be done in order to not repeat &lt;code&gt;Ball()&lt;/code&gt; multiple times.&lt;/p&gt;




&lt;p&gt;Using &lt;code&gt;collection-for&lt;/code&gt; if we need different instances of &lt;code&gt;Ball()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Ball&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_ballList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Ball&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;
&lt;span class="o"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we need the same instance of &lt;code&gt;Ball()&lt;/code&gt;, &lt;code&gt;List.filled&lt;/code&gt; should be used&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Ball&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_ballList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Ball&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="na"&gt;filled&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Ball&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/a/59739135"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q11: How is whenCompleted() different from then() in Future?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.whenComplete&lt;/code&gt; will fire a function either when the &lt;em&gt;Future&lt;/em&gt; completes with an error or not, while &lt;code&gt;.then&lt;/code&gt; returns a new &lt;em&gt;Future&lt;/em&gt; which is completed with the result of the call to &lt;code&gt;onValue&lt;/code&gt; (if this future completes with a value) or to &lt;code&gt;onError&lt;/code&gt; (if this future completes with an error)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.whenCompleted&lt;/code&gt; is the asynchronous equivalent of a "&lt;strong&gt;finally&lt;/strong&gt;" block.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/55381236/difference-between-then-and-whencompleted-methods-when-working-with-future/55382389"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q12: How to get difference of lists in Flutter/Dart?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Consider you have two lists &lt;code&gt;[1,2,3,4,5,6,7]&lt;/code&gt; and &lt;code&gt;[3,5,6,7,9,10]&lt;/code&gt;. How would you get the difference as output? eg. &lt;code&gt;[1, 2, 4]&lt;/code&gt;&lt;/p&gt;




&lt;p&gt;You can do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;alternative answer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[];&lt;/span&gt;

&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;forEach&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="o"&gt;)){&lt;/span&gt;
    &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;//at this point, output list should have the answer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;note that for both cases, you need to loop over the  &lt;strong&gt;larger list&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/59563964/flutter-difference-between-timer-and-animationcontroller-uses/59564169#59564169"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q13: Explain async, await in Flutter/Dart?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Asynchronous&lt;/em&gt; operations let your program complete work while waiting for another operation to finish. Here are some common asynchronous operations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Fetching data over a network.&lt;/li&gt;
&lt;li&gt;  Writing to a database.&lt;/li&gt;
&lt;li&gt;  Reading data from a file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To perform asynchronous operations in Dart, you can use the  &lt;code&gt;Future&lt;/code&gt;  class and the  &lt;code&gt;async&lt;/code&gt;  and  &lt;code&gt;await&lt;/code&gt;  keywords.&lt;/p&gt;

&lt;p&gt;The  &lt;code&gt;async&lt;/code&gt;  and  &lt;code&gt;await&lt;/code&gt;  keywords provide a declarative way to define asynchronous functions and use their results. Remember these two basic guidelines when using  &lt;code&gt;async&lt;/code&gt;  and  &lt;code&gt;await&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  To define an async function, add  &lt;code&gt;async&lt;/code&gt;  before the function body&lt;/li&gt;
&lt;li&gt;  The  &lt;code&gt;await&lt;/code&gt;  keyword works only in  &lt;code&gt;async&lt;/code&gt;  functions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An &lt;code&gt;async&lt;/code&gt; function runs synchronously until the first &lt;code&gt;await&lt;/code&gt; keyword. This means that within an &lt;code&gt;async&lt;/code&gt; function body, all synchronous code before the first &lt;code&gt;await&lt;/code&gt; keyword executes immediately.&lt;/p&gt;

&lt;p&gt;Consider an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'dart:async'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Employee&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;Employee&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;async&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"getting employee..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="n"&gt;getEmployee&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;33&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Got back &lt;/span&gt;&lt;span class="si"&gt;${x.firstName}&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="si"&gt;${x.lastName}&lt;/span&gt;&lt;span class="s"&gt; with id# &lt;/span&gt;&lt;span class="si"&gt;${x.id}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;getEmployee&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;async&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//Simluate what a real service call delay may look like by delaying 2 seconds   &lt;/span&gt;
  &lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="na"&gt;delayed&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;Duration&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;seconds:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
  &lt;span class="c1"&gt;//and then return an employee - lets pretend we grabbed this out of a database &lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Employee&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Joe"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Coder"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://dart.dev/codelabs/async-await"&gt;dart.dev&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q14: What is Future in Flutter/Dart?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A &lt;strong&gt;Future&lt;/strong&gt; is used to represent a potential value, or error, that will be available at some time in the future. Receivers of a &lt;em&gt;Future&lt;/em&gt; can register callbacks that handle the value or error once it is available. For example:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;future&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getFuture&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;future&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;then&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handleValue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
      &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;catchError&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handleError&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If a future doesn’t produce a usable value, then the future’s type is &lt;code&gt;Future&amp;lt;void&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;A future represents the result of an &lt;em&gt;asynchronous&lt;/em&gt; operation, and can have two states: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Uncompleted&lt;/strong&gt;
When you call an &lt;em&gt;asynchronous&lt;/em&gt; function, it returns an uncompleted future. That future is waiting for the function’s &lt;em&gt;asynchronous&lt;/em&gt; operation to finish or to throw an error.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Completed&lt;/strong&gt;
If the &lt;em&gt;asynchronous&lt;/em&gt; operation succeeds, the future completes with a value. Otherwise it completes with an error.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://api.dart.dev/stable/2.7.0/dart-async/Future-class.html"&gt;api.dart.dev&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q15: What are the similarities and differences of Future and Stream?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Similarity:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Future&lt;/code&gt; and &lt;code&gt;Stream&lt;/code&gt; both work &lt;em&gt;asynchronously.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Both have some potential value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Differences:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;Stream&lt;/code&gt; is a combination of &lt;strong&gt;Futures&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Future&lt;/code&gt; has only one response but &lt;code&gt;Stream&lt;/code&gt; could have any number of &lt;strong&gt;Response&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://medium.com/flutter-community/understanding-streams-in-flutter-dart-827340437da6"&gt;medium.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q16: How does Dart AOT work?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Dart source code will be translated into assembly files, then assembly files will be compiled into binary code for different architectures by the assembler.&lt;/li&gt;
&lt;li&gt;For mobile applications the source code is compiled for multiple processors ARM, ARM64, x64 and for both platforms - Android and iOS. This means there are multiple resulting binary files for each supported processor and platform combination.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://flutterbyexample.com/stateful-widget-lifecycle/"&gt;flutterbyexample.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q17: What is a difference between these operators "?? and ?."
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;??&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is a &lt;strong&gt;null-aware operator&lt;/strong&gt; which returns the expression on its left unless that expression’s value is null, in which case it evaluates and returns the expression on its right:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- Prints 1.&lt;/span&gt;
&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- Prints 12.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;?.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is a &lt;strong&gt;conditional property access&lt;/strong&gt; which is used to guard access to a property or method of an object that might be null, put a question mark (?) before the dot (.):&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You can chain multiple uses of  &lt;code&gt;?.&lt;/code&gt;  together in a single expression:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;myObject&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="na"&gt;someProperty&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="na"&gt;someMethod&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;The preceding code returns null (and never calls  &lt;code&gt;someMethod()&lt;/code&gt;) if either  &lt;code&gt;myObject&lt;/code&gt;  or  &lt;code&gt;myObject.someProperty&lt;/code&gt;  is null.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://flutter.dev/docs/testing/build-modes"&gt;flutter.dev&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q18: What's the difference between async and async* in Dart?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;async&lt;/code&gt;  gives you a  &lt;code&gt;Future&lt;/code&gt; while &lt;code&gt;async*&lt;/code&gt;  gives you a  &lt;code&gt;Stream&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;You add the &lt;code&gt;async&lt;/code&gt; keyword to a function that does some work that might take a long time. It returns the result wrapped in a &lt;code&gt;Future&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You add the &lt;code&gt;async*&lt;/code&gt; keyword to make a function that returns a bunch of future values one at a time. The results are wrapped in a Stream.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;async*&lt;/code&gt; will always returns a &lt;code&gt;Stream&lt;/code&gt; and offer some syntax sugar to emit a value through &lt;code&gt;yield&lt;/code&gt; keyword.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/a/55397133"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q19: How to compare two dates that are constructed differently in Dart?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can do that by converting the other date into  &lt;code&gt;utc&lt;/code&gt;  and then comparing them with  &lt;code&gt;isAtSameMomentAs&lt;/code&gt;  method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
  &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;dateTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'2020-02-03T08:30:00.000Z'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2020&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;month&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;day&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;hour&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;minute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;dt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;utc&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;month&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;hour&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;minute&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dateTime&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;isAtSameMomentAs&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dt&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/a/60033249"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q20: What does "non-nullable by default" mean in Dart?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Non-nullable by default&lt;/strong&gt; means that any variable that is declared normally &lt;strong&gt;cannot&lt;/strong&gt; be &lt;code&gt;null&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Any operation accessing the variable before it has been assigned is &lt;strong&gt;illegal&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;Additionally, assigning &lt;code&gt;null&lt;/code&gt; to a non-nullable variable is also not allowed.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// illegal&lt;/span&gt;

  &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'Hello, '&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// legal&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// forbidden&lt;/span&gt;
  &lt;span class="n"&gt;world&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'World!'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// allowed&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/60068435/what-is-nullability-in-dart-non-nullable-by-default/60068436#60068436"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q21: What does a class with a method named ._() mean in Dart/Flutter?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;In Dart, if the leading character is an underscore, then the function/constructor is private to the library.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Class._();&lt;/code&gt;  is a named constructor (another example might be the copy constructor on some object in &lt;em&gt;Flutter&lt;/em&gt;: &lt;code&gt;AnotherClass.copy(...);&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;The  &lt;code&gt;Class._();&lt;/code&gt;  isn't necessary unless you don't want &lt;code&gt;Class&lt;/code&gt; to ever be accidentally instantiated using the implicit default constructor.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/57878112/a-class-method-named-function-in-dart"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q22: How do you convert a List into a Map in Dart?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Flutter&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can use  &lt;a href="https://api.dartlang.org/stable/dart-core/Map/Map.fromIterable.html"&gt;Map.fromIterable&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fromIterable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;key:&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="nl"&gt;value:&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;or  &lt;em&gt;collection-for&lt;/em&gt;  (starting from Dart 2.3):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]:&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/59563964/flutter-difference-between-timer-and-animationcontroller-uses/59564169#59564169"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Thanks 🙌 for reading and good luck on your interview!&lt;/em&gt; &lt;br&gt;&lt;em&gt;Please share this article with your fellow devs if you like it!&lt;/em&gt; &lt;br&gt;&lt;em&gt;Check more FullStack Interview Questions &amp;amp; Answers on 👉 &lt;a href="https://www.fullstack.cafe"&gt;www.fullstack.cafe&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>career</category>
      <category>interview</category>
    </item>
    <item>
      <title>40 Kotlin Interview Questions Android Devs Should Know in 2020</title>
      <dc:creator>Alex 👨🏼‍💻FullStack.Cafe</dc:creator>
      <pubDate>Wed, 22 Jan 2020 07:28:41 +0000</pubDate>
      <link>https://forem.com/fullstackcafe/40-kotlin-interview-questions-android-devs-should-know-in-2020-4j33</link>
      <guid>https://forem.com/fullstackcafe/40-kotlin-interview-questions-android-devs-should-know-in-2020-4j33</guid>
      <description>&lt;p&gt;Kotlin is immensely practical. It addresses the problems that developers have, and not some guys in the academia. So, it has type inference, it has amazing type safety, good collection library, and concurrency library to top it. And it's now official - a lot of organisations are migrating their backend applications to Kotlin, and this trend is not likely to end soon. Follow along to check the most complete and comprehensive collection of the most common and advanced Kotlin Interview Questions every Android developer should know in 2020.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Originally published on &lt;a href="https://www.fullstack.cafe/blog/kotlin-interview-questions"&gt;FullStack.Cafe - Real Tech Interview Questions And Answers For Devs&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Q1: How to initialize an array in Kotlin with values?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In Java an array can be initialized such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;How does Kotlin's array initialization look like?&lt;/p&gt;






&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;IntArray&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;intArrayOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/31366229/how-to-initialize-an-array-in-kotlin-with-values/31366276#31366276"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q2: What is basic difference between fold and reduce in Kotlin? When to use which?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fold&lt;/code&gt; takes an initial value, and the first invocation of the lambda you pass to it will receive that initial value and the first element of the collection as parameters.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt; &lt;span class="nf"&gt;listOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;fold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first call to the lambda will be with parameters &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Having the ability to pass in an initial value is useful &lt;em&gt;if you have to provide some sort of default value or parameter for your operation&lt;/em&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;reduce&lt;/code&gt; doesn't take an initial value, but instead starts with the first element of the collection as the accumulator (called &lt;code&gt;sum&lt;/code&gt; in the following example)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt; &lt;span class="nf"&gt;listOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first call to the lambda here will be with parameters &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/44429419/what-is-basic-difference-between-fold-and-reduce-in-kotlin-when-to-use-which"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q3: What is the difference between var and val in Kotlin?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;var&lt;/strong&gt; is like &lt;code&gt;general&lt;/code&gt; variable and it's known as a &lt;em&gt;mutable&lt;/em&gt; variable in kotlin and can be assigned multiple times.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;val&lt;/strong&gt; is like &lt;code&gt;Final&lt;/code&gt; variable and it's known as &lt;em&gt;immutable&lt;/em&gt; in Kotlin and can be initialized only single time.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;+----------------+-----------------------------+---------------------------+
|                |             val             |            var            |
+----------------+-----------------------------+---------------------------+
| Reference &lt;span class="nb"&gt;type&lt;/span&gt; | Immutable&lt;span class="o"&gt;(&lt;/span&gt;once initialized  | Mutable&lt;span class="o"&gt;(&lt;/span&gt;can able to change|
|                | can&lt;span class="s1"&gt;'t be reassigned)        | value)                    |
+----------------+-----------------------------+---------------------------+
| Example        | val n = 20                  | var n = 20                |
+----------------+-----------------------------+---------------------------+
| In Java        | final int n = 20;           | int n = 20;               |
+----------------+-----------------------------+---------------------------+
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/44200075/val-and-var-in-kotlin"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q4: Where should I use var and where val?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Use &lt;strong&gt;var&lt;/strong&gt; where value is changing &lt;em&gt;frequently&lt;/em&gt;. For example while getting location of android device:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;integerVariable&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Use &lt;strong&gt;val&lt;/strong&gt; where there is &lt;em&gt;no change&lt;/em&gt; in value in whole class. For example you want set textview or button's text programmatically.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;stringVariables&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Button's Constant or final Text"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/44200075/val-and-var-in-kotlin"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q5: What is a data class in Kotlin?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We frequently create classes whose main purpose is to hold data. In Kotlin, this is called a data class and is marked as &lt;code&gt;data&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To ensure consistency and meaningful behavior of the generated code, data classes have to fulfill the following requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The primary constructor needs to have at least one parameter;&lt;/li&gt;
&lt;li&gt;All primary constructor parameters need to be marked as val or var;&lt;/li&gt;
&lt;li&gt;Data classes cannot be abstract, open, sealed or inner;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://kotlinlang.org/docs/reference/data-classes.html"&gt;kotlinlang.org&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q6: How to create singleton in Kotlin?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Just use &lt;code&gt;object&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;SomeSingleton&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above Kotlin object will be compiled to the following equivalent Java code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SomeSingleton&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;SomeSingleton&lt;/span&gt; &lt;span class="no"&gt;INSTANCE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;SomeSingleton&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="no"&gt;INSTANCE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SomeSingleton&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"init complete"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;SomeSingleton&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is the preferred way to implement singletons on a JVM because it enables thread-safe lazy initialization without having to rely on a locking algorithm like the complex double-checked locking.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://medium.com/@BladeCoder/kotlin-singletons-with-argument-194ef06edd9e"&gt;medium.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q7: What will be result of the following code execution?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What will be the output?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;aVar&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;lazy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I am computing this value"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="s"&gt;"Hola"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;aVar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;aVar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;For &lt;code&gt;lazy&lt;/code&gt; the first time you access the Lazy property, the initialisation (&lt;code&gt;lazy()&lt;/code&gt; function invocation) takes place. The second time, this value is remembered and returned:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;I am computing this value
Hola
Hola
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://dzone.com/articles/a-practical-explanation-for-initialization-modifiers-in-kotlin"&gt;dzone.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q8: Explain the Null safety in Kotlin
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Kotlin's type system is aimed at eliminating the danger of null references from code, also known as the The Billion Dollar Mistake.&lt;/p&gt;

&lt;p&gt;One of the most common pitfalls in many programming languages, including Java, is that accessing a member of a null reference will result in a null reference exception. In Java this would be the equivalent of a &lt;code&gt;NullPointerException&lt;/code&gt; or NPE for short.&lt;/p&gt;

&lt;p&gt;In Kotlin, the type system distinguishes between references that can hold &lt;code&gt;null&lt;/code&gt; (nullable references) and those that can not (&lt;code&gt;non-null&lt;/code&gt; references). For example, a regular variable of type String can not hold null:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"abc"&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="c1"&gt;// compilation error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To allow nulls, we can declare a variable as nullable string, written &lt;code&gt;String?&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"abc"&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="c1"&gt;// ok&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://kotlinlang.org/docs/reference/null-safety.html"&gt;kotlinlang.org&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q9: Explain what is wrong with that code?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Why is this code wrong?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Student has got a name as $name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sectionName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sectionName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;The property of the class can’t be declared inside the secondary constructor.. This will give an error because here we are declaring a property &lt;code&gt;id&lt;/code&gt; of the class in the secondary constructor, which is not allowed.&lt;/p&gt;

&lt;p&gt;If you want to use some property inside the secondary constructor, then declare the property inside the class and use it in the &lt;strong&gt;secondary constructor&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Student has got a name as $name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secname&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.programiz.com/kotlin-programming/constructors"&gt;www.programiz.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q10: What is a purpose of Companion Objects in Kotlin?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Unlike Java or C#, Kotlin doesn’t have &lt;code&gt;static&lt;/code&gt; members or member functions. If you need to write a function that can be called without having a class instance but needs access to the internals of a class, you can write it as a member of a &lt;strong&gt;companion object&lt;/strong&gt; declaration inside that class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EventManager&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;companion&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;FirebaseManager&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;firebaseManager&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;EventManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FirebaseManager&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The companion object is a singleton. The companion object is a &lt;strong&gt;proper object&lt;/strong&gt; on its own, and can have its own supertypes - and you can assign it to a variable and pass it around. If you're integrating with Java code and need a true static member, you can annotate a member inside a companion object with &lt;code&gt;@JvmStatic&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://kotlinlang.org/docs/tutorials/kotlin-for-py/objects-and-companion-objects.html"&gt;kotlinlang.org&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q11: What is Lateinit in Kotlin and when would you use it?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;lateinit&lt;/strong&gt; means &lt;em&gt;late initialization&lt;/em&gt;. If you do not want to initialize a variable in the constructor instead you want to initialize it later on and if you can guarantee the initialization before using it, then declare that variable with lateinit keyword. It will not allocate memory until initialized. You cannot use lateinit for primitive type properties like Int, Long etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;lateinit&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Some value"&lt;/span&gt;
    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Length of string is "&lt;/span&gt;&lt;span class="p"&gt;+&lt;/span&gt;&lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"change value"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There are a handful of use cases where this is extremely helpful, for example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Android: variables that get initialized in lifecycle methods;&lt;/li&gt;
&lt;li&gt;Using Dagger for DI: injected class variables are initialized outside and independently from the constructor;&lt;/li&gt;
&lt;li&gt;Setup for unit tests: test environment variables are initialized in a &lt;code&gt;@Before&lt;/code&gt; - annotated method;&lt;/li&gt;
&lt;li&gt;Spring Boot annotations (eg. &lt;code&gt;@Autowired&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://medium.com/@0xalihn/kotlin-var-val-lateinit-lazy-get-when-to-use-what-bd50200b0a38"&gt;medium.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q12: When to use lateinit over lazy initialization in Kotlin?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are some simple rules to determined if you should use one or the other for properties initialisation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If properties are mutable (i.e. might change at a later stage) use &lt;strong&gt;lateInit&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;If properties are set externally (e.g. need to pass in some external variable to set it), use &lt;strong&gt;lateinit&lt;/strong&gt;. There’s still workaround to use lazy but not as direct.&lt;/li&gt;
&lt;li&gt;If they are only meant to initialized once and shared by all, and it’s more internally set (dependent on variable internal to the class), then use &lt;strong&gt;lazy&lt;/strong&gt;. Tactically, you could still use lateinit, but using** lazy** would better encapsulate your initialization code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also compare:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;lateinit&lt;/strong&gt; var&lt;/td&gt;
&lt;td&gt;by &lt;strong&gt;lazy&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can be initialized from anywhere the object seen from.&lt;/td&gt;
&lt;td&gt;Can only be initialized from the initializer lambda.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multiple initialization possible.&lt;/td&gt;
&lt;td&gt;Only initialize single time.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Non-thread safe. It’s up to user to initialize correctly in a multi-threaded environment.&lt;/td&gt;
&lt;td&gt;Thread-safety by default and guarntees that the initializer is invoked by once.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can only be used for var.&lt;/td&gt;
&lt;td&gt;Can only be used for val.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Not eligible for nonnull properties.&lt;/td&gt;
&lt;td&gt;Not eligible for nonnull properties.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;An isInitialized method added to check whether the value has been initialized before.&lt;/td&gt;
&lt;td&gt;Property never able to un-initialized.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Not allowed on properties of primitive types.&lt;/td&gt;
&lt;td&gt;Allowed on properties of primitive types.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://ahsensaeed.com/differences-between-kotlin-lazy-lateinit-delegate/"&gt;ahsensaeed.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q13: What are coroutines in Kotlin?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Unlike many other languages with similar capabilities, async and await are not keywords in Kotlin and are not even part of its standard library. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;kotlinx.coroutines&lt;/code&gt; is a rich library for coroutines developed by JetBrains. It contains a number of high-level coroutine-enabled primitives, including &lt;code&gt;launch&lt;/code&gt;, &lt;code&gt;async&lt;/code&gt; and others. Kotlin Coroutines give you an API to write your asynchronous code sequentially. &lt;/p&gt;

&lt;p&gt;The documentation says Kotlin Coroutines are like lightweight threads. They are lightweight because creating coroutines doesn’t allocate new threads. Instead, they use predefined thread pools, and smart scheduling. Scheduling is the process of determining which piece of work you will execute next.&lt;/p&gt;

&lt;p&gt;Additionally, coroutines can be &lt;strong&gt;suspended&lt;/strong&gt; and &lt;strong&gt;resumed&lt;/strong&gt; mid-execution. This means you can have a long-running task, which you can execute little-by-little. You can pause it any number of times, and resume it when you’re ready again. &lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.raywenderlich.com/1423941-kotlin-coroutines-tutorial-for-android-getting-started"&gt;www.raywenderlich.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q14: What is the difference between suspending vs. blocking?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;blocking&lt;/strong&gt; call to a function means that a call to any other function, from the same thread, will halt the parent’s execution. Following up, this means that if you make a blocking call on the main thread’s execution, you effectively freeze the UI. Until that blocking calls finishes, the user will see a static screen, which is not a good thing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Suspending&lt;/strong&gt; doesn’t necessarily block your parent function’s execution. If you call a suspending function in some thread, you can easily push that function to a different thread. In case it is a heavy operation, it won’t block the main thread. If the suspending function has to suspend, it will simply pause its execution. This way you free up its thread for other work. Once it’s done suspending, it will get the next free thread from the pool, to finish its work.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.raywenderlich.com/1423941-kotlin-coroutines-tutorial-for-android-getting-started"&gt;www.raywenderlich.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q15: What is the equivalent of Java static methods in Kotlin?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Place the function in the &lt;strong&gt;companion object&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;will become:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;companion&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// to run&lt;/span&gt;
&lt;span class="nc"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;a&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Another way is to solve most of the needs for static functions with package-level functions. They are simply declared outside a class in a source code file. The package of a file can be specified at the beginning of a file with the package keyword. Under the hood these "top-level" or "package" functions are actually compiled into their own class. In the above example, the compiler would create a class FooPackage with all of the top-level properties and functions, and route all of your references to them appropriately.&lt;/p&gt;

&lt;p&gt;Consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;foo&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;foo.bar&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/40352684/what-is-the-equivalent-of-java-static-methods-in-kotlin"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q16: Explain advantages of "when" vs "switch" in Kotlin
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In Java we use switch but in Kotlin, that switch gets converted to &lt;strong&gt;when&lt;/strong&gt;. When has a better design. It is more concise and powerful than a traditional &lt;strong&gt;switch&lt;/strong&gt;. &lt;strong&gt;when&lt;/strong&gt; can be used either as an expression or as a statement.&lt;/p&gt;

&lt;p&gt;Some examples of &lt;strong&gt;when&lt;/strong&gt; usage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Two or more choices
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;when&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"One"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Two or Three"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Four"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Number is not between 1 and 4"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;"when" without arguments
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Number is less than 1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Number is greater than 1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Any type passed in "when"
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"One"&lt;/span&gt;
      &lt;span class="s"&gt;"Hello"&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Greeting"&lt;/span&gt;
      &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Long"&lt;/span&gt;
      &lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Not a string"&lt;/span&gt;
      &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Unknown"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Smart casting
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"X is integer"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"X is string"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Ranges
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;when&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"One"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//statement 1&lt;/span&gt;
    &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Two"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//statement 2&lt;/span&gt;
    &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Three"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//statement 3&lt;/span&gt;
    &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Number between 4 and 8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//statement 4&lt;/span&gt;
    &lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Number not in between 9 and 12"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//statement 5&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Number is not between 1 and 8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//statement 6&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://blog.mindorks.com/replace-switch-with-when-in-kotlin"&gt;blog.mindorks.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q17: What are the advantages of Kotlin over Java?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Basically for me less thinking required to write kotlin equivalent to most java code:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;data class&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  java: you have to write getters and setters for each thing, you have to write &lt;code&gt;hashCode&lt;/code&gt; properly (or let IDE auto generate, which you have to do again every time you change the class), &lt;code&gt;toString&lt;/code&gt; (same problem as &lt;code&gt;hashcode&lt;/code&gt;) and &lt;code&gt;equals&lt;/code&gt; (same problem as &lt;code&gt;hashCode&lt;/code&gt;). or you could use lombok, but that comes with some quirky problems of its own. &lt;code&gt;record&lt;/code&gt; types are hopefully on the way. *kotlin: &lt;code&gt;data class&lt;/code&gt; does it all for you.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;getter and setter patterns&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;java: rewrite the getter and setter for each variable you use it for&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;kotlin: don't have to write getter and setter, and custom getter and setter take a lot less typing in kotlin if you do want to. also delegates exist for identical getters\setters&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;abstract&lt;/code&gt; vs &lt;code&gt;open&lt;/code&gt; classes&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;java: you have to make an abstract class implementation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;kotlin: &lt;code&gt;open class&lt;/code&gt; lets you make an inheritable class while also being usable itself. nice mix of interface and regular class imo&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;extension functions&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;java: doesnt exist&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;kotlin: does exist, makes functions more clear in usage and feels more natural.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;null&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;java: Anything but primitives can be null at any time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;kotlin: you get to decide what can and cant be null. allows for nice things like &lt;code&gt;inline class&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;singleton&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;java: Memorize singleton pattern&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;kotlin: &lt;code&gt;object&lt;/code&gt; instead of &lt;code&gt;class&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;generics&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;java: Theyre alright, nothing fancy&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;kotlin: Reified generics (you can access the actual type), &lt;code&gt;in&lt;/code&gt; and &lt;code&gt;out&lt;/code&gt; for covariance&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;named parameters&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;java: does not exist, easy to break api back-compatibility if you arent careful.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;kotlin: does exist, easy to preserve api back-compatiblity.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;primary constructor&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;java: does not have per-se, you still have to define everything inside the class&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;kotlin: very nice to be able to quickly write a constructor without any constructor function or extra needless declarations&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.reddit.com/r/Kotlin/comments/bz9c47/what_are_the_advantages_of_kotlin_over_java/"&gt;www.reddit.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q18: What are some disadvantages of Kotlin?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Some think that Kotlin is a mess of extra syntax and keywords. Here are a few keywords which have non-obvious meanings: internal, crossinline, expect, reified, sealed, inner, open. Java has none of these. Kotlin is also amusingly inconsistent in its keywords: a function is is declared with ‘fun’, but an interface is declared with ‘interface’ (not ‘inter’?). Kotlin also doesn’t have checked exceptions. Checked exceptions have become unfashionable, yet many (including me) find them a powerful way to ensure that your code is robust. Finally, Kotlin hides a lot of what goes on. In Java, you can trace through almost every step of program logic. This can be vital for hunting down bugs. In Kotlin, if you define a data class, then getters, setters, equality testing, to string, and hash code are added for you invisibly. This can be a bad idea.&lt;/p&gt;

&lt;p&gt;Also according docs, what Java has that Kotlin does not:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checked exceptions&lt;/li&gt;
&lt;li&gt;Primitive types that are not classes&lt;/li&gt;
&lt;li&gt;Static members&lt;/li&gt;
&lt;li&gt;Non-private fields&lt;/li&gt;
&lt;li&gt;Wildcard-types&lt;/li&gt;
&lt;li&gt;Ternary-operator a ? b : c&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.quora.com/Why-is-kotlin-a-bad-programming-language"&gt;www.quora.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q19: What is the difference between "open" and "public" in Kotlin?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;open&lt;/strong&gt; keyword means “open for extension“. The open annotation on a class is the opposite of Java's &lt;code&gt;final&lt;/code&gt;: &lt;em&gt;it allows others to inherit from this class&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;If you do not specify any visibility modifier, &lt;strong&gt;public&lt;/strong&gt; is used by default, which means that your declarations will be visible everywhere. &lt;strong&gt;public&lt;/strong&gt; is the default if nothing else is specified explicitly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/49024200/what-is-the-difference-between-open-and-public-in-kotlin"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q20: What is the difference between “const” and “val”?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;const&lt;/code&gt;s are compile time constants. Meaning that their value has to be assigned during compile time, unlike &lt;code&gt;val&lt;/code&gt;s, where it can be done at runtime.&lt;/p&gt;

&lt;p&gt;This means, that &lt;code&gt;const&lt;/code&gt;s can never be assigned to a function or any class constructor, but only to a &lt;code&gt;String&lt;/code&gt; or primitive.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;foo&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;complexFunctionCall&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;   &lt;span class="c1"&gt;//Not okay&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;fooVal&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;complexFunctionCall&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;      &lt;span class="c1"&gt;//Okay&lt;/span&gt;

&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;bar&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello world"&lt;/span&gt;           &lt;span class="c1"&gt;//Also okay&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/37595936/what-is-the-difference-between-const-and-val/37596023#37596023"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q21: What is the difference between List and Array types?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The major difference from usage side is that &lt;code&gt;Arrays&lt;/code&gt; have a fixed size while &lt;code&gt;(Mutable)List&lt;/code&gt;can adjust their size dynamically. Moreover &lt;code&gt;Array&lt;/code&gt; is mutable whereas &lt;code&gt;List&lt;/code&gt; is not.&lt;/p&gt;

&lt;p&gt;Furthermore &lt;code&gt;kotlin.collections.List&lt;/code&gt; is an interface implemented among others by &lt;code&gt;java.util.ArrayList&lt;/code&gt;. It's also extended by &lt;code&gt;kotlin.collections.MutableList&lt;/code&gt;to be used when a collections that allows for item modification is needed.&lt;/p&gt;

&lt;p&gt;On the jvm level &lt;code&gt;Array&lt;/code&gt; is represented by arrays. &lt;code&gt;List&lt;/code&gt; on the other hand is represented by &lt;code&gt;java.util.List&lt;/code&gt; since there are no immutable collections equivalents available in Java.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/36262305/difference-between-list-and-array-types-in-kotlin/36263748#36263748"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q22: When would you use Elvis operator in Kotlin?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The Elvis operator is part of many programming languages, e.g. Kotlin but also Groovy or C#. The Elvis operator is the ternary operator with its second operand omitted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="c1"&gt;// yields `x` if `x` is not null, `y` otherwise.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;x&lt;/code&gt; isn't null, then it will be returned. If it is null, then the &lt;code&gt;y&lt;/code&gt; will be returned. &lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://kotlinlang.org/docs/reference/null-safety.html#null-safety"&gt;kotlinlang.org&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q23: Rewrite this code in Kotlin
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Can you rewrite this Java code in Kotlin?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;synchronized&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;createInstance&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt; &lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;createInstance&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;Using Kotlin:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Singleton&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://antonioleiva.com/objects-kotlin/"&gt;antonioleiva.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q24: How is it recommended to create constants in Kotlin?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In Kotlin, if you want to create the local constants which are supposed to be used with in the class then you can create it like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;MY_CONSTANT_1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Constants1"&lt;/span&gt;
&lt;span class="c1"&gt;// or &lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;MY_CONSTANT_2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Constants2"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Like &lt;code&gt;val&lt;/code&gt;, variables defined with the &lt;code&gt;const&lt;/code&gt; keyword are immutable. The difference here is that &lt;strong&gt;const is used for variables that are known at compile-time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Also avoid using companion objects. Behind the hood, getter and setter instance methods are created for the fields to be accessible. Calling instance methods is technically more expensive than calling static methods. Instead define the constants in &lt;code&gt;object&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;DbConstants&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;TABLE_USER_ATTRIBUTE_EMPID&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"_id"&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;TABLE_USER_ATTRIBUTE_DATA&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"data"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/44038721/constants-in-kotlin-whats-a-recommended-way-to-create-them"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q25: May you use IntArray and an Array is in Kotlin interchangeably?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;Array&amp;lt;Int&amp;gt;&lt;/code&gt; is an &lt;code&gt;Integer[]&lt;/code&gt; under the hood, while &lt;code&gt;IntArray&lt;/code&gt; is an &lt;code&gt;int[]&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This means that when you put an &lt;code&gt;Int&lt;/code&gt; in an &lt;code&gt;Array&amp;lt;Int&amp;gt;&lt;/code&gt;, it will always be boxed (specifically, with an &lt;code&gt;Integer.valueOf()&lt;/code&gt; call). In the case of &lt;code&gt;IntArray&lt;/code&gt;, no boxing will occur, because it translates to a Java primitive array.&lt;/p&gt;

&lt;p&gt;So &lt;strong&gt;no&lt;/strong&gt;, we can't use them interchangeably.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KfsCUzY5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.stack.imgur.com/rwdMu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KfsCUzY5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.stack.imgur.com/rwdMu.png" class="img-fluid"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/45090808/intarray-vs-arrayint-in-kotlin"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q26: What is the Kotlin double-bang (!!) operator?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;strong&gt;not-null assertion operator !!&lt;/strong&gt; converts any value to a non-null type and throws a &lt;code&gt;KotlinNullPointerException&lt;/code&gt; exception if the value is null.&lt;/p&gt;

&lt;p&gt;Consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;
    &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This operator should be used in cases where the developer is guaranteeing – it allows you to be 100% sure that its value is not null. &lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/34342413/what-is-the-kotlin-double-bang-operator/34342561#34342561"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q27: What is the purpose of Unit-returning in functions? Why is VALUE there? What is this VALUE?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Explain what is the purpose of Unit-returning in functions? Why is VALUE there? What is this VALUE?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;printHello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
     &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, $name!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
   &lt;span class="k"&gt;else&lt;/span&gt; 
     &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hi there!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
   &lt;span class="c1"&gt;// We don't need to write 'return Unit.VALUE' or 'return', although we could &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;The purpose is the same as C's or Java's &lt;code&gt;void&lt;/code&gt;. Only Unit is a proper type, so it can be passed as a generic argument etc.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Why we don't call it "Void": because the word "void" means "nothing", and there's another type, &lt;code&gt;Nothing&lt;/code&gt;, that means just "no value at all", i.e. the computation did not complete normally (looped forever or threw an exception). We could not afford the clash of meanings.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Why Unit has a value (i.e. is not the same as Nothing): because generic code can work smoothly then. If you pass Unit for a generic parameter T, the code written for any T will expect an object, and there must be an object, the sole value of Unit.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How to access that value of Unit: since it's a singleton object, just say &lt;code&gt;Unit&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;UNIT&lt;/code&gt; actually contains valuable information, it basically just means "DONE". It just returns the information to the caller, that the method has been finished. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/22654932/what-is-the-purpose-of-unit-returning-in-functions/22655381#22655381"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q28: How would you refactor this code using "apply"?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;body&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MessageBody&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

  &lt;span class="nf"&gt;init&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"\n"&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;signature&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Do you see any refactoring that could be done?&lt;/p&gt;




&lt;p&gt;You can write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;body&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MessageBody&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"\n"&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;signature&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://cargocult.dev/kotlin-standard-extensions"&gt;cargocult.dev&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q29: What is inline class in Kotlin and when do we need one? Provide an example.
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sometimes it is necessary for business logic to create a wrapper around some type. However, it introduces runtime overhead due to additional heap allocations. Moreover, if the wrapped type is primitive, the performance hit is terrible, because primitive types are usually heavily optimized by the runtime.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inline classes&lt;/strong&gt; provide us with a way to wrap a type, thus adding functionality and creating a new type by itself. As opposed to regular (non-inlined) wrappers, they will benefit from improved performance. This happens because the data is inlined into its usages, and object instantiation is skipped in the resulting compiled code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;
        &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, $s"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;    

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Kotlin"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// method `greet` is called as a static method&lt;/span&gt;
    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// property getter is called as a static method&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Some notes about inline classes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A single property initialized in the primary constructor is the basic requirement of an inline class&lt;/li&gt;
&lt;li&gt;Inline classes allow us to define properties and functions just like regular classes&lt;/li&gt;
&lt;li&gt;Init blocks, inner classes, and backing fields are not allowed&lt;/li&gt;
&lt;li&gt;Inline classes can inherit only from interfaces&lt;/li&gt;
&lt;li&gt;Inline classes are also effectively final&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://www.baeldung.com/kotlin-inline-classes"&gt;www.baeldung.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q30: What is Coroutine Scope and how is that different from Coroutine Context?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Coroutines always execute in some context represented by a value of the &lt;strong&gt;CoroutineContext&lt;/strong&gt; type, defined in the Kotlin standard library. The coroutine context is a set of various elements. The main elements are the &lt;strong&gt;Job&lt;/strong&gt; of the coroutine.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CoroutineScope&lt;/strong&gt; has no data on its own, it just holds a &lt;strong&gt;CoroutineContext&lt;/strong&gt;. Its key role is as the implicit receiver of the block you pass to &lt;code&gt;launch&lt;/code&gt;, &lt;code&gt;async&lt;/code&gt; etc.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;runBlocking&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;scope0&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
    &lt;span class="c1"&gt;// scope0 is the top-level coroutine scope.&lt;/span&gt;
    &lt;span class="n"&gt;scope0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;scope1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
        &lt;span class="c1"&gt;// scope1 inherits its context from scope0. It replaces the Job field&lt;/span&gt;
        &lt;span class="c1"&gt;// with its own job, which is a child of the job in scope0.&lt;/span&gt;
        &lt;span class="c1"&gt;// It retains the Dispatcher field so the launched coroutine uses&lt;/span&gt;
        &lt;span class="c1"&gt;// the dispatcher created by runBlocking.&lt;/span&gt;
        &lt;span class="n"&gt;scope1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;scope2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
            &lt;span class="c1"&gt;// scope2 inherits from scope1&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You might say that &lt;strong&gt;CoroutineScope&lt;/strong&gt; formalizes the way the &lt;strong&gt;CoroutineContext&lt;/strong&gt; is inherited. You can see how the &lt;strong&gt;CoroutineScope&lt;/strong&gt; mediates the inheritance of coroutine contexts. If you cancel the job in &lt;code&gt;scope1&lt;/code&gt;, this will propagate to &lt;code&gt;scope2&lt;/code&gt; and will cancel the launched job as well.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/54416840/kotlin-coroutines-scope-vs-coroutine-context"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q31: How would you override default getter for Kotlin data class?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Given the following Kotlin class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;How would I override the Int getter so that it returns &lt;code&gt;0&lt;/code&gt; if the value negative?&lt;/p&gt;




&lt;ol&gt;
&lt;li&gt;Have your business logic that creates the data class alter the value to be 0 or greater before calling the constructor with the bad value. This is probably the best approach for most cases.&lt;/li&gt;
&lt;li&gt;Don't use a &lt;code&gt;data class&lt;/code&gt;. Use a regular &lt;code&gt;class&lt;/code&gt;.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
     &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt;

     &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;?):&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="p"&gt;===&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt; &lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;hashCode&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;javaClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hashCode&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create an additional safe property on the object that does what you want instead of having a private value that's effectively overriden.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt; &lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;safeValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;
     &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/38492103/override-getter-for-kotlin-data-class"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q32: How to create empty constructor for data class in Kotlin?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you give &lt;strong&gt;default values to all the fields&lt;/strong&gt; - empty constructor is generated automatically by Kotlin.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;uniqueIdentifier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and you can simply call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;user&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Another option is to declare a secondary constructor that has no parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;uniqueIdentifier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?){&lt;/span&gt;
    &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;(-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/37873995/how-to-create-empty-constructor-for-data-class-in-kotlin-android"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q33: What are Object expressions in Kotlin and when to use them?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sometimes we need to create an object of some class with slight modification, without explicitly declaring a new subclass for it. Java handles this case with anonymous inner classes. Kotlin uses &lt;strong&gt;object expression&lt;/strong&gt; to achieve the same functionality. We can even create an object expression for an interface or abstract class by just implementing their abstract methods.&lt;/p&gt;

&lt;p&gt;It is often used as a substitution to a Java anonymous class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addMouseListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="err"&gt;: &lt;/span&gt;&lt;span class="nc"&gt;MouseAdapter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;mouseClicked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MouseEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;mouseEntered&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MouseEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://kotlinlang.org/docs/reference/object-declarations.html"&gt;kotlinlang.org&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q34: What's wrong with that code?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's say I want to override the Int getter so that it returns 0 if the value negative for the data class. What's bad with that approach?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;
    &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_value&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;_value&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;The problem with this approach is that data classes aren't really meant for altering data like this. They are really just for &lt;em&gt;holding&lt;/em&gt; data. Overriding the getter for a data class like this would mean that &lt;code&gt;Test(0)&lt;/code&gt; and &lt;code&gt;Test(-1)&lt;/code&gt; wouldn't equal one another and would have different &lt;code&gt;hashCodes&lt;/code&gt;, but when you called &lt;code&gt;.value&lt;/code&gt;, they would have the same result. This is inconsistent, and while it may work for you, other people on your team who see this is a data class, may accidentally misuse it.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/38492103/override-getter-for-kotlin-data-class"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q35: What is the difference between “*” and “Any” in Kotlin generics?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;List&amp;lt;*&amp;gt;&lt;/code&gt; can contain objects of any type, but &lt;em&gt;only that type&lt;/em&gt;, so it can contain &lt;code&gt;Strings&lt;/code&gt; (but only &lt;code&gt;Strings&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;while &lt;code&gt;List&amp;lt;Any&amp;gt;&lt;/code&gt; can contain &lt;code&gt;Strings&lt;/code&gt; and &lt;code&gt;Integers&lt;/code&gt; and whatnot, all in the same list&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/40138923/difference-between-and-any-in-kotlin-generics/40139892#40139892"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q36: Imagine you moving your code from Java to Kotlin. How would you rewrite this code in Kotlin?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐⭐&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt; &lt;span class="no"&gt;LOG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LoggerFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getLogger&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Foo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;Use &lt;strong&gt;Static-like&lt;/strong&gt; approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;companion&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;LOG&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getLogger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MyClass&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;LOG&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warning&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello from MyClass"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/34416869/idiomatic-way-of-logging-in-kotlin/34462577#34462577"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q37: How Kotlin coroutines are better than RxKotlin/RxJava?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Kotlin coroutines are different from Rx. Both are designed to address a problem of asynchronous programming, however their approach to solution is very different:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Rx comes with a particular functional style of programming that can be implemented in virtually any programming language without support from the language itself. It works well when the problem at hand easily decomposes into a sequence of standard operators and not so well otherwise.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Kotlin coroutines provide a language feature that let library writers implement various asynchronous programming styles, including, but not limited to functional reactive style (Rx). With Kotlin coroutines you can also write your asynchronous code in imperative style, in promise/futures-based style, in actor-style, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How Kotlin coroutines are better than RxKotlin? You just write sequential code, everything is as easy as writing synchronous code except it execute asynchronously. It's easier to grasp.&lt;/p&gt;

&lt;p&gt;Coroutines are better to deal with resources&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In RxJava you can assign computations to schedulers but &lt;code&gt;subscribeOn()&lt;/code&gt; and &lt;code&gt;ObserveOn()&lt;/code&gt;are confusing. Every coroutine is given a thread context and return to parent context. For a channel, both side (producer, consumer) execute on his own context. Coroutines are more intuitive on thread or thread pool affectation. &lt;/li&gt;
&lt;li&gt;Coroutines give more control on when those computation occur. You can for example pass hand (&lt;code&gt;yield&lt;/code&gt;), prioritize (&lt;code&gt;select&lt;/code&gt;), parallelize (multiple &lt;code&gt;producer&lt;/code&gt;/&lt;code&gt;actor&lt;/code&gt; on &lt;code&gt;channel&lt;/code&gt;) or lock resource (&lt;code&gt;Mutex&lt;/code&gt;) for a given computation. It may not matter on server (where RxJava came first) but on resources limited environment this level of control may be required.&lt;/li&gt;
&lt;li&gt;Due to it's reactive nature, backpressure doesn't fit well in RxJava. In the other end &lt;code&gt;send()&lt;/code&gt; to channel is a suspensive function that suspend when channel capacity is reached. It's out-of-the-box backpressure given by nature. You could also &lt;code&gt;offer()&lt;/code&gt; to channel, in which case the call never suspend but return &lt;code&gt;false&lt;/code&gt; in case the channel is full, effectively reproducing &lt;code&gt;onBackpressureDrop()&lt;/code&gt; from RxJava. Or you could just write your own custom backpressure logic, which won't be difficult with coroutines, especially compared to do the same with RxJava.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/42066066/how-kotlin-coroutines-are-better-than-rxkotlin"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q38: What is the difference between launch/join and async/await in Kotlin coroutines?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;launch&lt;/strong&gt; is used to** fire and forget coroutine**. It is like starting a new thread. If the code inside the &lt;code&gt;launch&lt;/code&gt; terminates with exception, then it is treated like &lt;em&gt;uncaught&lt;/em&gt; exception in a thread -- usually printed to stderr in backend JVM applications and crashes Android applications. &lt;code&gt;join&lt;/code&gt; is used to wait for completion of the launched coroutine and it does not propagate its exception. However, a crashed &lt;em&gt;child&lt;/em&gt; coroutine cancels its parent with the corresponding exception, too.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;async&lt;/strong&gt; is used to &lt;strong&gt;start a coroutine that computes some result&lt;/strong&gt;. The result is represented by an instance of &lt;code&gt;Deferred&lt;/code&gt; and you &lt;strong&gt;must&lt;/strong&gt; use &lt;code&gt;await&lt;/code&gt; on it. An uncaught exception inside the async code is stored inside the resulting &lt;code&gt;Deferred&lt;/code&gt; and is not delivered anywhere else, it will get silently dropped unless processed. &lt;strong&gt;You MUST NOT forget about the coroutine you’ve started with async.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/46226518/what-is-the-difference-between-launch-join-and-async-await-in-kotlin-coroutines/46226519#46226519"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q39: What is The Billion Dollar Mistake?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Kotlin's type system is aimed at eliminating the danger of null references from code, also known as the The Billion Dollar Mistake.&lt;/p&gt;

&lt;p&gt;One of the most common pitfalls in many programming languages, including Java, is that accessing a member of a null reference will result in a null reference exception. In Kotlin, the type system distinguishes between references that can hold null (nullable references) and those that can not (non-null references).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I call it my billion-dollar mistake. It was the invention of the &lt;strong&gt;null reference&lt;/strong&gt; in 1965. At that time, I was designing the first comprehensive type system for references in an object-oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Tony Hoare at QCon London in 2009 &lt;a href="https://en.wikipedia.org/wiki/Tony_Hoare"&gt;https://en.wikipedia.org/wiki/Tony_Hoare&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://kotlinlang.org/docs/reference/data-classes.html"&gt;kotlinlang.org&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Q40: How to implement Builder pattern in Kotlin?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Topic: &lt;strong&gt;Kotlin&lt;/strong&gt;&lt;br&gt;Difficulty: ⭐⭐⭐⭐⭐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First and foremost, in most cases you don't need to use builders in Kotlin because we have default and named arguments but if you need one use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="c1"&gt;//add private constructor if necessary&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;year&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Builder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
            &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;

        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;year&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
            &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;

        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;year&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;car&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"X"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🔗 &lt;strong&gt;Source:&lt;/strong&gt; &lt;a href="https://stackoverflow.com/questions/36140791/how-to-implement-builder-pattern-in-kotlin/36150989#36150989"&gt;stackoverflow.com&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Thanks 🙌 for reading and good luck on your interview!&lt;/em&gt; &lt;br&gt;&lt;em&gt;Please share this article with your fellow devs if you like it!&lt;/em&gt; &lt;br&gt;&lt;em&gt;Check more FullStack Interview Questions &amp;amp; Answers on 👉 &lt;a href="https://www.fullstack.cafe"&gt;www.fullstack.cafe&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>career</category>
      <category>interview</category>
    </item>
  </channel>
</rss>
