<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8700135416943985114</id><updated>2026-03-26T00:47:56.692-07:00</updated><category term="Microsoft"/><category term="Amazon"/><category term="Data structures"/><category term="google"/><category term="c"/><category term="OS"/><category term="Techgig"/><category term="algorithms"/><category term="compiler"/><category term="linked list"/><category term="Yahoo"/><category term="july"/><category term="1000000007"/><category term="Database"/><category term="Fermats theorem"/><category term="bitonic"/><category term="dynamic programming."/><category term="interpreter"/><category term="inverse"/><category term="java"/><category term="josephus problem"/><category term="june"/><category term="large computation"/><category term="may"/><category term="oracle"/><category term="other"/><category term="partition problem"/><category term="sql"/><category term="techg"/><title type='text'>INTERVIEW QUESTIONS</title><subtitle type='html'>Guys, lets share the Technical interview Questions from various A grade companies....</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>73</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-6021085831700390021</id><published>2015-04-03T04:12:00.001-07:00</published><updated>2015-04-06T23:03:29.831-07:00</updated><title type='text'>Cognizant Interview questions C/C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Hi Guys,&lt;br /&gt;
Its been Long have updated my blog. I have attended an Interview at Cognizant on 28th March 2015 at Powai (Mumbai) for &quot;C&quot; Developer position.&lt;br /&gt;
&lt;br /&gt;
I was contacted by the HR. Interview consisted of 2 Technical rounds. One with a Sr developer and other with Manager and another Senior developer.&lt;br /&gt;
&lt;br /&gt;
I can recollect some of the question asked to me. Here are those...&lt;br /&gt;
&lt;br /&gt;
1) How do we allocate dynamic memory in C. (write the syntax of malloc)&lt;br /&gt;
2) How do we free it ?.&lt;br /&gt;
3) How does free understand how much memory to free as we just pass a pointer to it.&lt;br /&gt;
4) Return type of free.&lt;br /&gt;
==&amp;gt; First interview had these question and he asked me to design a solution to a problem. He was quite helpful. I had already told that I don&#39;t have much hands-on with IPC.&lt;br /&gt;
&lt;br /&gt;
5) How to allocate 1 bit to a variable.&lt;br /&gt;
6) Go to the 3rd last node of a list.&lt;br /&gt;
7) What are zombies ?&lt;br /&gt;
8) How to identify the zombies in Linux.&lt;br /&gt;
9) What does fork do ?&lt;br /&gt;
10) Whether to use fork or system calls ?&lt;br /&gt;
11) How to create a daemon process ?&lt;br /&gt;
12) What is a race condition.&lt;br /&gt;
13) He asked about thread local memory. (I wasn&#39;t quite sure what he exactly meant)&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/6021085831700390021/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2015/04/cognizant-interview-questions-cc.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/6021085831700390021'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/6021085831700390021'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2015/04/cognizant-interview-questions-cc.html' title='Cognizant Interview questions C/C++'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-2246334925648550213</id><published>2013-10-27T23:34:00.002-07:00</published><updated>2015-04-03T04:39:14.815-07:00</updated><title type='text'>Know thy Complexities !!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;html&gt;
&lt;style&gt;
body {
  padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}

/* Sticky footer styles
-------------------------------------------------- */

html,
body {
  height: 100%;
  /* The html and body elements cannot have any padding or margin. */
}

/* Wrapper for page content to push down footer */
#wrap {
  min-height: 100%;
  height: auto !important;
  height: 100%;
  /* Negative indent footer by it&#39;s height */
  margin: 0 auto -60px;
}

/* Set the fixed height of the footer here */
#push,
#footer {
  height: 120px;
}
#footer {
  background-color: #f5f5f5;
}

/* Lastly, apply responsive CSS fixes as necessary */
@media (max-width: 767px) {
  #footer {
    margin-left: -20px;
    margin-right: -20px;
    padding-left: 20px;
    padding-right: 20px;
  }
}

/* my stuff */
 .green {
  border: 1px solid #286500;
  color: #286500;
  background-color: #53d000;
 }
 
 .red {
  border: 1px solid #b20000;
  color: #b20000;
  background-color: #ff8989;
 }
 
 .yellow {
  border: 1px solid #6f6e00;
  color: #6f6e00;
  background-color: yellow;
 }
 
 #contribute-container {
   background-color: #f5f5f5;
 }
 
 h2 {
   padding-top: 40px; 
 }

  #socialContainer {
      margin-top: 10px;
  }
  #socialContainer iframe {
      background-color: transparent;
  }
  #socialContainer &gt; div {
      padding-right: 5px;
      display: inline-block;
      vertical-align: top;
  }
  #socialContainer #facebookContainer {
      padding-right: 0px;
  }
  
  #legend {
      width: auto; 
  }
  
.gittip-widget {
    float: right;
}

#comments {
    padding-top: 50px; 
}
&lt;/style&gt;
&lt;body&gt;
&lt;h1&gt;
Know Thy Complexities!&lt;/h1&gt;
Hi there!&amp;nbsp; This webpage covers the space and time Big-O complexities of common algorithms used in Computer Science.&amp;nbsp; When preparing for technical interviews in the past, I found myself spending hours crawling the internet putting together the best, average, and worst case complexities for search and sorting algorithms so that I wouldn&#39;t be stumped when asked about them.&amp;nbsp; Over the last few years, I&#39;ve interviewed at several Silicon Valley startups, and also some bigger companies, like Yahoo, eBay, LinkedIn, and Google, and each time that I prepared for an interview, I thought to msyelf &quot;Why oh why hasn&#39;t someone created a nice Big-O cheat sheet?&quot;.&amp;nbsp; So, to save all of you fine folks a ton of time, I went ahead and created one.&amp;nbsp; Enjoy!&lt;br /&gt;

&lt;table class=&quot;table table-bordered table-striped&quot; id=&quot;legend&quot;&gt;
  &lt;tbody&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;Good&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;Fair&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;Poor&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&quot;searching&quot;&gt;
Searching&lt;/h2&gt;
&lt;table class=&quot;table table-bordered table-striped&quot;&gt;
  &lt;thead&gt;
&lt;tr&gt;
      &lt;th&gt;Algorithm&lt;/th&gt;
      &lt;th&gt;Data Structure&lt;/th&gt;
      &lt;th colspan=&quot;2&quot;&gt;Time Complexity&lt;/th&gt;
      &lt;th colspan=&quot;3&quot;&gt;Space Complexity&lt;/th&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;Average&lt;/th&gt;  
      &lt;th&gt;Worst&lt;/th&gt; 
      &lt;th&gt;Worst&lt;/th&gt;                
    &lt;/tr&gt;
&lt;/thead&gt;
  &lt;tbody&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Depth-first_search&quot;&gt;Depth First Search (DFS)&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Graph of |V| vertices and |E| edges&lt;/td&gt;
      &lt;td&gt;&lt;code&gt;-&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(|E| + |V|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(|V|)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Breadth-first_search&quot;&gt;Breadth First Search (BFS)&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Graph of |V| vertices and |E| edges&lt;/td&gt;
      &lt;td&gt;&lt;code&gt;-&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(|E| + |V|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(|V|)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Binary_search_algorithm&quot;&gt;Binary search&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Sorted array of n elements&lt;/td&gt;
      &lt;td&gt; 
        &lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Brute-force_search&quot;&gt;Linear (Brute Force)&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Array&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Dijkstra&#39;s_algorithm&quot;&gt;Shortest path by Dijkstra,&lt;br /&gt;using a Min-heap as priority queue&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Graph with |V| vertices and |E| edges&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O((|V| + |E|) log |V|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O((|V| + |E|) log |V|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(|V|)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Dijkstra&#39;s_algorithm&quot;&gt;Shortest path by Dijkstra,&lt;br /&gt;using an unsorted array as priority queue&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Graph with |V| vertices and |E| edges&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(|V|^2)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(|V|^2)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(|V|)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm&quot;&gt;Shortest path by Bellman-Ford&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Graph with |V| vertices and |E| edges&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(|V||E|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(|V||E|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(|V|)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&quot;sorting&quot;&gt;
Sorting&lt;/h2&gt;
&lt;table class=&quot;table table-bordered table-striped&quot;&gt;
  &lt;thead&gt;
&lt;tr&gt;
      &lt;th&gt;Algorithm&lt;/th&gt;
      &lt;th&gt;Data Structure&lt;/th&gt;
      &lt;th colspan=&quot;3&quot;&gt;Time Complexity&lt;/th&gt;
      &lt;th colspan=&quot;3&quot;&gt;Worst Case Auxiliary Space Complexity&lt;/th&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;Best&lt;/th&gt;
      &lt;th&gt;Average&lt;/th&gt;  
      &lt;th&gt;Worst&lt;/th&gt;  
      &lt;th&gt;Worst&lt;/th&gt;                
    &lt;/tr&gt;
&lt;/thead&gt;
  &lt;tbody&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Quicksort&quot;&gt;Quicksort&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Array&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(n log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n^2)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Merge_sort&quot;&gt;Mergesort&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Array&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(n log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(n log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Heapsort&quot;&gt;Heapsort&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Array&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(n log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(n log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Bubble_sort&quot;&gt;Bubble Sort&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Array&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n^2)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n^2)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Insertion_sort&quot;&gt;Insertion Sort&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Array&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n^2)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n^2)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Selection_sort&quot;&gt;Select Sort&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Array&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n^2)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n^2)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n^2)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Bucket_sort&quot; title=&quot;Only for integers with range k&quot;&gt;Bucket Sort&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;Array&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(n+k)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(n+k)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n^2)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(nk)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Radix_sort&quot; title=&quot;Constant number of digits &#39;k&#39;&quot;&gt;Radix Sort&lt;/a&gt;       &lt;/td&gt;
      &lt;td&gt;Array&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(nk)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(nk)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(nk)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n+k)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&quot;data-structures&quot;&gt;
Data Structures&lt;/h2&gt;
&lt;table class=&quot;table table-bordered table-striped&quot;&gt;
  &lt;thead&gt;
&lt;tr&gt;
      &lt;th&gt;Data Structure&lt;/th&gt;
      &lt;th colspan=&quot;8&quot;&gt;Time Complexity&lt;/th&gt;
      &lt;th&gt;Space Complexity&lt;/th&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th colspan=&quot;4&quot;&gt;Average&lt;/th&gt;  
      &lt;th colspan=&quot;4&quot;&gt;Worst&lt;/th&gt;                
      &lt;th&gt;Worst&lt;/th&gt;                
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;Indexing&lt;/th&gt;
      &lt;th&gt;Search&lt;/th&gt;
      &lt;th&gt;Insertion&lt;/th&gt;  
      &lt;th&gt;Deletion&lt;/th&gt;  
      &lt;th&gt;Indexing&lt;/th&gt;
      &lt;th&gt;Search&lt;/th&gt;
      &lt;th&gt;Insertion&lt;/th&gt;  
      &lt;th&gt;Deletion&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;a href=&quot;http://en.wikipedia.org/wiki/Array_data_structure&quot;&gt;Basic Array&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code&gt;-&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code&gt;-&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code&gt;-&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code&gt;-&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Dynamic_array&quot;&gt;Dynamic Array&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Singly_linked_list#Singly_linked_lists&quot;&gt;Singly-Linked List&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Doubly_linked_list&quot;&gt;Doubly-Linked List&lt;/a&gt;&lt;/td&gt;

      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Skip_list&quot;&gt;Skip List&lt;/a&gt;&lt;/td&gt;

    &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n log(n))&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Hash_table&quot;&gt;Hash Table&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code&gt;-&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code&gt;-&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Binary_search_tree&quot;&gt;Binary Search Tree&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Cartesian_tree&quot;&gt;Cartresian Tree&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code&gt;-&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code&gt;-&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;red&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/B_tree&quot;&gt;B-Tree&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Red-black_tree&quot;&gt;Red-Black Tree&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Splay_tree&quot;&gt;Splay Tree&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code&gt;-&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code&gt;-&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/AVL_tree&quot;&gt;AVL Tree&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;green&quot;&gt;O(log(n))&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;yellow&quot;&gt;O(n)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&quot;heaps&quot;&gt;
Heaps&lt;/h2&gt;
&lt;table class=&quot;table table-bordered table-striped&quot;&gt;
    &lt;thead&gt;
&lt;tr&gt;
            &lt;th&gt;Heaps&lt;/th&gt;
            &lt;th colspan=&quot;7&quot;&gt;Time Complexity&lt;/th&gt;
        &lt;/tr&gt;
&lt;tr&gt;
            &lt;th&gt;&lt;/th&gt;
            &lt;th&gt;Heapify&lt;/th&gt;
            &lt;th&gt;Find Max&lt;/th&gt;  
            &lt;th&gt;Extract Max&lt;/th&gt;  
            &lt;th&gt;Increase Key&lt;/th&gt;
            &lt;th&gt;Insert&lt;/th&gt;
            &lt;th&gt;Delete&lt;/th&gt;
            &lt;th&gt;Merge&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;a href=&quot;http://en.wikipedia.org/wiki/Linked_list&quot;&gt;Linked List (sorted)&lt;/a&gt;&lt;/td&gt;
            &lt;td&gt;&lt;code&gt;-&lt;/code&gt;&lt;/td&gt; &amp;lt;!-- heapify &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/body&gt;&lt;/html&gt;
            &lt;br /&gt;
&lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;br /&gt;
 &lt;!-- Find max --&gt;
            &lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;br /&gt;
 &lt;!-- Extract max --&gt;
            &lt;code class=&quot;&amp;quot;red&amp;quot;&quot;&gt;O(n)&lt;/code&gt;&lt;br /&gt;
 &lt;!-- Increase --&gt;
            &lt;code class=&quot;&amp;quot;red&amp;quot;&quot;&gt;O(n)&lt;/code&gt;&lt;br /&gt;
 &lt;!-- insert --&gt;
            &lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;br /&gt;
 &lt;!-- delete --&gt;
            &lt;code class=&quot;&amp;quot;red&amp;quot;&quot;&gt;O(m+n)&lt;/code&gt;&lt;br /&gt;
 &lt;!-- merge --&gt;
        &lt;/div&gt;
&lt;br /&gt;
&lt;a href=&quot;https://www.blogger.com/%22http://en.wikipedia.org/wiki/Linked_list%22&quot;&gt;Linked List (unsorted)&lt;/a&gt;&lt;br /&gt;

            &lt;code&gt;-&lt;/code&gt;&lt;br /&gt;
 &lt;!-- heapify --&gt;
            &lt;code class=&quot;&amp;quot;red&amp;quot;&quot;&gt;O(n)&lt;/code&gt;&lt;br /&gt;
 &lt;!-- Find max --&gt;
            &lt;code class=&quot;&amp;quot;red&amp;quot;&quot;&gt;O(n)&lt;/code&gt;&lt;br /&gt;
 &lt;!-- Extract max --&gt;
            &lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;br /&gt;
 &lt;!-- Increase --&gt;
            &lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;br /&gt;
 &lt;!-- insert --&gt;
            &lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;br /&gt;
 &lt;!-- delete --&gt;
            &lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;br /&gt;
 &lt;!-- merge --&gt;
        

            &lt;a href=&quot;https://www.blogger.com/%22http://en.wikipedia.org/wiki/Binary_heap%22&quot;&gt;Binary Heap&lt;/a&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(n)&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(log(n))&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(log(n))&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(log(n))&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(log(n))&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;red&amp;quot;&quot;&gt;O(m+n)&lt;/code&gt;&lt;br /&gt;

        

            &lt;a href=&quot;https://www.blogger.com/%22http://en.wikipedia.org/wiki/Binomial_heap%22&quot;&gt;Binomial Heap&lt;/a&gt;&lt;br /&gt;

            &lt;code&gt;-&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(log(n))&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(log(n))&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(log(n))&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(log(n))&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(log(n))&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(log(n))&lt;/code&gt;&lt;br /&gt;

        

            &lt;a href=&quot;https://www.blogger.com/%22http://en.wikipedia.org/wiki/Fibonacci_heap%22&quot;&gt;Fibonacci Heap&lt;/a&gt;&lt;br /&gt;

            &lt;code&gt;-&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot; title=&quot;&amp;quot;Amortized&amp;quot;&quot;&gt;O(log(n))*&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;green&amp;quot;&quot; title=&quot;&amp;quot;Amortized&amp;quot;&quot;&gt;O(1)*&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot; title=&quot;&amp;quot;Amortized&amp;quot;&quot;&gt;O(log(n))*&lt;/code&gt;&lt;br /&gt;

            &lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;br /&gt;

        
&lt;br /&gt;
&lt;h2 id=&quot;&amp;quot;graphs&amp;quot;&quot;&gt;
Graphs&lt;/h2&gt;
&lt;table class=&quot;&amp;quot;table&quot; quot=&quot;&quot; table-bordered=&quot;&quot; table-striped=&quot;&quot;&gt;
  &lt;tbody&gt;
&lt;tr&gt;
      &lt;th&gt;Node / Edge Management&lt;/th&gt;
      &lt;th&gt;Storage&lt;/th&gt;
      &lt;th&gt;Add Vertex&lt;/th&gt;
      &lt;th&gt;Add Edge&lt;/th&gt;
      &lt;th&gt;Remove Vertex&lt;/th&gt;
      &lt;th&gt;Remove Edge&lt;/th&gt;
      &lt;th&gt;Query&lt;/th&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://www.blogger.com/%22http://en.wikipedia.org/wiki/Adjacency_list%22&quot;&gt;Adjacency list&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(|V|+|E|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(|V| + |E|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(|E|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(|V|)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://www.blogger.com/%22http://en.wikipedia.org/wiki/Incidence_list%22&quot;&gt;Incidence list&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(|V|+|E|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(|E|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(|E|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(|E|)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://www.blogger.com/%22http://en.wikipedia.org/wiki/Adjacency_matrix%22&quot;&gt;Adjacency matrix&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;red&amp;quot;&quot;&gt;O(|V|^2)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;red&amp;quot;&quot;&gt;O(|V|^2)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;red&amp;quot;&quot;&gt;O(|V|^2)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;green&amp;quot;&quot;&gt;O(1)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https://www.blogger.com/%22http://en.wikipedia.org/wiki/Incidence_matrix%22&quot;&gt;Incidence matrix&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;red&amp;quot;&quot;&gt;O(|V| ⋅ |E|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;red&amp;quot;&quot;&gt;O(|V| ⋅ |E|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;red&amp;quot;&quot;&gt;O(|V| ⋅ |E|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;red&amp;quot;&quot;&gt;O(|V| ⋅ |E|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;red&amp;quot;&quot;&gt;O(|V| ⋅ |E|)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;&amp;quot;yellow&amp;quot;&quot;&gt;O(|E|)&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&quot;&amp;quot;growth&amp;quot;&quot;&gt;
Notation for asymptotic growth&lt;/h2&gt;
&lt;table class=&quot;&amp;quot;table&quot; quot=&quot;&quot; table-bordered=&quot;&quot; table-striped=&quot;&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
    &lt;th&gt;letter&lt;/th&gt;
    &lt;th&gt;bound&lt;/th&gt;  
    &lt;th&gt;growth&lt;/th&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;(theta) Θ&lt;/td&gt;
    &lt;td&gt;upper and lower, tight&lt;a href=&quot;https://www.blogger.com/%22http://www.blogger.com/blogger.g?blogID=8700135416943985114#footnote-1&amp;quot;&quot;&gt;&lt;sup&gt;[1]&lt;/sup&gt;&lt;/a&gt;&lt;/td&gt;
    &lt;td&gt;equal&lt;a href=&quot;https://www.blogger.com/%22http://www.blogger.com/blogger.g?blogID=8700135416943985114#footnote-1&amp;quot;&quot;&gt;&lt;sup&gt;[2]&lt;/sup&gt;&lt;/a&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;(big-oh) O&lt;/td&gt;
    &lt;td&gt;upper, tightness unknown&lt;/td&gt;
    &lt;td&gt;less than or equal&lt;a href=&quot;https://www.blogger.com/%22http://www.blogger.com/blogger.g?blogID=8700135416943985114#footnote-1&amp;quot;&quot;&gt;&lt;sup&gt;[3]&lt;/sup&gt;&lt;/a&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;(small-oh) o&lt;/td&gt;
    &lt;td&gt;upper, not tight&lt;/td&gt;
    &lt;td&gt;less than&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;(big omega) Ω&lt;/td&gt;
    &lt;td&gt;lower, tightness unknown&lt;/td&gt;
    &lt;td&gt;greater than or equal&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;(small omega) ω&lt;/td&gt;
    &lt;td&gt;lower, not tight&lt;/td&gt;
    &lt;td&gt;greater than&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div id=&quot;&amp;quot;footnote-1&amp;quot;&quot;&gt;
[1] Big O is the upper bound, while Omega is the lower bound. Theta requires both Big O and Omega, so that&#39;s why it&#39;s referred to as a tight bound (it must be both the upper and lower bound). For example, an algorithm taking Omega(n log n) takes at least n log n time but has no upper limit. An algorithm taking Theta(n log n) is far preferential since it takes AT LEAST n log n (Omega n log n) and NO MORE THAN n log n (Big O n log n).&lt;sup&gt;&lt;a href=&quot;https://www.blogger.com/%22http://stackoverflow.com/a/464081/412916%22&quot;&gt;SO&lt;/a&gt;&lt;/sup&gt;&lt;/div&gt;
&lt;div id=&quot;&amp;quot;footnote-2&amp;quot;&quot;&gt;
[2] f(x)=Θ(g(n)) means f (the running time of the algorithm) grows exactly like g when n (input size) gets larger. In other words, the growth rate of f(x) is asymptotically proportional to g(n).&lt;/div&gt;
&lt;div id=&quot;&amp;quot;footnote-1&amp;quot;&quot;&gt;
[3] Same thing. Here the growth rate is no faster than g(n). big-oh is the most useful because represents the worst-case behavior.&lt;/div&gt;
In short, if algorithm is __ then its performance is __ 
&lt;br /&gt;
&lt;table class=&quot;&amp;quot;table&quot; quot=&quot;&quot; table-bordered=&quot;&quot; table-striped=&quot;&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
    &lt;th&gt;algorithm&lt;/th&gt;
    &lt;th&gt;performance&lt;/th&gt;  
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;o(n)&lt;/td&gt;
    &lt;td&gt;&amp;lt; n&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;O(n)&lt;/td&gt;
    &lt;td&gt;≤ n&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;Θ(n)&lt;/td&gt;
    &lt;td&gt;= n&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;Ω(n)&lt;/td&gt;
    &lt;td&gt;≥ n&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;ω(n)&lt;/td&gt;
    &lt;td&gt;&amp;gt; n&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;&amp;quot;chart&amp;quot;&quot;&gt;
Big-O Complexity Chart&lt;/h2&gt;
&lt;img alt=&quot;&amp;quot;Big&quot; complexity=&quot;&quot; graph=&quot;&quot; o=&quot;&quot; quot=&quot;&quot; src=&quot;&amp;quot;http://bigocheatsheet.com/img/big-o-complexity.png&amp;quot;&quot; title=&quot;&amp;quot;Big&quot; width=&quot;&amp;quot;500&amp;quot;&quot; /&gt;


--&amp;gt;


