<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: The Amazon.com Interview</title>
	<atom:link href="http://elliottback.com/wp/the-amazoncom-interview/feed/" rel="self" type="application/rss+xml" />
	<link>http://elliottback.com/wp/the-amazoncom-interview/</link>
	<description>Internet &#38; Technology</description>
	<lastBuildDate>Thu, 10 May 2012 16:09:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: William</title>
		<link>http://elliottback.com/wp/the-amazoncom-interview/#comment-2197506</link>
		<dc:creator>William</dc:creator>
		<pubDate>Fri, 04 Feb 2011 02:28:10 +0000</pubDate>
		<guid isPermaLink="false">/?p=867#comment-2197506</guid>
		<description>Instead of using hash table, you can use a bit map which saves a lot of memory. You can use bit map instead of hash table because the number of occurrence is only important if it is zero or at least one. 
One thing to mention is that we might need to check if there is two number value of $target/2 existing in the array. However, it can be done during the bit map construction.
This takes O(n) to finished but takes much less memory</description>
		<content:encoded><![CDATA[<p>Instead of using hash table, you can use a bit map which saves a lot of memory. You can use bit map instead of hash table because the number of occurrence is only important if it is zero or at least one.<br />
One thing to mention is that we might need to check if there is two number value of $target/2 existing in the array. However, it can be done during the bit map construction.<br />
This takes O(n) to finished but takes much less memory</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: veeru</title>
		<link>http://elliottback.com/wp/the-amazoncom-interview/#comment-2195964</link>
		<dc:creator>veeru</dc:creator>
		<pubDate>Mon, 01 Nov 2010 07:35:54 +0000</pubDate>
		<guid isPermaLink="false">/?p=867#comment-2195964</guid>
		<description>what about negative numbers??? the code will fail if sum of two numbers add up to Zero

arr[] ={ -22, 19 22} and the sought number is 31
in the first iteration, it will fail because sum of -22 and +22 is zero and the function returns true

??</description>
		<content:encoded><![CDATA[<p>what about negative numbers??? the code will fail if sum of two numbers add up to Zero</p>
<p>arr[] ={ -22, 19 22} and the sought number is 31<br />
in the first iteration, it will fail because sum of -22 and +22 is zero and the function returns true</p>
<p>??</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raj</title>
		<link>http://elliottback.com/wp/the-amazoncom-interview/#comment-2193720</link>
		<dc:creator>Raj</dc:creator>
		<pubDate>Sat, 21 Aug 2010 15:54:31 +0000</pubDate>
		<guid isPermaLink="false">/?p=867#comment-2193720</guid>
		<description>public static void main(String[] args) {
		
		int sum = 18;
		int[] arr = {2, 10, 7, 6, 9, 5, 1, 4, 9, 6, 5, 7, 8};

		Set set1 = new HashSet();
		for (int i: arr) {
			set1.add(i);
		}
		
		boolean found = false;
		Iterator itr = set1.iterator();
		while (itr.hasNext()) {
			int temp = itr.next();
			int findThis = sum - temp;
			if ((temp != findThis) &amp;&amp; set1.contains(new Integer(findThis))) {
				System.out.println(temp +&quot;+&quot;+ findThis+&quot;=&quot;+sum);
				found = true;
				break;				
			}
		}
		
		System.out.println(&quot;Is the sum found: &quot;+found);
	}</description>
		<content:encoded><![CDATA[<p>public static void main(String[] args) {</p>
<p>		int sum = 18;<br />
		int[] arr = {2, 10, 7, 6, 9, 5, 1, 4, 9, 6, 5, 7, 8};</p>
<p>		Set set1 = new HashSet();<br />
		for (int i: arr) {<br />
			set1.add(i);<br />
		}</p>
<p>		boolean found = false;<br />
		Iterator itr = set1.iterator();<br />
		while (itr.hasNext()) {<br />
			int temp = itr.next();<br />
			int findThis = sum &#8211; temp;<br />
			if ((temp != findThis) &amp;&amp; set1.contains(new Integer(findThis))) {<br />
				System.out.println(temp +&#8221;+&#8221;+ findThis+&#8221;=&#8221;+sum);<br />
				found = true;<br />
				break;<br />
			}<br />
		}</p>
<p>		System.out.println(&#8220;Is the sum found: &#8220;+found);<br />
	}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cp</title>
		<link>http://elliottback.com/wp/the-amazoncom-interview/#comment-2192107</link>
		<dc:creator>cp</dc:creator>
		<pubDate>Thu, 29 Apr 2010 00:02:23 +0000</pubDate>
		<guid isPermaLink="false">/?p=867#comment-2192107</guid>
		<description>I was just served this question when I went in for an interview at Amazon not too long ago (funnily enough, it wasn&#039;t for a software dev position, but for a support position.)

Basically, you&#039;re trying to find

1. a[i] + a[j] = c,

for some number c.

I did the O(n^2) solution initially, though they hinted at there being a faster solution, to which I scribbled the following equation:

2. a[i] = c - a[j]

If you set up the hash so that instead of array indices referring directly to the value that they hold, you flip the relation so that the value of a[i] refers to the array index:

3. a[i] =&gt; i vs. i =&gt; a[i]

Pick j = 0.  Do a hash search for a[i] with (2).  Increment j by one, then repeat, until you find the answer.  If you&#039;ve hit j &gt; last array index, then the answer is obviously not there. :)</description>
		<content:encoded><![CDATA[<p>I was just served this question when I went in for an interview at Amazon not too long ago (funnily enough, it wasn&#8217;t for a software dev position, but for a support position.)</p>
<p>Basically, you&#8217;re trying to find</p>
<p>1. a[i] + a[j] = c,</p>
<p>for some number c.</p>
<p>I did the O(n^2) solution initially, though they hinted at there being a faster solution, to which I scribbled the following equation:</p>
<p>2. a[i] = c &#8211; a[j]</p>
<p>If you set up the hash so that instead of array indices referring directly to the value that they hold, you flip the relation so that the value of a[i] refers to the array index:</p>
<p>3. a[i] =&gt; i vs. i =&gt; a[i]</p>
<p>Pick j = 0.  Do a hash search for a[i] with (2).  Increment j by one, then repeat, until you find the answer.  If you&#8217;ve hit j &gt; last array index, then the answer is obviously not there. <img src='http://elliottback.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mikeo</title>
		<link>http://elliottback.com/wp/the-amazoncom-interview/#comment-2191971</link>
		<dc:creator>mikeo</dc:creator>
		<pubDate>Wed, 21 Apr 2010 07:56:49 +0000</pubDate>
		<guid isPermaLink="false">/?p=867#comment-2191971</guid>
		<description>I think I got it in O(n)

For each integer in array
  check a hash table if number exists as key
  if it does return true
  else push its complement (taget_sum - current_int) to hash
loop

return false

You have to be very careful about precision though.  This code would likely only work with integers.

this is O(n)</description>
		<content:encoded><![CDATA[<p>I think I got it in O(n)</p>
<p>For each integer in array<br />
  check a hash table if number exists as key<br />
  if it does return true<br />
  else push its complement (taget_sum &#8211; current_int) to hash<br />
loop</p>
<p>return false</p>
<p>You have to be very careful about precision though.  This code would likely only work with integers.</p>
<p>this is O(n)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic page generated in 0.157 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-05-21 01:14:52 -->