&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/2246334925648550213/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2013/10/blog-post_27.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/2246334925648550213'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/2246334925648550213'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2013/10/blog-post_27.html' title='Know thy Complexities !!'/><author><name>srikanth</name><uri>http://www.blogger.com/profile/16203849087897910819</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-8965641299585538159</id><published>2013-08-23T00:59:00.002-07:00</published><updated>2013-08-23T04:01:19.129-07:00</updated><title type='text'>&quot;Query end&quot; state in Mysql</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&quot;Query end&quot; state in MySql appears to consume a lot of time which becomes a bottleneck for insert operations. &lt;br /&gt;
&lt;br /&gt;
I had written a stored Procedure in Mysql which implements a cursor to iterate over the rows of a table. This cursor worked well on my machine but it took a lot of time in the live environment (POC setup).&lt;br /&gt;
&lt;br /&gt;
Initial culprit seemed to be the hardware but after analysing the procedure with &quot;show processlist&quot;, I found that every insert would used to get stuck at query insert state.&lt;br /&gt;
&lt;br /&gt;
Incase any of you guys come across similar issue, you can edit your my.cnf file (I think my.ini for windows) and change this&lt;br /&gt;
&lt;br /&gt;
innodb_flush_log_at_trx_commit = 1&lt;br /&gt;
to&lt;br /&gt;
innodb_flush_log_at_trx_commit = 0 or innodb_flush_log_at_trx_commit = 2.&lt;br /&gt;
&lt;br /&gt;
This will drastically improve the performance but it also prohibits the innodb engine to serve ACID properties.&lt;br /&gt;
&lt;br /&gt;
After reading some forums I found that the above parameter can be changed if you are ready to incur a loss of one statement in case of system crash(not Mysql) or power failure.&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/8965641299585538159/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2013/08/query-end-state-in-mysql.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/8965641299585538159'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/8965641299585538159'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2013/08/query-end-state-in-mysql.html' title='&quot;Query end&quot; state in Mysql'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-7535440732506702882</id><published>2013-07-29T01:17:00.000-07:00</published><updated>2013-07-30T00:17:06.351-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="1000000007"/><category scheme="http://www.blogger.com/atom/ns#" term="Fermats theorem"/><category scheme="http://www.blogger.com/atom/ns#" term="inverse"/><category scheme="http://www.blogger.com/atom/ns#" term="large computation"/><category scheme="http://www.blogger.com/atom/ns#" term="techg"/><title type='text'>What is 1000000007 ?</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Hello Guys,&lt;br /&gt;
&lt;br /&gt;
As a newbie on online Coding contests, I was&amp;nbsp;initially&amp;nbsp;fed-up with&amp;nbsp;1000000007.I hardly understood the logic behind it.&lt;br /&gt;
&lt;br /&gt;
Some guys give an hint that &quot;its the first 10 digit prime..&quot;, so what ?&lt;br /&gt;
There were 2 questions in my mind.&lt;br /&gt;
1) Why only a prime.&lt;br /&gt;
2) Why not 2nd or 3rd prime&lt;br /&gt;
&lt;br /&gt;
Instantly I got an partial answer from myself for the first question, A prime because we need (ans % Prime) as the actual answer, so the o/p should be unique.&lt;br /&gt;
&lt;br /&gt;
But, there was a lot behind that word Prime in this case (Explained later)&lt;br /&gt;
&lt;br /&gt;
The 2nd question what still open, but lately I understood that&amp;nbsp;1000000007 is easy to represent as 10^9+7. Any greater Prime would have served the purpose but this was easy to write. Infact some times we find the 2nd 10 digit prime which is again easy to represent&amp;nbsp;1000000009.&lt;br /&gt;
&lt;br /&gt;
This would have been very well understood by everyone now.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Coming to the actual point.&lt;br /&gt;
&lt;br /&gt;
All of us might have noticed that&amp;nbsp;1000000007 comes only when our answers are expected to be very large, more commonly when we implement combinations n(c,k) or nCk.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
The combination formula is n!/(r!(n-r)!).&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
We are asked mod operation with&amp;nbsp;1000000007 for 2 reasons,&lt;br /&gt;
1)&amp;nbsp;Obviously, so as to restrict our answer to 10 digit and&lt;br /&gt;
2) Help us with large multiplications and divisions.&lt;br /&gt;
&lt;br /&gt;
Did u read Division ? yes !!!&lt;br /&gt;
&lt;br /&gt;
We must know that (%) is distributive over multiplication,addition etc, but not over division.&lt;br /&gt;
So, how to do large divisions ?&lt;br /&gt;
&lt;br /&gt;
There`s a theorem known as &quot;Fermat&#39;s&amp;nbsp;little theorem&quot;.&lt;br /&gt;
It say`s that (A/B) % MOD = ((A % MOD)*(B^(MOD-2) % MOD))%MOD (You can refer wiki for actual theorem)&lt;br /&gt;
&lt;br /&gt;
Now, we can see that,&amp;nbsp;Fermat&#39;s&amp;nbsp;theorem essentially convert all our division to multiplication.So we can use our mod (%) operation.&lt;br /&gt;
&lt;br /&gt;
If you have any issues/queries, please comment the same... I will try my best to answer it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/7535440732506702882/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2013/07/what-is-1000000007.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/7535440732506702882'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/7535440732506702882'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2013/07/what-is-1000000007.html' title='What is 1000000007 ?'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-6570689126129977935</id><published>2013-07-28T23:09:00.003-07:00</published><updated>2013-07-30T00:18:02.315-07:00</updated><title type='text'>Techgig Monthend challenge : Foreign Trip : Level Easy</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;h2 class=&quot;cc-hd2 mrgn-lft15&quot; id=&quot;heading&quot; style=&quot;background-color: white; color: #e03f00; font-family: arial, Georgia, &#39;Times New Roman&#39;, Times, serif; font-size: 13px; line-height: 18px; margin-bottom: 8px; margin-left: 15px !important; margin-right: 0px; margin-top: 0px; padding: 0px;&quot;&gt;
Problem Statement&lt;/h2&gt;
&lt;div class=&quot;tp-txt&quot; id=&quot;description&quot; style=&quot;background-color: white; margin: 0px 0px 10px 15px; padding: 0px;&quot;&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; padding: 0px;&quot;&gt;
A company organizes two foreign trips for its employees yearly. Aim of the trip is to increase interaction among the employees of the company and hence company wants each of his employee to see new people on the trip and not even a single person with whom he has worked in past. Therefore it is a rule in company that in any of the trips, all the employees should be new to each other and no two of them should have worked together in past.&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; padding: 0px;&quot;&gt;
&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;
Given the work history of each employee (which people he has worked with sometimes in past), you have to tell whether all of the employees can be accommodated within trips without violating the above rule or not. Each employee is given a unique integer ID by which they are recognized. You can also assume that each employee has worked with at least one other employee in past.&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; padding: 0px;&quot;&gt;
&lt;strong style=&quot;margin: 0px; padding: 0px;&quot;&gt;Note:&amp;nbsp;&lt;/strong&gt;No employee can be in both trips and every employee must be part of one trip.&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; padding: 0px;&quot;&gt;
&lt;/div&gt;
&lt;h2 class=&quot;cc-hd2 mrgn-lft15&quot; id=&quot;heading&quot; style=&quot;color: #e03f00; font-family: arial, Georgia, &#39;Times New Roman&#39;, Times, serif; font-size: 13px; line-height: 18px; margin-bottom: 8px; margin-left: 15px !important; margin-right: 0px; margin-top: 0px; padding: 0px;&quot;&gt;
&lt;/h2&gt;
&lt;h2 class=&quot;cc-hd2 mrgn-lft15&quot; id=&quot;heading&quot; style=&quot;color: #e03f00; font-family: arial, Georgia, &#39;Times New Roman&#39;, Times, serif; font-size: 13px; line-height: 18px; margin-bottom: 8px; margin-left: 15px !important; margin-right: 0px; margin-top: 0px; padding: 0px;&quot;&gt;
Examples&lt;/h2&gt;
&lt;div class=&quot;tp-txt&quot; id=&quot;examples&quot; style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px 0px 10px 15px; padding: 0px;&quot;&gt;
&lt;strong style=&quot;margin: 0px; padding: 0px;&quot;&gt;Example:&amp;nbsp;&lt;/strong&gt;&lt;br /&gt;
&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;
i) Suppose the work history is given as follows: {(1,2),(2,3),(3,4)}; then it is possible to accommodate all the four employees in two trips (one trip consisting of employees 1&amp;amp; 3 and other having employees 2 &amp;amp; 4). Neither of the two employees in the same trip have worked together in past.&lt;br /&gt;
&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;
ii) Suppose the work history is given as {(1,2),(1,3),(2,3)} then there is no way possible to have two trips satisfying the company rule and accommodating all the employees.&lt;/div&gt;
&lt;div class=&quot;tp-txt&quot; id=&quot;examples&quot; style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px 0px 10px 15px; padding: 0px;&quot;&gt;
==========================================================================&lt;/div&gt;
&lt;div class=&quot;tp-txt&quot; id=&quot;examples&quot; style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px 0px 10px 15px; padding: 0px;&quot;&gt;
This is an old Question on Techgig, but lets discuss it as we are finding it difficult to approach the problem.&lt;/div&gt;
&lt;div class=&quot;tp-txt&quot; id=&quot;examples&quot; style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px 0px 10px 15px; padding: 0px;&quot;&gt;
Well, I had solved this question few months back and scored just 90. Unable to 100 (Maybe I am missing a corner case, not sure though).&lt;/div&gt;
&lt;div class=&quot;tp-txt&quot; id=&quot;examples&quot; style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px 0px 10px 15px; padding: 0px;&quot;&gt;
I can`t recollect the exact thought process of mine towards this problem but I will explain the final approach used by me.&lt;/div&gt;
&lt;div class=&quot;tp-txt&quot; id=&quot;examples&quot; style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px 0px 10px 15px; padding: 0px;&quot;&gt;
As per the question, they are giving us the set of employees who have worked with each other. So lets keep a mapping of the employees who have worked with each other.&lt;/div&gt;
&lt;div class=&quot;tp-txt&quot; id=&quot;examples&quot; style=&quot;margin: 0px 0px 10px 15px; padding: 0px;&quot;&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px;&quot;&gt;
Then, if an employee has worked with someone, check for all combinations of employee such that the intersection should not be marked as &quot;1&quot; (i.e worked with each other)&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px;&quot;&gt;
Here is the code : (Fetches only 90)&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;#include &lt;stdio .h=&quot;&quot;&gt;&lt;stdio .h=&quot;&quot;&gt;&lt;stdio .h=&quot;&quot;&gt;&lt;/stdio&gt;&lt;/stdio&gt;&lt;/stdio&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;#include &lt;stdlib .h=&quot;&quot;&gt;&lt;stdlib .h=&quot;&quot;&gt;&lt;string .h=&quot;&quot;&gt;&lt;/string&gt;&lt;/stdlib&gt;&lt;/stdlib&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;char* emp_violating(int input1,char* input2)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; int i=0,j=0,k=0;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; int a=0,b=0;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; int map[input1+1][input1+1];&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; int cnt[input1+1];&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; for(i=0;i&amp;lt;=input1;i++)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; { &amp;nbsp; &amp;nbsp;for(j=0;j&amp;lt;=input1;j++)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; map[i][j]=0;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cnt[i]=0;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; while(1)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; while(*input2 != &#39;(&#39;)&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{input2++;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; input2++;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; while(*input2&amp;gt;=&#39;0&#39; &amp;amp;&amp;amp; *input2&amp;lt;=&#39;9&#39;) &amp;nbsp; &amp;nbsp;/*For more than 1 digit numbers*/&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a=a*10 + (int)*input2++ - &#39;0&#39;;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; while(*input2 != &#39;,&#39;)&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; input2++;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; input2++;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; while(*input2&amp;gt;=&#39;0&#39; &amp;amp;&amp;amp; *input2&amp;lt;=&#39;9&#39;) &amp;nbsp; &amp;nbsp;/*For more than 1 digit numbers*/&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; b=b*10 + (int)*input2++ - &#39;0&#39;;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; map[a][b]=1;map[b][a]=1; /*Mark that both guys have been with each other*/&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cnt[a]++;cnt[b]++; /*Increment the count to identify that they have been to a picnic. This can also just set to be dirty*/&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; input2++;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(*input2!=&#39;,&#39;)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; break;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a=0;b=0;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; for(i=1;i&amp;lt;=input1;i++)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(cnt[i]&amp;gt;1)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(j=1;j&amp;lt;=input1;j++)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(k=j+1;k&lt;input1 input1=&quot;&quot; k=&quot;&quot; span=&quot;&quot;&gt;&lt;/input1&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(map[i][j]==1 &amp;amp;&amp;amp; map[i][k]==1)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(map[j][k]==1) return &quot;no&quot;;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; return &quot;yes&quot;;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; line-height: 17px;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;int main() /*Driver*/&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; char *a=&quot;(10,2),(2,3),(3,4)&quot;;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; printf(&quot;%s\n&quot;,emp_violating(10,a));&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; return 0;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;tp-txt&quot; id=&quot;examples&quot; style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px 0px 10px 15px; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;tp-txt&quot; id=&quot;examples&quot; style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px 0px 10px 15px; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/6570689126129977935/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2013/07/techgig-monthend-challenge-foreign-trip.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/6570689126129977935'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/6570689126129977935'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2013/07/techgig-monthend-challenge-foreign-trip.html' title='Techgig Monthend challenge : Foreign Trip : Level Easy'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-8625085742862911514</id><published>2013-07-15T02:52:00.001-07:00</published><updated>2013-07-15T05:16:03.628-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="josephus problem"/><category scheme="http://www.blogger.com/atom/ns#" term="july"/><category scheme="http://www.blogger.com/atom/ns#" term="Techgig"/><title type='text'>Techgig July contest : Musical Pillow : Level (Medium)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;h2 class=&quot;cc-hd2 mrgn-lft15&quot; id=&quot;heading&quot; style=&quot;background-color: white; color: #e03f00; font-family: arial, Georgia, &#39;Times New Roman&#39;, Times, serif; font-size: 13px; line-height: 18px; margin-bottom: 8px; margin-left: 15px !important; margin-right: 0px; margin-top: 0px; padding: 0px;&quot;&gt;
Problem Statement&lt;/h2&gt;
&lt;div class=&quot;tp-txt&quot; id=&quot;description&quot; style=&quot;background-color: white; margin: 0px 0px 10px 15px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;In a family party, people are playing the musical pillow game in which people&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;b style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;sit in a circle&lt;/b&gt;&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;. Starting from the first person, they transfer the pillow to the person to their left. For simplicity, we’ll assume that people are assigned an integer tag from&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;b style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;1 to n&lt;/b&gt;&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;(which is the total number of people) in the clockwise manner, so that during the game pillow is passed in the increasing sequence of. When game starts, a song is played and the person with the pillow at the end of the song, is eliminated and game starts again till there is only one person is left, which is declared as the winner of the game.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; You are give&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;total number of people playing the game&lt;/b&gt;&amp;nbsp;and the&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;duration of the song in seconds&lt;/b&gt;. If game starts with pillow being put on a table &amp;amp; person1 (person with tag number 1)picking it up(at the end of first second [time], pillow will be with person1) and each person takes one second to transfer the pillow to the next person, you have to tell which person (tag number) which will win the game.&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
&lt;h2 class=&quot;cc-hd2 mrgn-lft15&quot; id=&quot;heading&quot; style=&quot;color: #e03f00; font-family: arial, Georgia, &#39;Times New Roman&#39;, Times, serif; font-size: 13px; line-height: 18px; margin-bottom: 8px; margin-left: 15px !important; margin-right: 0px; margin-top: 0px; padding: 0px;&quot;&gt;
&lt;/h2&gt;
&lt;h2 class=&quot;cc-hd2 mrgn-lft15&quot; id=&quot;heading&quot; style=&quot;color: #e03f00; font-family: arial, Georgia, &#39;Times New Roman&#39;, Times, serif; font-size: 13px; line-height: 18px; margin-bottom: 8px; margin-left: 15px !important; margin-right: 0px; margin-top: 0px; padding: 0px;&quot;&gt;
Examples&lt;/h2&gt;
&lt;div class=&quot;tp-txt&quot; id=&quot;examples&quot; style=&quot;margin: 0px 0px 10px 15px; padding: 0px;&quot;&gt;
&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Example 1:&lt;/b&gt;&amp;nbsp;Let there are 5 people and duration of the song is 2 seconds(every second person present in the clockwise manner will be eliminated) then people will be eliminated in the following order: 2,4,1, 5 and person with tag 3 will be the winner.&lt;br /&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;b style=&quot;font-size: 15px; margin: 0px; padding: 0px;&quot;&gt;Example 2:&lt;/b&gt;&amp;nbsp;If there are 11 people in the beginning of the game, and song duration is 4 seconds then people will be eliminated in following order: 4,8,1,6,11, 7, 3,2,5, 10 and winner will be the person with tag no. 9.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
==============================================================================&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
Dont know why they called it as a medium level problem after that Bitonic problem (Easy level) which ruined a couple of days of my life.. :(&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;Lets come to the problem, its a very easy problem to solve. A new programmer would face only one problem i.e to start the loop from first position when we reach the end of our&amp;nbsp;sequence.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;Here`s the logic :&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;i) First we declare an array with the number of persons (input1) and fill it with 0.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;ii) Then, as the games starts and a person gets out, that position is to be marked as 1. Because while the next round this position needs to be ignored as the person is already out.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;ii) Continue till only a single person remains.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;The tricky point here is, how to traverse through the loop. The answer to this is, just maintain a flag which is updated only when we encounter a 0 at that position in the array else just move on until flag becomes equal to input2. In case, we reach the end of array, start from 1 again without resetting the flag.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;If you have doubts or you want the source code for reference the comment below with your email address.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
&lt;br /&gt;
EDIT: Just learnt to find that this is called as &quot;Josephus Problem&quot; and a recursive solution can be written for it.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/8625085742862911514/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2013/07/techgig-july-contest-musical-pillow.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/8625085742862911514'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/8625085742862911514'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2013/07/techgig-july-contest-musical-pillow.html' title='Techgig July contest : Musical Pillow : Level (Medium)'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-2928175509325074985</id><published>2013-07-08T05:46:00.002-07:00</published><updated>2013-07-08T21:49:53.023-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="dynamic programming."/><category scheme="http://www.blogger.com/atom/ns#" term="may"/><category scheme="http://www.blogger.com/atom/ns#" term="partition problem"/><category scheme="http://www.blogger.com/atom/ns#" term="Techgig"/><title type='text'>Techgig May contest : Partition problem : Level Easy</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;b&gt;Problem Statement&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;tp-txt&quot; id=&quot;description&quot; style=&quot;background-color: white; color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px 0px 10px 15px; padding: 0px;&quot;&gt;
You are given a set of positive integers. your task is to solve partition problem.&lt;br /&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
Lets define the partition problem.&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
Partition problem is the task of deciding whether a given multiset of positive integers can be partitioned into two subsets S1 and S2 such that the sum of the elements in S1 equals the sum of the elements in S2.&lt;br /&gt;
&lt;br /&gt;
&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Example 1:&lt;/b&gt;&lt;br /&gt;
array[] = {1, 5, 11, 5}&lt;br /&gt;
&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Output:&lt;/b&gt;&amp;nbsp;Yes&lt;br /&gt;
This array can be divided into to subsets {1, 5, 5} and {11} these two have equal sum.&lt;br /&gt;
&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;
&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Example 2:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
array[] = {1, 5, 3}&lt;br /&gt;
&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Output:&lt;/b&gt;&amp;nbsp;No&lt;br /&gt;
This array cant be divided into two subsets of equal sum.&lt;br /&gt;
&lt;br /&gt;
===============================================================================&lt;br /&gt;
&lt;br /&gt;
The level set was easy but this was really a nice question, took half hour to code it.. :P&lt;br /&gt;
&lt;br /&gt;
Well, Most of us get the answer just by reading the question... &quot;Divide the array such that sum of each partition is equal&quot;, but as soon we we try to write the code, out mind keeps wondering &quot;how the hell do I implement this ?&quot;&lt;br /&gt;
&lt;br /&gt;
I thought for some time and then realized that, The worst case of forming the partitions for N element is; One partition of 0 elements and the other of N elements, but this wont help us.&lt;br /&gt;
&lt;br /&gt;
Now lets move forward in the same line. One partition of N-1 elements and the other of 1 element.This is our first step.&lt;br /&gt;
&lt;br /&gt;
This can be easily achieved with a for loop. But, the problem is, there are many different ways for selection 2 partitions each of N-1 and 1 elements respectively. To be precise they are N ways.&lt;br /&gt;
&lt;br /&gt;
First thing to be taken care of is to cover all the N ways. Next, every time the partition should be increase and decrease respectively keeping in mind all the cases.&lt;br /&gt;
&lt;br /&gt;
Here is the code,&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;#include &lt;stdio .h=&quot;&quot;&gt;&lt;stdio .h=&quot;&quot;&gt;&lt;/stdio&gt;&lt;/stdio&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;#include &lt;stdlib .h=&quot;&quot;&gt;&lt;string .h=&quot;&quot;&gt;&lt;/string&gt;&lt;/stdlib&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;
&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;char* partition(int input1[])&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int i=0,n,total=0,loop,round,s1=0;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;while(input1[i]&amp;gt;-1)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(input1[i]==0)return &quot;Invalid&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; total+=input1[i++];&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;n=i;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;round=0;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;while(round &amp;lt; N)&lt;n span=&quot;&quot;&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/span&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; loop=round;&lt;/span&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(i=0;i&amp;lt;=round-1;i++)&lt;/span&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; s1=s1+input1[i];&lt;/span&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; while(loop &amp;lt; round)&lt;n span=&quot;&quot;&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/span&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;s1=s1+input1[loop];&lt;/span&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if(s1==(total-s1))return &quot;Yes&quot;;&lt;/span&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;s1=s1-input1[loop];&lt;/span&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;loop++;&lt;/span&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/span&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;s1=0;&lt;/span&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;round++;&lt;/span&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return &quot;No&quot;;&lt;/span&gt;&lt;br style=&quot;background-color: #fefefe; color: #333333; line-height: 20px; margin: 0px; padding: 0px;&quot; /&gt;&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;}&lt;/span&gt;&lt;/n&gt;&lt;/span&gt;&lt;/n&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: #fefefe; color: #333333; line-height: 20px;&quot;&gt;Let me know if you have any doubts.&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/2928175509325074985/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2013/07/techgig-may-contest-partition-problem.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/2928175509325074985'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/2928175509325074985'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2013/07/techgig-may-contest-partition-problem.html' title='Techgig May contest : Partition problem : Level Easy'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-8033572839106639295</id><published>2013-07-08T04:18:00.000-07:00</published><updated>2013-07-08T21:53:46.592-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="june"/><category scheme="http://www.blogger.com/atom/ns#" term="Techgig"/><title type='text'>Techgig June contest : Disks in a Pile : Level (Easy)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h2 class=&quot;cc-hd2 mrgn-lft15&quot; id=&quot;heading&quot; style=&quot;background-color: white; color: #e03f00; font-family: arial, Georgia, &#39;Times New Roman&#39;, Times, serif; font-size: 13px; line-height: 18px; margin-bottom: 8px; margin-left: 15px !important; margin-right: 0px; margin-top: 0px; padding: 0px;&quot;&gt;
Problem Statement&lt;/h2&gt;
&lt;div class=&quot;tp-txt&quot; id=&quot;description&quot; style=&quot;background-color: white; margin: 0px 0px 10px 15px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;Rahul is playing a very interesting game. He has&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;b style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;N disks&lt;/b&gt;&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;b style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;( each of equal radius).&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
Every disk has a distinct number out of&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;1 to N&lt;/b&gt;&amp;nbsp;associated with it. Disks are placed one over other in&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;a single pile&lt;/b&gt;.&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
Rahul wants to&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;sort this pile of disk in increasing order from top to bottom.&lt;/b&gt;&amp;nbsp;But he has a very very special method of doing this. In a&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;single step&lt;/b&gt;&amp;nbsp;he can only choose one disk out of the pile and he can only put it at the top.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
Rahul wants to sort his pile of disks in&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;minimum number of possible steps&lt;/b&gt;. So help Rahul in doing so. So don&#39;t have to show the actual steps just answer the&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;minimum number of possible steps to sort the pile&lt;/b&gt;&amp;nbsp;so that that Rahul can check whether he is doing his work right or wrong&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
===============================================================================&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
This was a very easy question, but Some of us found it difficult.&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
Here goes the explanation,&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;If you write it down on a paper then you will notice that We first track the highest numbered disk and then just move the disks with the decreasing number on top of it.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;For example lets take 3 4 5 1 2 &amp;nbsp;as the numbering.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;Here, since 5 is the highest number I will move only 2 and then 1 on top of it, so the ans comes as 2.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;There are two steps to solve this problem&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;1) First find the max element. then,&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;2) Find (P)&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #303030; font-family: Arial; line-height: 17px;&quot;&gt;the number of elements before the max element which are in increasing order. Total-1-P is the ans.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; line-height: 17px;&quot;&gt;This logic fetches 100 ont Techgig.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;&quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;If anyone wants the code then just give a comment.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/8033572839106639295/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2013/07/techgig-june-contest-disks-in-pile.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/8033572839106639295'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/8033572839106639295'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2013/07/techgig-june-contest-disks-in-pile.html' title='Techgig June contest : Disks in a Pile : Level (Easy)'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-4995877469312078158</id><published>2013-07-08T01:05:00.001-07:00</published><updated>2013-07-08T01:49:12.308-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="bitonic"/><category scheme="http://www.blogger.com/atom/ns#" term="july"/><category scheme="http://www.blogger.com/atom/ns#" term="Techgig"/><title type='text'>Techgig July contest : Bitonic Sequence : Level(Easy)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Problem Statement&lt;br /&gt;
&lt;div class=&quot;tp-txt&quot; id=&quot;description&quot; style=&quot;background-color: white; margin: 0px 0px 10px 15px; padding: 0px;&quot;&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px;&quot;&gt;
You are given an array A[] containing n positive integers. Lets define&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Bitonic subsequence&lt;/b&gt;. A subsequence of array A[] is called&amp;nbsp;&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Bitonic&lt;/b&gt;&amp;nbsp;if it is first strictly increasing and then strictly decreasing. Your task is to find the length of largest bitonic subsequence given an array.&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
&lt;br style=&quot;margin: 0px; padding: 0px;&quot; /&gt;
&lt;b style=&quot;margin: 0px; padding: 0px;&quot;&gt;Note-&lt;/b&gt;&amp;nbsp;A sequence sorted in increasing order is considered as Bitonic with decreasing part as empty. Similarly, A sequence sorted in decreasing order is also considered as bitonic with increasing part as empty&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px; margin: 0px; padding: 0px;&quot;&gt;
How many of you guys have got the solution for this problem ?&lt;br /&gt;
&lt;br /&gt;
Well, I have got the logic which work perfect but unfortunately It doesn`t fetch 100 on the Techgig compiler. The problem is not with my Code/Loigc but the problem is Techgig`s definition for Bitonic subsequence. Which can be found &lt;a href=&quot;https://www.google.co.in/url?sa=t&amp;amp;rct=j&amp;amp;q=&amp;amp;esrc=s&amp;amp;source=web&amp;amp;cd=3&amp;amp;cad=rja&amp;amp;ved=0CD0QFjAC&amp;amp;url=http%3A%2F%2Ffacultyfp.salisbury.edu%2Ftaanastasio%2FCOSC490%2FFall03%2FLectures%2FSorting%2Fbitonic.pdf&amp;amp;ei=Y3PaUcStBIv_rAfbhoC4Dg&amp;amp;usg=AFQjCNHqmKBsdnutkroUxvIm2AhnEWLR9w&amp;amp;sig2=bG2vdFx9cvkIedIH6xWsng&amp;amp;bvm=bv.48705608,d.bmk&quot; style=&quot;color: black;&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px;&quot;&gt;
Techgig say that the sequence should be strictly increasing and then decreasing where as it can be either way and that too in circular fashion.
&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #303030; font-family: Arial; font-size: 13px; line-height: 17px;&quot;&gt;
If you refer the previous link we can see that&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br class=&quot;Apple-interchange-newline&quot; /&gt;3 5 8 9 7 4 2 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;5 8 9 7 4 2 1 3&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;both are bitonic such that 2nd sequence can be converted to first by a circular shift.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;Lets, come to the basic question on Techgig and see how to implement a logic for it.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;I am considering a sequence for example given below.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;9,10,1,3,5,8,2,1,7,5,3,1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;Here starts the actual logic :-&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;As we want an subsequence which is first increasing and then decreasing let find the increasing numbers.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;We will be going number by number.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;step 1 : Consider 9, It is the first number so directly put it into a list(Array)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9 (List)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;step 2: next is 10, since 10 is greater and 9, put it into same list&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 9,10&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;step 3: 1, since 1 is less than 10 and we are searching for increasing number, we will create a new list&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9,10&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;At this point, If we had any numbers less then 1 in the previous list then those needed to be first &amp;nbsp; &amp;nbsp; &amp;nbsp;i inserted in the new list and then 1. But we didnt have any here&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;step 4: Once we have more than 1 list, every new number has to be checked with the last number of that list and if the new number is greater then it has to be appended to that list. The next number is 3,first check in the previous list if 3 is greater than the last number if it. In our case we have 10 as the last number so we need not append 3.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;Now, 3 is greater than 1, so just append it.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9,10&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1,3&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;Repeat the above steps, we get&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 9,10&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1,3,5,8&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1,2&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1,7&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1,5&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1,3&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;These are the various lists which we get. Now we can see here that 2nd list has the largest length. But that wont help us, because it may happen the there is not decreasing subsequence for it or may be some other list has a decreasing sequence of greater length.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;Hence, once these lists are formed we perform a similar process for decreasing lists.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;The important thing to keep in mind is the position(in input1[]) of last number in our lists.Because, we need to find the decreasing sequence only after that position.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;For example, lest take our first list 9,10 and try to find the various decreasing sequences for it.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;step 1 :1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;step 2: 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;step 3: 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5 &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;step 4: 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;8&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;step 5: 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3,2&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5,2&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;8,2&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;step 6: 1&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3,2,1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5,2,1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;8,2,1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;step 7: 1 (Important)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3,2,1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5,2,1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;8,2,1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;8,7&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;step 8: 1 (Important)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3,2,1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5,2,1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;8,2,1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;8,7,5,3,1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;Now, we can see that for increasing order 9,10; we have a decreasing order 8,7,5,3,1 of length 5, so 5+2=7 is the length of this Bitonic.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;Similarly, we can find the decreasing sequences for other lsit`s generated in part I and check the largest length.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial;  line-height: 17px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;Hope this was helpful. There may be other logic for the implementation but this was my own from scratch.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;Also, this can be modified for the circular sequence.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #303030; font-family: Arial; &quot;&gt;&lt;span style=&quot;line-height: 17px;&quot;&gt;If anyone want the code for this just put a comment below, and I would share the same.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: black; font-family: Arial; font-size: 13px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/4995877469312078158/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2013/07/techgig-july-contest-bitonic-sequence.html#comment-form' title='40 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/4995877469312078158'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/4995877469312078158'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2013/07/techgig-july-contest-bitonic-sequence.html' title='Techgig July contest : Bitonic Sequence : Level(Easy)'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>40</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-3014006333343418827</id><published>2010-06-30T11:01:00.000-07:00</published><updated>2013-06-28T03:28:06.507-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data structures"/><title type='text'>How to delete a node in the singly link list ?</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Given a pointer to a node in a singly linked list, how do you delete  the  node ? &lt;br /&gt;
A &lt;strong&gt;simple solution &lt;/strong&gt;is to traverse the linked list   until you find the node you want to delete.  But this solution requires   pointer to the head node which contradicts the problem statement.&lt;br /&gt;
&lt;strong&gt;Fast solution&lt;/strong&gt; is to copy the data from the next node   to the node to be deleted  and delete the next node.  Something like   following.&lt;br /&gt;
&lt;pre&gt;    struct node *temp  = node_ptr-&amp;gt;next;
   node_ptr-&amp;gt;data  = temp-&amp;gt;data;
   node_ptr-&amp;gt;next  = temp-&amp;gt;next;
   free(temp);&lt;/pre&gt;
In java, the code would look something like this :&lt;br /&gt;
&lt;div class=&quot;bar    &quot;&gt;
&lt;div class=&quot;toolbar&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;line alt1&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;1&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;java  keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;void&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;deleteNode(Node node){&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line alt2&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;2&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;spaces&quot;&gt; &lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;Node  temp = node.next;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line alt1&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;3&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;spaces&quot;&gt; &lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;node.data  = temp.data;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line  alt2&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;4&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;spaces&quot;&gt; &lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;node.next  = temp.next;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line  alt1&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;5&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;spaces&quot;&gt; &lt;/code&gt;&lt;code class=&quot;java comments&quot;&gt;//do  nothing with temp and let the garbage collector remove it&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;6&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;spaces&quot;&gt; &lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/3014006333343418827/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/06/how-to-delete-node-in-singly-link-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/3014006333343418827'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/3014006333343418827'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/06/how-to-delete-node-in-singly-link-list.html' title='How to delete a node in the singly link list ?'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-8735644138785731202</id><published>2010-06-30T10:59:00.000-07:00</published><updated>2010-06-30T11:00:15.346-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="algorithms"/><category scheme="http://www.blogger.com/atom/ns#" term="Data structures"/><title type='text'>Vertical sum of binary tree</title><content type='html'>Find vertical sum of given binary tree. &lt;p&gt;Example:&lt;/p&gt; &lt;div&gt; &lt;div&gt;Code:&lt;/div&gt; &lt;pre&gt;     1&lt;br /&gt;   / \&lt;br /&gt; 2     3&lt;br /&gt;/ \   / \&lt;br /&gt;4   5 6   7&lt;/pre&gt; &lt;/div&gt; &lt;p&gt;The tree has 5 vertical lines&lt;br /&gt;Vertical-1: nodes-4     =&gt; vertical sum is 4&lt;br /&gt;Vertical-2: nodes-2     =&gt; vertical sum is 2&lt;br /&gt;Vertical-3: nodes-1,5,6 =&gt; vertical sum is 1+5+6 = 12&lt;br /&gt;Vertical-4: nodes-3     =&gt; vertical sum is 3&lt;br /&gt;Vertical-5: nodes-7     =&gt; vertical sum is 7&lt;br /&gt;We need to output: 4 2 12 3 7&lt;/p&gt; &lt;p&gt;&lt;span id=&quot;more-227&quot;&gt;&lt;/span&gt;We can do an inorder traversal and hash  the column. We call  Traverse(root, 0) which means the root is at column  0. As we are doing  our traversal, we can hash the column and increase  its value by T.data. A  rough sketch of my function looks like this -&lt;/p&gt; &lt;p&gt;Traverse(Tree T, int column)&lt;br /&gt;{&lt;br /&gt;if(T==NULL) return;&lt;br /&gt;Traverse(T.left, column-1);&lt;br /&gt;Hash(column) += T.data;&lt;br /&gt;Traverse(T.right, column+1);&lt;br /&gt;}&lt;/p&gt; &lt;p&gt;Traverse(root, 0);&lt;br /&gt;Print Hash&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/8735644138785731202/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/06/vertical-sum-of-binary-tree.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/8735644138785731202'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/8735644138785731202'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/06/vertical-sum-of-binary-tree.html' title='Vertical sum of binary tree'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-7602135122269283363</id><published>2010-06-30T10:57:00.000-07:00</published><updated>2013-07-08T02:59:24.701-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Amazon"/><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft"/><title type='text'>Card shuffle</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Write a program to shuffle an pack of cards in the most efficient  way. This question can be asked in several flavors. &lt;br /&gt;
&lt;a href=&quot;http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle&quot; target=&quot;_blank&quot;&gt;Knuth Shuffle / Fisher-Yates Shuffle(Modified version by  Durstenfeld)&lt;/a&gt; algorithm is the answer to all the trick questions. So  how does this algorithm work ?&lt;span id=&quot;more-240&quot;&gt;&lt;/span&gt;&lt;br /&gt;
Fisher  and Yates’ original method was designed to be  implemented  using pencil and paper, with a precomputed table of random  numbers as  the source of randomness. Modern approach suggested by – &lt;a href=&quot;http://en.wikipedia.org/w/index.php?title=Richard_Durstenfeld&amp;amp;action=edit&amp;amp;redlink=1&quot; title=&quot;Richard Durstenfeld (page does not exist)&quot;&gt;Richard   Durstenfeld&lt;/a&gt; is as follows:&lt;br /&gt;
To shuffle an array &lt;em&gt;a&lt;/em&gt; of &lt;em&gt;n&lt;/em&gt; elements:&lt;br /&gt;
&lt;pre&gt;&lt;strong&gt;for&lt;/strong&gt; &lt;em&gt;i&lt;/em&gt; &lt;strong&gt;from&lt;/strong&gt; &lt;em&gt;n&lt;/em&gt; - 1 &lt;strong&gt;downto&lt;/strong&gt; 1 &lt;strong&gt;do&lt;/strong&gt;&lt;em&gt;&lt;strong&gt;
        &lt;/strong&gt;j&lt;/em&gt; ← random integer with 0 ≤ &lt;em&gt;j&lt;/em&gt; ≤ &lt;em&gt;i
       &lt;/em&gt; exchange &lt;em&gt;a&lt;/em&gt;[&lt;em&gt;j&lt;/em&gt;] and &lt;em&gt;a&lt;/em&gt;[&lt;em&gt;i&lt;/em&gt;]&lt;/pre&gt;
Reduced time complexity to &lt;em&gt;O&lt;/em&gt;(&lt;em&gt;n&lt;/em&gt;), compared to &lt;em&gt;O&lt;/em&gt;(&lt;em&gt;n&lt;/em&gt;&lt;sup&gt;2&lt;/sup&gt;)  for  the naive implementation. A sample java implementation :&lt;br /&gt;
&lt;div class=&quot;bar      show&quot;&gt;
&lt;div class=&quot;toolbar&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;line alt1&quot;&gt;
&lt;br /&gt;&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;01&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;java  keyword&quot;&gt;import&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;java.util.Random;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line alt2&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;02&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line alt1&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;03&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;java keyword&quot;&gt;static&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Random rng =  &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;new&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Random();&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line alt2&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;04&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;static&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;void&lt;/code&gt;  &lt;code class=&quot;java plain&quot;&gt;shuffle(&lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;int&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;[] array) {&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line alt1&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;05&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;spaces&quot;&gt;    &lt;/code&gt;&lt;code class=&quot;java  comments&quot;&gt;// i is the number of items remaining to be shuffled.&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line alt2&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;06&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;spaces&quot;&gt;    &lt;/code&gt;&lt;code class=&quot;java  keyword&quot;&gt;for&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;(&lt;/code&gt;&lt;code class=&quot;java  keyword&quot;&gt;int&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;i = array.length; i  &amp;gt; &lt;/code&gt;&lt;code class=&quot;java value&quot;&gt;1&lt;/code&gt;&lt;code class=&quot;java  plain&quot;&gt;; i--) {&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line  alt1&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;07&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;spaces&quot;&gt;        &lt;/code&gt;&lt;code class=&quot;java  comments&quot;&gt;// Pick a random element to swap with the i-th element.&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line alt2&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;08&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;spaces&quot;&gt;        &lt;/code&gt;&lt;code class=&quot;java  keyword&quot;&gt;int&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;j = rng.nextInt(i);  &lt;/code&gt;&lt;code class=&quot;java comments&quot;&gt;// 0 &amp;lt;= j &amp;lt;= i-1 (0-based array)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line alt1&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;09&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;spaces&quot;&gt;        &lt;/code&gt;&lt;code class=&quot;java  comments&quot;&gt;// Swap array elements.&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line alt2&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;10&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;spaces&quot;&gt;        &lt;/code&gt;&lt;code class=&quot;java  keyword&quot;&gt;int&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;tmp = array[j];&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line alt1&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;11&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;spaces&quot;&gt;        &lt;/code&gt;&lt;code class=&quot;java  plain&quot;&gt;array[j] = array[i-&lt;/code&gt;&lt;code class=&quot;java value&quot;&gt;1&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;];&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line alt2&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;12&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;spaces&quot;&gt;        &lt;/code&gt;&lt;code class=&quot;java  plain&quot;&gt;array[i-&lt;/code&gt;&lt;code class=&quot;java value&quot;&gt;1&lt;/code&gt;&lt;code class=&quot;java  plain&quot;&gt;] = tmp;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;line  alt1&quot;&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;13&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;spaces&quot;&gt;    &lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;number&quot;&gt;&lt;code&gt;14&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;content&quot;&gt;&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/7602135122269283363/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/06/card-shuffle.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/7602135122269283363'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/7602135122269283363'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/06/card-shuffle.html' title='Card shuffle'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-9216922285885920701</id><published>2010-05-08T12:21:00.000-07:00</published><updated>2010-05-08T12:22:49.524-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><title type='text'>How to connect database with C and CPP ?</title><content type='html'>&lt;p&gt;I got this tutorial from one site its very helpful hence shared here..... :)&lt;/p&gt;&lt;p&gt;This tutorial show coding how to connect with a database in C++….&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;This are actual guides&lt;br /&gt;&lt;br /&gt;Note:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Quote&lt;br /&gt;Before I start just to introduce some SQL commands and stuff. For starter you had some back grounds on it….&lt;br /&gt;&lt;br /&gt;SQL (Structured Query Language) is a fourth-generation language (4GL) that is used to define, manipulate, and control an RDBMS (relational database management system).&lt;br /&gt;&lt;br /&gt;DML sublanguage is concerned with commands that can perform operations on data in tables.It provides four commands for data manipulation:&lt;br /&gt;&lt;br /&gt;SELECT command retrieves data for queries.&lt;br /&gt;&lt;br /&gt;INSERT command adds new rows into a database table.&lt;br /&gt;&lt;br /&gt;UPDATE command changes the values of data items in a database table.&lt;br /&gt;&lt;br /&gt;DELETE command removes rows from a database table.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And let’s start….&lt;br /&gt;&lt;br /&gt;ODBC (Open Database Connectivity)&lt;br /&gt;&lt;br /&gt;• A standard interface for connecting from C++ to relational databases&lt;br /&gt;• It allows individual providers to implement and extend the standard with their own ODBC drivers&lt;br /&gt;&lt;br /&gt;Here are procedures or rather steps used in the industry in C++ coding for connecting to a database&lt;br /&gt;&lt;br /&gt;Steps of the ODBC&lt;br /&gt;&lt;br /&gt;• Include Header Files&lt;br /&gt;• Open a Connection to a Database&lt;br /&gt;• Choose an ODBC Driver&lt;br /&gt;• Query the Database&lt;br /&gt;• Creating an ODBC Statement Object&lt;br /&gt;• Executing a Query and Returning an ODBCResultSet Object&lt;br /&gt;• Extracting Data from an ODBCResultSet&lt;br /&gt;• Closing the ODBCResultSet and ODBCStatement&lt;br /&gt;• Importance of closing the connection&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Okey, this are actual steps how I connect to a database…definitely I used Oracle database, but I don’t want to further say what version it is. LOL&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;1. Include the Header Files&lt;br /&gt;# include statements at the beginning of your programs:&lt;br /&gt;#include &lt;sql.h&gt;&lt;br /&gt;#include&lt;sqltypes.h&gt;&lt;br /&gt;#include&lt;sqlext.h&gt;&lt;br /&gt;&lt;br /&gt;2. Open a Connection to a Database&lt;br /&gt;Set the environment handle:&lt;br /&gt;SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &amp;amp;hdlEnv);&lt;br /&gt;&lt;br /&gt;Set ODBC Driver version:&lt;br /&gt;SQLSetEnvAttr(hdlEnv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC30);&lt;br /&gt;&lt;br /&gt;Set the connection handle:&lt;br /&gt;SQLAllocHandle(SQL_HANDLE_DBC, hdlEnv, &amp;amp;hdlConn);&lt;br /&gt;&lt;br /&gt;Connect to the database:&lt;br /&gt;SQLConnect(hdlConn, (SQLCHAR*)dsnName,SQL_NTS,(SQLCHAR*)userID,SQL_NTS, (SQLCHAR*)passwd, SQL_NTS);&lt;br /&gt;&lt;br /&gt;3. Choose Driver&lt;br /&gt;•DSN – Data Source Name&lt;br /&gt;•Open the GUI ODBC Administrator (ODBCConfig)&lt;br /&gt;•Choose the appropriate ODBC driver&lt;br /&gt;•Provide a meaningful name to the DSN&lt;br /&gt;•Specify the server and the host string (host string is required if the server is running on a different machine)&lt;br /&gt;&lt;br /&gt;4. Query the Database&lt;br /&gt;Querying the database involves the following steps:&lt;br /&gt;&lt;br /&gt;–Creating a Statement&lt;br /&gt;SQLAllocHandle(SQL_HANDLE_STMT, hdlDbc, &amp;amp;hdlStmt);&lt;br /&gt;It allocates the memory for the statement handle. The database handle obtained during connection phase is passed as the second argument.&lt;br /&gt;&lt;br /&gt;– Executing a Query&lt;br /&gt;SQLExecDirect(hdlStmt, stmt, SQL_NTS);&lt;br /&gt;It executes the query, which is passed in as SQLCHAR* in the second argument.&lt;br /&gt;&lt;br /&gt;5. Extract the Data out of the Executed Query&lt;br /&gt;&lt;br /&gt;SQLGetData(hStmt,colNum,type,retVal,buffLength,&amp;amp;cbData);&lt;br /&gt;It extracts data from table as void* data and places it in retVal&lt;br /&gt;&lt;br /&gt;colNum refers to the column number provided in the SELECT statement in SQLExecDirect()&lt;br /&gt;&lt;br /&gt;Type is one of the standard ODBC data types&lt;br /&gt;example: DT_STRING à for a string data type&lt;br /&gt;DT_DOUBLE à for a double data type&lt;br /&gt;&lt;br /&gt;buffLength is the estimated size of the expected data&lt;br /&gt;&lt;br /&gt;cbData is the actual size of the data&lt;br /&gt;&lt;br /&gt;6. Traverse Through the Results&lt;br /&gt;SQLFetch(hStmt);&lt;br /&gt;Fetches the next record&lt;br /&gt;&lt;br /&gt;hStmt is the statement handle obtained using SQLAllocHandle&lt;br /&gt;If a record is available, It returns SQL_SUCCEEDED&lt;br /&gt;&lt;br /&gt;7. Close the Statement and the Connection&lt;br /&gt;SQLFreeHandle(SQL_HANDLE_STMT, hdlStmt);&lt;br /&gt;It closes and de-allocates the memory reserved for the statement handle&lt;br /&gt;&lt;br /&gt;SQLFreeHandle(SQL_HANDLE_DBC, hdlConn);&lt;br /&gt;It disconnects and de-allocates the memory reserved for the connection handle&lt;br /&gt;&lt;br /&gt;SQLFreeHandle(SQL_HANDLE_ENV, hdlEnv);&lt;br /&gt;It de-allocates the memory occupied by the environment handle&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;For further escalate the whole steps here a sample program&lt;br /&gt;&lt;br /&gt;//Header files:&lt;br /&gt;&lt;br /&gt;#include &lt;sql.h&gt;&lt;br /&gt;#include&lt;sqltypes.h&gt;&lt;br /&gt;#include&lt;sqlext.h&gt;&lt;br /&gt;...&lt;br /&gt;....&lt;br /&gt;&lt;br /&gt;//Declaration:&lt;br /&gt;&lt;br /&gt;SQLHandle hdlEnv, hdlConn, hdlStmt, hdlDbc&lt;br /&gt;char* stmt = &quot;SELECT * from NutHead&quot;; //SQL statement NutHead is the Table name&lt;br /&gt;&lt;br /&gt;//for example&lt;br /&gt;char *dsnName = “COLLECTOR”  name of your program or what ever…..&lt;br /&gt;char* userID = &quot;eXceed&quot;;&lt;br /&gt;char* passwd = &quot;hole&quot;;&lt;br /&gt;char* retVal[256];&lt;br /&gt;unsigned int cbData;&lt;br /&gt;&lt;br /&gt;SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &amp;amp;hdlEnv);&lt;br /&gt;SQLSetEnvAttr(hdlEnv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC30);&lt;br /&gt;SQLAllocHandle(SQL_HANDLE_DBC, hdlEnv, &amp;amp;hdlConn);&lt;br /&gt;SQLConnect(hdlConn, (SQLCHAR*)dsnName,SQL_NTS,(SQLCHAR*)userID,SQL_NTS, (SQLCHAR*)passwd, SQL_NTS);&lt;br /&gt;SQLAllocHandle(SQL_HANDLE_STMT, hdlDbc, &amp;amp;hdlStmt);&lt;br /&gt;SQLExecDirect(hdlStmt, (SQLCHAR*)stmt, SQL_NTS);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;//Initialize the database connection&lt;br /&gt;&lt;br /&gt;while(SQLFetch(hdlStmt) == SQL_SUCCEEDED)&lt;br /&gt;{&lt;br /&gt; SQLGetData(hdlStmt,0,DT_STRING,retVal,256,&amp;amp;cbData);&lt;br /&gt; std::cout &lt;&lt; retVal &lt;&lt; std::endl;&lt;br /&gt;}&lt;br /&gt;SQLFreeHandle(SQL_HANDLE_STMT, hdlStmt);&lt;br /&gt;SQLFreeHandle(SQL_HANDLE_DBC, hdlConn);&lt;br /&gt;SQLFreeHandle(SQL_HANDLE_ENV, hdlEnv);  //End the connecti</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/9216922285885920701/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/05/how-to-connect-database-with-c-and-cpp.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/9216922285885920701'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/9216922285885920701'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/05/how-to-connect-database-with-c-and-cpp.html' title='How to connect database with C and CPP ?'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-5728679600091411826</id><published>2010-04-08T10:37:00.000-07:00</published><updated>2010-04-08T10:39:06.407-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data structures"/><title type='text'>How to Rebuild a binary tree from Inorder and Preorder traversals ?</title><content type='html'>&lt;p&gt;This is a well known problem where given any two traversals of a tree   such as inorder &amp;amp; preorder or inorder &amp;amp; postorder or inorder  &amp;amp; levelorder traversals we need to rebuild the tree.&lt;/p&gt; &lt;p&gt;The following procedure demonstrates on how to rebuild tree from  given inorder and preorder traversals of a binary tree:&lt;/p&gt; &lt;ul&gt;&lt;li&gt;Preorder      traversal visits Node, left subtree, right subtree  recursively&lt;/li&gt;&lt;li&gt;Inorder      traversal visits left subtree, node, right subtree  recursively&lt;/li&gt;&lt;li&gt;Since      we know that the first node in Preorder is its root, we  can easily locate      the root node in the inorder traversal and hence  we can obtain left      subtree and right subtree from the inorder  traversal recursively&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style=&quot;text-decoration: underline;&quot;&gt;Consider the following  example:&lt;/span&gt;&lt;/p&gt; &lt;p&gt;Preorder Traversal:    1    2    4    8    9    10    11    5    3     6    7&lt;/p&gt; &lt;p&gt;Inorder Traversal:       8    4    10    9    11    2    5    1     6    3    7&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Iteration 1:&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Root – {1}&lt;/p&gt; &lt;p&gt;Left Subtree – {8,4,10,9,11,2,5}&lt;/p&gt; &lt;p&gt;Right Subtree – {6,3,7}&lt;/p&gt; &lt;p&gt;&lt;a href=&quot;http://crackinterviewtoday.files.wordpress.com/2010/03/inorderpreorder1.jpeg&quot;&gt;&lt;img class=&quot;aligncenter size-full wp-image-104&quot; title=&quot;InorderPreorder1&quot; src=&quot;http://crackinterviewtoday.files.wordpress.com/2010/03/inorderpreorder1.jpeg?w=450&amp;amp;h=132&quot; alt=&quot;&quot; height=&quot;132&quot; width=&quot;450&quot; /&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Iteration 2:&lt;/strong&gt;&lt;/p&gt; &lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign=&quot;top&quot; width=&quot;319&quot;&gt;Root – {2} &lt;p&gt;Left Subtree – {8,4,10,9,11}&lt;/p&gt; &lt;p&gt;Right Subtree – {5}&lt;/p&gt;&lt;/td&gt; &lt;td valign=&quot;top&quot; width=&quot;319&quot;&gt;Root – {3} &lt;p&gt;Left Subtree – {6}&lt;/p&gt; &lt;p&gt;Right Subtree – {7}&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;p style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://crackinterviewtoday.files.wordpress.com/2010/03/inorderpreorder2.jpeg&quot;&gt;&lt;img class=&quot;size-full wp-image-105 aligncenter&quot; title=&quot;InorderPreorder2&quot; src=&quot;http://crackinterviewtoday.files.wordpress.com/2010/03/inorderpreorder2.jpeg?w=446&amp;amp;h=167&quot; alt=&quot;&quot; height=&quot;167&quot; width=&quot;446&quot; /&gt;&lt;/a&gt;&lt;/p&gt; &lt;p style=&quot;text-align: center;&quot;&gt; &lt;/p&gt;&lt;p&gt;&lt;strong&gt;Iteration 3:&lt;/strong&gt;&lt;/p&gt; &lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td colspan=&quot;2&quot; valign=&quot;top&quot; width=&quot;319&quot;&gt;Root – {2} &lt;p&gt;Left Subtree – {8,4,10,9,11}&lt;/p&gt; &lt;p&gt;Right Subtree – {5}&lt;/p&gt;&lt;/td&gt; &lt;td valign=&quot;top&quot; width=&quot;319&quot;&gt;Root – {3} &lt;p&gt;Left Subtree – {6}&lt;/p&gt; &lt;p&gt;Right Subtree – {7}&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td valign=&quot;top&quot; width=&quot;160&quot;&gt;Root – {4} &lt;p&gt;Left Subtree – {8}&lt;/p&gt; &lt;p&gt;Right Subtree – {10,9,11}&lt;/p&gt;&lt;/td&gt; &lt;td valign=&quot;top&quot; width=&quot;160&quot;&gt;Done&lt;/td&gt; &lt;td valign=&quot;top&quot; width=&quot;319&quot;&gt;Done&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;p style=&quot;text-align: center;&quot;&gt; &lt;/p&gt;&lt;p style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://crackinterviewtoday.files.wordpress.com/2010/03/inorderpreorder4.jpeg&quot;&gt;&lt;img class=&quot;aligncenter size-full wp-image-106&quot; title=&quot;InorderPreorder4&quot; src=&quot;http://crackinterviewtoday.files.wordpress.com/2010/03/inorderpreorder4.jpeg?w=450&amp;amp;h=214&quot; alt=&quot;&quot; height=&quot;214&quot; width=&quot;450&quot; /&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Iteration 4:&lt;/strong&gt;&lt;/p&gt; &lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td colspan=&quot;3&quot; valign=&quot;top&quot; width=&quot;319&quot;&gt;Root – {2} &lt;p&gt;Left Subtree – {8,4,10,9,11}&lt;/p&gt; &lt;p&gt;Right Subtree – {5}&lt;/p&gt;&lt;/td&gt; &lt;td valign=&quot;top&quot; width=&quot;319&quot;&gt;Root – {3} &lt;p&gt;Left Subtree – {6}&lt;/p&gt; &lt;p&gt;Right Subtree – {7}&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan=&quot;2&quot; valign=&quot;top&quot; width=&quot;160&quot;&gt;Root – {4} &lt;p&gt;Left   Subtree – {8}&lt;/p&gt; &lt;p&gt;Right   Subtree – {10,9,11}&lt;/p&gt;&lt;/td&gt; &lt;td valign=&quot;top&quot; width=&quot;160&quot;&gt;Done&lt;/td&gt; &lt;td valign=&quot;top&quot; width=&quot;319&quot;&gt;Done&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td valign=&quot;top&quot; width=&quot;80&quot;&gt;Done&lt;/td&gt; &lt;td valign=&quot;top&quot; width=&quot;80&quot;&gt;R – {9} &lt;p&gt;Left ST – {10}&lt;/p&gt; &lt;p&gt;Right ST-{11}&lt;/p&gt;&lt;/td&gt; &lt;td valign=&quot;top&quot; width=&quot;160&quot;&gt;Done&lt;/td&gt; &lt;td valign=&quot;top&quot; width=&quot;319&quot;&gt;Done&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;p style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://crackinterviewtoday.files.wordpress.com/2010/03/diagram21.jpeg&quot;&gt;&lt;img class=&quot;aligncenter size-full wp-image-107&quot; title=&quot;Diagram2&quot; src=&quot;http://crackinterviewtoday.files.wordpress.com/2010/03/diagram21.jpeg?w=450&amp;amp;h=379&quot; alt=&quot;&quot; height=&quot;379&quot; width=&quot;450&quot; /&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;The following are the two versions of programming solutions even  though both are based on above mentioned algorithm:&lt;/p&gt; &lt;ul&gt;&lt;li&gt;Creating left preorder,      left inorder, right preorder, right  inorder lists at every iteration to construct      tree.&lt;/li&gt;&lt;li&gt;Passing index of preorder      and inorder traversals and using the  same input list to construct tree.&lt;/li&gt;&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/5728679600091411826/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/04/how-to-rebuild-binary-tree-from-inorder.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/5728679600091411826'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/5728679600091411826'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/04/how-to-rebuild-binary-tree-from-inorder.html' title='How to Rebuild a binary tree from Inorder and Preorder traversals ?'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-6001002887253020782</id><published>2010-04-08T10:36:00.000-07:00</published><updated>2010-04-08T10:37:09.015-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data structures"/><category scheme="http://www.blogger.com/atom/ns#" term="Yahoo"/><title type='text'>O(1) stack operations.</title><content type='html'>&lt;p&gt;Design a datastructure to implement a stack such that ALL of push(),  pop() and getMax() functions work in O(1) time. You have infinite memory  at your disposal.   &lt;br /&gt;Also note that function getMax() just returns  the element with maximum value in the stack and NOT delete it from the  stack like what pop() does. &lt;/p&gt; &lt;p&gt; &lt;span id=&quot;more-139&quot;&gt;&lt;/span&gt; &lt;/p&gt;&lt;p&gt; &lt;/p&gt;  &lt;pre style=&quot;background-color: rgb(255, 255, 255); white-space: pre-wrap; overflow: auto;&quot;&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;Insight : compress the information using diff&lt;br /&gt;&lt;br /&gt;PUSH :&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt; stack empty&lt;br /&gt;push(elem)&lt;br /&gt;push(elem)&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;&lt;br /&gt;min &lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt; pop()&lt;br /&gt;push(elem&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;min)&lt;br /&gt;push(min)&lt;br /&gt;&lt;br /&gt;POP :&lt;br /&gt;min &lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt; pop()&lt;br /&gt;elem &lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt; pop()&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt; elem is &lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;ve&lt;br /&gt;   push(min&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;elem) &lt;/span&gt;&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt; earlier min  &lt;/span&gt;&lt;span style=&quot;color: rgb(0, 128, 0);&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt; min&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;&lt;br /&gt;   push(min)&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt; elem &lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt; min &lt;br /&gt;&lt;br /&gt;MIN :&lt;br /&gt;min &lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt; pop()&lt;br /&gt;push(min)&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt; min &lt;br /&gt;&lt;br /&gt;O(n&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;) space and  constant operations &lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 255);&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt; pop push min &lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt;!!&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);&quot;&gt; &lt;/span&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/6001002887253020782/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/04/o1-stack-operations.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/6001002887253020782'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/6001002887253020782'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/04/o1-stack-operations.html' title='O(1) stack operations.'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-9079808475288643369</id><published>2010-03-28T11:08:00.000-07:00</published><updated>2010-03-28T11:09:14.381-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Microsoft"/><title type='text'>A memory Q here..</title><content type='html'>In file1.c&lt;br /&gt;we have static int i = 5;&lt;br /&gt;and int *ptr = &amp;i;&lt;br /&gt;&lt;br /&gt;now value of ptr is written in some file say abc.txt&lt;br /&gt;&lt;br /&gt;now file2.c (another program) opens abc.txt&lt;br /&gt;read the value&lt;br /&gt;&lt;br /&gt;and print the value loacted at dat adress say&lt;br /&gt;what will be the o/p????&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;example i = 5;&lt;br /&gt;ptr = &amp;i; ptr has now 0xABFF&lt;br /&gt;&lt;br /&gt;0xABFF is written in abc.txt&lt;br /&gt;&lt;br /&gt;file2.c opens the abc.txt and read the value 0xABFF&lt;br /&gt;&lt;br /&gt;now print the value present at this address&lt;br /&gt;waht it will print????&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;ANSWER:&lt;br /&gt;&lt;br /&gt;A program has nothing to do with RAM but an address space available to it. That is, total amount of memory it can index (on 32 bit systems 2^32). Program is written at the lower level to hold addresses (any of these 2^32 minus some reserved for kernel) within this range irrespective of the amount of RAM you have. Program assume it has and can have whole addresses only for itself. OS and CPU help to map this wide range of addresses to the amount of available RAM in your computer. Thus, the address your pointer holds is an address in this address space (called virtual address).&lt;br /&gt;&lt;br /&gt;Even if the same program runs twice your pointer may not be the same (with same value) next time due to various environmental (and your program logic) reasons.&lt;br /&gt;&lt;br /&gt;There are programs that store addresses of objects in virtual memory and the links between these objects to a file. But prior to writing they change these addresses to file offsets (thinking now this file to be memory.) Converting memory pointers (addresses) to file offsets and fro is termed &#39;pointer swizzling.&#39; These programs are in general called &#39;object stores&#39;, and object-oriented databases are on genre of object stores.&lt;br /&gt;&lt;br /&gt;The variable being static makes no difference, the code produced by your program may be relinkable and reloadable (relocatable code.)</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/9079808475288643369/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/memory-q-here.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/9079808475288643369'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/9079808475288643369'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/memory-q-here.html' title='A memory Q here..'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-6952259759478915746</id><published>2010-03-28T09:41:00.001-07:00</published><updated>2010-03-28T09:41:23.377-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="linked list"/><title type='text'>Write a C program to insert nodes into a linked list in a sorted fashion</title><content type='html'>&lt;span class=&quot;normal&quot;&gt;The solution is to iterate down the list looking  for the correct place to insert the new node. That could be the end of  the list, or a point just before a node which is larger than the new  node.&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;announcelink&quot;&gt;Note&lt;/span&gt; that we assume the memory for the  new node has already been allocated and a pointer to that memory is  being passed to this function.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// Special case code for the head end&lt;br /&gt;void linkedListInsertSorted(struct node** headReference, struct node* newNode)&lt;br /&gt;{&lt;br /&gt;  // Special case for the head end&lt;br /&gt;  if (*headReference == NULL || (*headReference)-&gt;data &gt;= newNode-&gt;data)&lt;br /&gt;  {&lt;br /&gt;     newNode-&gt;next = *headReference;&lt;br /&gt;     *headReference = newNode;&lt;br /&gt;  }&lt;br /&gt;  else&lt;br /&gt;  {&lt;br /&gt;     // Locate the node before which the insertion is to happen!&lt;br /&gt;     struct node* current = *headReference;&lt;br /&gt;     while (current-&gt;next!=NULL &amp;amp;&amp;amp; current-&gt;next-&gt;data &lt;&gt;data)&lt;br /&gt;     {&lt;br /&gt;        current = current-&gt;next;&lt;br /&gt;     }&lt;br /&gt;     newNode-&gt;next = current-&gt;next;&lt;br /&gt;     current-&gt;next = newNode;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;  &lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/6952259759478915746/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/write-c-program-to-insert-nodes-into.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/6952259759478915746'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/6952259759478915746'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/write-c-program-to-insert-nodes-into.html' title='Write a C program to insert nodes into a linked list in a sorted fashion'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-8015548482299159884</id><published>2010-03-28T09:39:00.000-07:00</published><updated>2010-03-28T09:40:11.551-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data structures"/><title type='text'>What is the difference between the stack and the heap? Where are the different types of variables of a program stored in memory?</title><content type='html'>&lt;span class=&quot;normal&quot;&gt;When a program is loaded into memory, it is  organized into three areas of memory, called segments: &lt;span class=&quot;announcelink&quot;&gt;the text segment&lt;/span&gt;, &lt;span class=&quot;announcelink&quot;&gt;stack  segment&lt;/span&gt;, and the &lt;span class=&quot;announcelink&quot;&gt;heap segment&lt;/span&gt;.  The text segment (sometimes also called the code segment) is where the  compiled code of the program itself resides. This is the machine  language representation of the program steps to be carried out,  including all functions making up the program, both user defined and  system.&lt;br /&gt;&lt;br /&gt;The remaining two areas of system memory is where storage may be  allocated by the compiler for data storage. The stack is where memory is  allocated for automatic variables within functions. A stack is a Last  In First Out (LIFO) storage device where new storage is allocated and  deallocated at only one &quot;end&quot;, called the &lt;span class=&quot;announcelink&quot;&gt;Top&lt;/span&gt;  of the stack. When a program begins executing in the function main(),  space is allocated on the stack for all variables declared within  main(). If main() calls a function, func(), additional storage is  allocated for the variables in func() at the top of the stack. Notice  that the parameters passed by main() to func() are also stored on the  stack. If func() were to call any additional functions, storage would be  allocated at the new Top of stack. When func() returns, storage for its  local variables is deallocated, and the Top of the stack returns to its  old position. If main() were to call another function, storage would be  allocated for that function at the Top. The memory allocated in the  stack area is used and reused during program execution. It should be  clear that memory allocated in this area will contain garbage values  left over from previous usage.&lt;br /&gt;&lt;br /&gt;The &lt;span class=&quot;announcelink&quot;&gt;heap&lt;/span&gt; segment provides more stable  storage of data for a program; memory allocated in the heap remains in  existence for the duration of a program. Therefore, global variables  (storage class external), and static variables are allocated on the  heap. The memory allocated in the heap area, if initialized to zero at  program start, remains zero until the program makes use of it. Thus, the  heap area need not contain garbage. &lt;br /&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/8015548482299159884/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/what-is-difference-between-stack-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/8015548482299159884'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/8015548482299159884'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/what-is-difference-between-stack-and.html' title='What is the difference between the stack and the heap? Where are the different types of variables of a program stored in memory?'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-2349431301229590136</id><published>2010-03-28T09:38:00.001-07:00</published><updated>2010-03-28T09:38:51.278-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="compiler"/><title type='text'>How the compiler make difference between C and C++?</title><content type='html'>Most compilers recognized the file type by looking at the file extension.&lt;br /&gt;&lt;br /&gt;You might also be able to force the compiler to ignore the file type by supplying compiler switch. In MS VC++ 6, for example, the MSCRT defines a macro, __cplusplus. If you undefine that macro, then the compiler will treat your code as C code. You don&#39;t define the __cplusplus macro. It is defined by the compiler when compiling a C++ source. In MSVC6 there&#39;s a switch for the compiler, /Tc, that forces a C compilation instead of C++.</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/2349431301229590136/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/how-compiler-make-difference-between-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/2349431301229590136'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/2349431301229590136'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/how-compiler-make-difference-between-c.html' title='How the compiler make difference between C and C++?'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-2823569779350159488</id><published>2010-03-28T09:37:00.000-07:00</published><updated>2010-03-28T09:38:04.177-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="compiler"/><title type='text'>What are the general steps in compilation?</title><content type='html'>1. Lexical analysis.&lt;br /&gt;2. Syntactic analysis.&lt;br /&gt;3. Sematic analysis. &lt;br /&gt;4. Pre-optimization of internal representation.&lt;br /&gt;5. Code generation.&lt;br /&gt;6. Post optimization.</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/2823569779350159488/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/what-are-general-steps-in-compilation.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/2823569779350159488'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/2823569779350159488'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/what-are-general-steps-in-compilation.html' title='What are the general steps in compilation?'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-6589021612434102105</id><published>2010-03-28T09:36:00.002-07:00</published><updated>2010-03-28T09:37:21.962-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="compiler"/><title type='text'>What are the different types of linkages?</title><content type='html'>Linkage is used to determine what makes the same name declared in different scopes refer to the same thing. An object only ever has one name, but in many cases we would like to be able to refer to the same object from different scopes. A typical example is the wish to be able to call printf() from several different places in a program, even if those places are not all in the same source file.&lt;br /&gt;&lt;br /&gt;The Standard warns that declarations which refer to the same thing must all have compatible type, or the behaviour of the program will be undefined. Except for the use of the storage class specifier, the declarations must be identical.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The three different types of linkage are:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    * external linkage&lt;br /&gt;    * internal linkage&lt;br /&gt;    * no linkage&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In an entire program, built up perhaps from a number of source files and libraries, if a name has external linkage, then every instance of a that name refers to the same object throughout the program. For something which has internal linkage, it is only within a given source code file that instances of the same name will refer to the same thing. Finally, names with no linkage refer to separate things.&lt;br /&gt;&lt;br /&gt;Linkage and definitions&lt;br /&gt;&lt;br /&gt;Every data object or function that is actually used in a program (except as the operand of a sizeof operator) must have one and only one corresponding definition. This &quot;exactly one&quot; rule means that for objects with external linkage there must be exactly one definition in the whole program; for things with internal linkage (confined to one source code file) there must be exactly one definition in the file where it is declared; for things with no linkage, whose declaration is always a definition, there is exactly one definition as well.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The three types of accessibility that you will want of data objects or functions are:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    * Throughout the entire program,&lt;br /&gt;    * Restricted to one source file,&lt;br /&gt;    * Restricted to one function (or perhaps a single compound statement).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;For the three cases above, you will want external linkage, internal linkage, and no linkage respectively. The external linkage declarations would be prefixed with extern, the internal linkage declarations with static.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;// External linkage.&lt;br /&gt;extern int var1;&lt;br /&gt;&lt;br /&gt;// Definitions with external linkage.&lt;br /&gt;extern int var2 = 0;&lt;br /&gt;&lt;br /&gt;// Internal linkage:&lt;br /&gt;static int var3;&lt;br /&gt;&lt;br /&gt;// Function with external linkage&lt;br /&gt;void f1(int a){}&lt;br /&gt;&lt;br /&gt;// Function can only be invoked by name from within this file.&lt;br /&gt;static int f2(int a1, int a2)&lt;br /&gt;{&lt;br /&gt;    return(a1 * a2);&lt;br /&gt;}</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/6589021612434102105/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/what-are-different-types-of-linkages.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/6589021612434102105'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/6589021612434102105'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/what-are-different-types-of-linkages.html' title='What are the different types of linkages?'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-6234043948911973559</id><published>2010-03-28T09:36:00.001-07:00</published><updated>2010-03-28T09:36:38.304-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><title type='text'>Write a C program to swap two variables without using a temporary variable</title><content type='html'>This questions is asked almost always in every interview.&lt;br /&gt;&lt;br /&gt;The best way to swap two variables is to use a temporary variable.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int a,b,t;&lt;br /&gt;&lt;br /&gt;t = a;&lt;br /&gt;a = b;&lt;br /&gt;b = t;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There is no way better than this as you will find out soon. There are a few slick expressions that do swap variables without using temporary storage. But they come with their own set of problems.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method1 (The XOR trick)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a ^= b ^= a ^= b;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Although the code above works fine for most of the cases, it tries to modify variable &#39;a&#39; two times between sequence points, so the behavior is undefined. What this means is it wont work in all the cases. This will also not work for floating-point values. Also, think of a scenario where you have written your code like this&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;swap(int *a, int *b)&lt;br /&gt;{&lt;br /&gt;  *a ^= *b ^= *a ^= *b;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, if suppose, by mistake, your code passes the pointer to the same variable to this function. Guess what happens? Since Xor&#39;ing an element with itself sets the variable to zero, this routine will end up setting the variable to zero (ideally it should have swapped the variable with itself). This scenario is quite possible in sorting algorithms which sometimes try to swap a variable with itself (maybe due to some small, but not so fatal coding error). One solution to this problem is to check if the numbers to be swapped are already equal to each other.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;swap(int *a, int *b)&lt;br /&gt;{&lt;br /&gt;  if(*a!=*b)&lt;br /&gt;  {&lt;br /&gt;    *a ^= *b ^= *a ^= *b;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method2&lt;br /&gt;&lt;br /&gt;This method is also quite popular&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; a=a+b;&lt;br /&gt; b=a-b;&lt;br /&gt; a=a-b;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;But, note that here also, if a and b are big and their addition is bigger than the size of an int, even this might end up giving you wrong results.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Method3&lt;br /&gt;&lt;br /&gt;One can also swap two variables using a macro. However, it would be required to pass the type of the variable to the macro. Also, there is an interesting problem using macros. Suppose you have a swap macro which looks something like this&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#define swap(type,a,b) type temp;temp=a;a=b;b=temp;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, think what happens if you pass in something like this&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;swap(int,temp,a) //You have a variable called &quot;temp&quot; (which is quite possible).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is how it gets replaced by the macro&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int temp;&lt;br /&gt;temp=temp;&lt;br /&gt;temp=b;&lt;br /&gt;b=temp;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Which means it sets the value of &quot;b&quot; to both the variables!. It never swapped them! Scary, isn&#39;t it?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So the moral of the story is, dont try to be smart when writing code to swap variables. Use a temporary variable. Its not only fool proof, but also easier to understand and maintain.</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/6234043948911973559/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/write-c-program-to-swap-two-variables.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/6234043948911973559'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/6234043948911973559'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/write-c-program-to-swap-two-variables.html' title='Write a C program to swap two variables without using a temporary variable'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-1875753852089492646</id><published>2010-03-28T09:34:00.000-07:00</published><updated>2010-03-28T09:35:45.974-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="algorithms"/><title type='text'>How can I sort things that are too large to bring into memory?</title><content type='html'>A sorting program that sorts items that are on secondary storage (disk or tape) rather than primary storage (memory) is called an external sort. Exactly how to sort large data depends on what is meant by ?too large to fit in memory.? If the items to be sorted are themselves too large to fit in memory (such as images), but there aren?t many items, you can keep in memory only the sort key and a value indicating the data?s location on disk. After the key/value pairs are sorted, the data is rearranged on disk into the correct order. If ?too large to fit in memory? means that there are too many items to fit into memory at one time, the data can be sorted in groups that will fit into memory, and then the resulting files can be merged. A sort such as a radix sort can also be used as an external sort, by making each bucket in the sort a file. Even the quick sort can be an external sort. The data can be partitioned by writing it to two smaller files. When the partitions are small enough to fit, they are sorted in memory and concatenated to form the sorted file.</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/1875753852089492646/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/how-can-i-sort-things-that-are-too.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/1875753852089492646'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/1875753852089492646'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/how-can-i-sort-things-that-are-too.html' title='How can I sort things that are too large to bring into memory?'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-3617820322374392036</id><published>2010-03-28T09:32:00.001-07:00</published><updated>2010-03-28T09:32:56.961-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="algorithms"/><title type='text'>What is the difference between Merge Sort and Quick sort?</title><content type='html'>&lt;span class=&quot;normal&quot;&gt;Both Merge-sort and Quick-sort have same time  complexity i.e. O(nlogn). In merge sort the file a[1:n] was divided  at  its midpoint into sub-arrays which are independently sorted and later  merged. Whereas, in quick sort the division into two sub-arrays is made  so that the sorted sub-arrays do not need to be merged latter. &lt;br /&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/3617820322374392036/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/what-is-difference-between-merge-sort.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/3617820322374392036'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/3617820322374392036'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/what-is-difference-between-merge-sort.html' title='What is the difference between Merge Sort and Quick sort?'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8700135416943985114.post-1097222072514832417</id><published>2010-03-28T09:31:00.000-07:00</published><updated>2010-03-28T09:32:27.754-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="algorithms"/><title type='text'>What is heap sort?</title><content type='html'>&lt;span class=&quot;normal&quot;&gt;A &lt;span class=&quot;announcelink&quot;&gt;Heap&lt;/span&gt; is an  almost complete binary tree.In this tree, if the maximum level is i,  then, upto the (i-1)th level should be complete. At level i, the number  of nodes can be less than or equal to 2^i. If the number of nodes is  less than 2^i, then the nodes in that level should be completely filled,  only from left to right.&lt;br /&gt;&lt;br /&gt;The property of an ascending heap is that, the root is the lowest and  given any other node i, that node should be less than its left child and  its right child. In a descending heap, the root should be the highest  and given any other node i, that node should be greater than its left  child and right child.&lt;br /&gt;&lt;br /&gt;To sort the elements, one should create the heap first. Once the heap is  created, the root has the highest value. Now we need to sort the  elements in ascending order. The root can not be exchanged with the nth  element so that the item in the nth position is sorted. Now, sort the  remaining (n-1) elements. This can be achieved by &lt;span class=&quot;announcelink&quot;&gt;reconstructing&lt;/span&gt; the heap for (n-1) elements.&lt;br /&gt;&lt;br /&gt;A highly &lt;span class=&quot;announcelink&quot;&gt;simplified&lt;/span&gt; pseudocode is  given below&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;heapsort()&lt;br /&gt;{&lt;br /&gt;   n = array();   // Convert the tree into an array.&lt;br /&gt;   makeheap(n);   // Construct the initial heap.&lt;br /&gt;&lt;br /&gt;   for(i=n; i&gt;=2; i--)&lt;br /&gt;   {&lt;br /&gt;      swap(s[1],s[i]);&lt;br /&gt;      heapsize--;&lt;br /&gt;      keepheap(i);&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;makeheap(n)&lt;br /&gt;{&lt;br /&gt;   heapsize=n;&lt;br /&gt;   for(i=n/2; i&gt;=1; i--)&lt;br /&gt;     keepheap(i);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;keepheap(i)&lt;br /&gt;{&lt;br /&gt;   l = 2*i;&lt;br /&gt;   r = 2*i + 1;&lt;br /&gt; &lt;br /&gt;   p = s[l];&lt;br /&gt;   q = s[r];&lt;br /&gt;   t = s[i];&lt;br /&gt;&lt;br /&gt;   if(l&lt;=heapsize &amp;amp;&amp;amp; p-&gt;value &gt; t-&gt;value)&lt;br /&gt;     largest = l;&lt;br /&gt;   else&lt;br /&gt;     largest = i;&lt;br /&gt;&lt;br /&gt;   m = s[largest];&lt;br /&gt;&lt;br /&gt;   if(r&lt;=heapsize &amp;amp;&amp;amp; q-&gt;value &gt; m-&gt;value)&lt;br /&gt;     largest = r;&lt;br /&gt;&lt;br /&gt;   if(largest != i)&lt;br /&gt;   {&lt;br /&gt;      swap(s[i], s[largest]);&lt;br /&gt;      keepheap(largest);&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt; &lt;br /&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://hacktheinterview.blogspot.com/feeds/1097222072514832417/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/what-is-heap-sort.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/1097222072514832417'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8700135416943985114/posts/default/1097222072514832417'/><link rel='alternate' type='text/html' href='http://hacktheinterview.blogspot.com/2010/03/what-is-heap-sort.html' title='What is heap sort?'/><author><name>Ajit</name><uri>http://www.blogger.com/profile/10593356169369014473</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>