<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>China Cat</title>
	<atom:link href="http://www.chinacatblog.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chinacatblog.com</link>
	<description>my RAM</description>
	<lastBuildDate>Thu, 02 Sep 2010 21:40:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>I overthunk it</title>
		<link>http://www.chinacatblog.com/2010/09/02/i-overthunk-it/</link>
		<comments>http://www.chinacatblog.com/2010/09/02/i-overthunk-it/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 21:40:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Misc]]></category>

		<guid isPermaLink="false">http://www.chinacatblog.com/?p=67</guid>
		<description><![CDATA[I got seriously duped today by an age-old logic question... A co-worker of mine asked me and another co-worker the following question: Question: If there's 3 light switches in 1 room and 3 light bulbs in a different room downstairs. With only taking 1 trip to the room with the light bulbs, how can you [...]]]></description>
			<content:encoded><![CDATA[<p>I got seriously duped today by an age-old logic question... A co-worker of mine asked me and another co-worker the following question:</p>
<p><strong>Question: </strong></p>
<p>If there's 3 light switches in 1 room and 3 light bulbs in a different room downstairs. With only taking 1 trip to the room with the light bulbs, how can you tell what light switches control which light bulbs?</p>
<p>*<em>If you don't already know it, don't go looking it up because it'll ruin my story <img src='http://www.chinacatblog.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </em></p>
<p>I mulled over it a bit after work that day. I even asked my 8-year old son. Thankfully, he didn't know either (although I would have been impressed and proud, I would've felt even dumber).</p>
<p>While sitting in traffic, I was wracking my brain - there had to be a mathematical solution to this, but I just can't figure it out! Then I started to doubt whether or not there was a mathematical solution... I thought, "Maybe my co-worker left out a detail? Could the light switches be dimmers? But that would make it a stupid question! Let me just do a quick search on my iPhone, but I won't look at the answer..."</p>
<p>So I did just that.... Two of the URLs that came up were <a title="wiki.answers.com" href="http://wiki.answers.com/Q/There_is_two_rooms_one_has_three_light_bulbs_in_it_the_other_has_three_switches_you_are_only_allowed_in_the_first_room_once_you_start_in_the_secon_how_do_you_find_out_which_switch_operates_which_bulb">wiki.answers.com</a> and <a title="mathisfun.com" href="http://www.mathsisfun.com/cellar_problem.html">mathisfun.com</a>. I thought, "Well if the answer is listed on <a title="mathisfun.com" href="http://www.mathsisfun.com/cellar_problem.html">mathisfun.com</a>, there HAS TO BE a mathematical solution! I mean, I always knew that I wasn't a math genius, but come on... am I this bad??"</p>
<p><strong>Solution:</strong></p>
<p>I eventually gave in.... I cheated and looked it up on <a title="wiki.answers.com" href="http://wiki.answers.com/Q/There_is_two_rooms_one_has_three_light_bulbs_in_it_the_other_has_three_switches_you_are_only_allowed_in_the_first_room_once_you_start_in_the_secon_how_do_you_find_out_which_switch_operates_which_bulb">wiki.answers.com</a><a title="wiki.answers.com" href="http://www.google.com/url?sa=t&amp;source=web&amp;cd=1&amp;ved=0CBwQFjAA&amp;url=http%3A%2F%2Fwiki.answers.com%2FQ%2FThere_is_two_rooms_one_has_three_light_bulbs_in_it_the_other_has_three_switches_you_are_only_allowed_in_the_first_room_once_you_start_in_the_secon_how_do_you_find_out_which_switch_operates_which_bulb&amp;rct=j&amp;q=3%20light%20switches%20and%203%20light%20bulbs&amp;ei=RhaATMm0OIW6sAO75amcAQ&amp;usg=AFQjCNE0vCRdvz_9GOEKwqJgBwVZJEAVBQ&amp;sig2=7XSa9ejk_C-_0XJ_lJU9IQ&amp;cad=rja"></a>. The answer was so "common sense" and so NOT mathematical, I thought for sure that the wiki's answer was a layman's answer and that the "real" answer had to be on <a title="mathisfun.com" href="http://www.mathsisfun.com/cellar_problem.html">mathisfun.com</a>.... Nope, that's it.</p>
<p>Sometimes I don't think... And sometimes I overthink...</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chinacatblog.com/2010/09/02/i-overthunk-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Subset Sum with a negative number</title>
		<link>http://www.chinacatblog.com/2010/07/08/subset-sum-with-negative-2/</link>
		<comments>http://www.chinacatblog.com/2010/07/08/subset-sum-with-negative-2/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 23:28:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[data algorithm problems]]></category>

		<guid isPermaLink="false">http://www.chinacatblog.com/?p=43</guid>
		<description><![CDATA[Problem: Given a sorted array say int num[10]={-5,1,6,7,9,9,20,25,31,45}, write a function void findsum(int [],int x) that can print out all possible pair of elements in array that can make a sum equal to x. For example if value of x is 26, then function should print {1,25}, {25,1}, {-5,31}, {31,-5}. private static void findSumFunction() { [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem:</strong><br />
Given a sorted array say int num[10]={-5,1,6,7,9,9,20,25,31,45}, write a function void findsum(int [],int x) that can print out all possible pair of elements in array that can make a sum equal to x. For example if value of x is 26, then function should print {1,25}, {25,1}, {-5,31}, {31,-5}. </p>
<pre class="brush: java;">
private static void findSumFunction() {

    //The problem provided me with an already-sorted array
	final int[] testData = new int[]{-5,1,6,7,9,9,20,25,31,45};

	final int X = 26;

    /*first task is to loop through original sorted array and find out where
    * searching can stop. If x = 26, then searching can stop once you get
    * to numbers &gt; 26 EXCEPT for the fact that there are negative number(s)
    * in the test data.
    * So first, find out what the lowest number is. If it is negative, adding a
    * simple calculation to the algorithm will handle it.
    * find lowest number to see if there is a negative number, this is O(1)
    */
	if (testData[0] &lt; lowestNumber) {
		lowestNumber = testData[0];
	}
	System.out.println(&quot;lowestNumber is &quot; + lowestNumber);

    //1st go through array backward &amp; stop before any value + negative num &gt; X
    //because those elem(s) are not needed. Worst case, this is O(n)
	int rightIndex = testData.length - 1;
	while (testData[rightIndex] + lowestNumber &gt; X) {
		int answer = testData[rightIndex] + lowestNumber;
		System.out.println(&quot;don't need &quot; + answer);
		rightIndex--;
	}

	//Now go through the array frontward.Never go past rightIndex
	//Simultaneously I am going through the array frontward and backward
	int leftIndex = 0;
	while (leftIndex &lt; rightIndex) {
		if (testData[leftIndex] + testData[rightIndex] == X) {
			System.out.println(testData[leftIndex] + &quot; + &quot; + testData[rightIndex]);
			leftIndex++;
			rightIndex--;
		}
		else if (testData[leftIndex] + testData[rightIndex] &lt; X) {
			leftIndex++;
		}
		else {
			rightIndex--;
		}
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.chinacatblog.com/2010/07/08/subset-sum-with-negative-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Singly Linked List &#8211; remove element</title>
		<link>http://www.chinacatblog.com/2010/07/08/singly-linked-list-remove/</link>
		<comments>http://www.chinacatblog.com/2010/07/08/singly-linked-list-remove/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 23:25:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[data algorithm problems]]></category>

		<guid isPermaLink="false">http://www.chinacatblog.com/?p=22</guid>
		<description><![CDATA[Problem: Given a singly linked list, write a function void DeleteBefore(node **head, data). For example, if the list has values 1->3->6->7->8->9->13, if data is 7, then element with data = 6 will be removed. As I've said before, Java's LinkedList, which is an implementation of a doubly linked list, would make solving this problem trivial, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem:</strong><br />
Given a singly linked list, write a function void DeleteBefore(node **head, data). For example, if the list has values 1->3->6->7->8->9->13, if data is 7, then element with data = 6 will be removed.</p>
<p>As I've said before, Java's LinkedList, which is an implementation of a doubly linked list, would make solving this problem trivial, so that is exactly why I must use a singly linked list.</p>
<p>First, I'll create a Java object that represents an element in a singly linked list.</p>
<pre class="brush: java;">
public class SinglyLinkedListElement {
	private int data;
	private SinglyLinkedListElement next;

	public SinglyLinkedListElement(int data) {
		this.data = data;
		next = null;
	}
	public int getValue() {
		return this.data;
	}
	public SinglyLinkedListElement getNext() {
		return this.next;
	}
	public void setNext(SinglyLinkedListElement next) {
		this.next = next;
	}
}
</pre>
<p>Next I'll (1) instantiate a SinglyLinkedListElement for each test integer creating a singly linked list and (2) remove the element before the element x (in this case, x is hard-coded to be 9).</p>
<pre class="brush: java;">
public static void removePreviousElementSinglyLinkedList() {

    int[] testData = new int[]{1, 3, 6, 7, 8, 9, 13};
    int x = 9;

    SinglyLinkedListElement head = null;
    SinglyLinkedListElement prevElem = null;
    for (int i = 0; i &lt; testData.length; i++) {
    	SinglyLinkedListElement newElem = new SinglyLinkedListElement(testData[i]);
    	if (head == null) {
    	    head = newElem;
    	}
    	else {
    	    prevElem.setNext(newElem);
    	}
    	prevElem = newElem;
    }

    SinglyLinkedListElement curr = head;
    SinglyLinkedListElement prevElem = null;
    SinglyLinkedListElement prevPrevElem = null;
    while (curr != null) {
    	if (curr.getValue() == x) {
    		if (prevElem == null) {
    			System.out.println(&quot;No previous element to delete&quot;);
    		}
    		else {
    			if (prevPrevElem == null) {
    				//deleting head elem, so make the curr elem the new head
    				prevElem.setNext(null);
    				head = curr;
    			}
    			else {
    				prevPrevElem.setNext(curr);
    			}
    		}
    		break;
    	}
    	else {
    		if (prevElem != null) {
    			prevPrevElem = prevElem;
    		}
    		prevElem = curr;
    	}
    	curr = curr.getNext();
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.chinacatblog.com/2010/07/08/singly-linked-list-remove/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Singly Linked List &#8211; mth to the last element</title>
		<link>http://www.chinacatblog.com/2010/07/08/mth-to-the-last-element/</link>
		<comments>http://www.chinacatblog.com/2010/07/08/mth-to-the-last-element/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 22:00:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[data algorithm problems]]></category>

		<guid isPermaLink="false">http://www.chinacatblog.com/?p=14</guid>
		<description><![CDATA[Example Problem: Given a singly-linked list, devise a time- and space-efficient algorithm to find the mth-to-last element of the list. Implement your algorithm, taking care to handle relevant error conditions. Define mth to last such that when m = 0, the last element of the list is returned. 1->3->6->7->8->9->13 Java's LinkedList class, which is an [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Example Problem:<br />
</strong>Given a singly-linked list, devise a time- and space-efficient algorithm to find the mth-to-last element of the list. Implement your algorithm, taking care to handle relevant error conditions. Define mth to last such that when m = 0, the last element of the list is returned. 1->3->6->7->8->9->13</p>
<p>Java's LinkedList class, which is an implementation of a doubly linked list, would make this task trivial, so that is exactly why it must be done in a singly linked list. Here is my implementation in Java.</p>
<p>First I would create a Java object representing an element in a singly linked list.</p>
<pre class="brush: java;">
public class SinglyLinkedListElement {
	private int data;
	private SinglyLinkedListElement next;

	public SinglyLinkedListElement(int data) {
		this.data = data;
		next = null;
	}
	public int getValue() {
		return this.data;
	}
	public SinglyLinkedListElement getNext() {
		return this.next;
	}
	public void setNext(SinglyLinkedListElement next) {
		this.next = next;
	}
}
</pre>
<p>Now I'll (1) instantiate a SinglyLinkedListElement for each test integer creating a singly linked list and (2) find the element that is mth from the last. In this case, m is hard-coded to be 3 below.</p>
<pre class="brush: java;">
public static void getMthToLastElementInSinglyLinkedList() {

    int[] testData = new int[]{1, 3, 6, 7, 8, 9, 13};

    SinglyLinkedListElement head = null;
    SinglyLinkedListElement prevElem = null;
    for (int i = 0; i &lt; testData.length; i++) {
    	SinglyLinkedListElement newElem = new SinglyLinkedListElement(testData[i]);
    	if (head == null) {
    	    head = newElem;
    	}
    	else {
    	    prevElem.setNext(newElem);
    	}
    	prevElem = newElem;
    }
    final int M = 3;

    //advanced the head to at least M
    SinglyLinkedListElement curr = head;
    for (int i = 0; i &lt; M; i++) {
    	if (curr.getNext() != null) {
    		curr = curr.getNext();
    	}
    	else {
    		int listCount = i + 1;
    		System.out.println(&quot;Not enough items in list&quot;);
    	}
    }

    SinglyLinkedListElement mBehind = head;
    while (curr.getNext() != null) {
    	mBehind = mBehind.getNext();
    	curr = curr.getNext();
    }
    System.out.println(&quot;Value of mBehind is &quot; + mBehind.getValue());
}
</pre>
<p>Reference: <a href="http://www.piexposed.com/">Programming Interviews Exposed</a> (provides a solution in C)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chinacatblog.com/2010/07/08/mth-to-the-last-element/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Big O (&#8230;not that kind of O)</title>
		<link>http://www.chinacatblog.com/2010/07/08/bigo/</link>
		<comments>http://www.chinacatblog.com/2010/07/08/bigo/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 21:57:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[data algorithm problems]]></category>

		<guid isPermaLink="false">http://www.chinacatblog.com/?p=7</guid>
		<description><![CDATA[I got this list from a Princeton site, but here's my condensed, I-wasn't-smart-or-rich-enough-for-Princeton version. O(1): constant Running time is the same no matter how many inputs you have (N). O(log N): logarithmic Gets slightly slower as N grows. When N doubles, the running time increases by a constant. Example: Binary search, insert/delete on heap or [...]]]></description>
			<content:encoded><![CDATA[<p>I got this list from a <a href="http://www.cs.princeton.edu/introcs/41analysis/">Princeton site</a>, but here's my condensed, I-wasn't-smart-or-rich-enough-for-Princeton version.</p>
<p><strong>O(1): constant</strong><br />
Running time is the same no matter how many inputs you have (N).</p>
<p><strong>O(log N): logarithmic</strong><br />
Gets slightly slower as N grows. When N doubles, the running time increases by a constant.<br />
Example: Binary search, insert/delete on heap or BST</p>
<p><strong>O(N): linear</strong><br />
Running time increases directly proportional to growth of N. When N doubles, so does running time.</p>
<p><strong>O(N log N): linearithmic</strong><br />
When N doubles, the running time slightly more than doubles.<br />
Example: Quicksort, mergesort</p>
<p><strong>O(N<sup><span style="vertical-align:super;">2</span></sup>): quadratic</strong><br />
Acceptable for relatively small problems. When N doubles, the running time increases fourfold.</p>
<p><strong>O(N<sup><span style="vertical-align:super;">3</span></sup>): cubic</strong><br />
Acceptable only for small problems. When N doubles, running time increases eightfold.</p>
<p><strong>O(2<sup><span style="vertical-align:super;">N</span></sup>): exponential</strong><br />
Not typically appropriate for practical use. When N doubles, the running time squares.</p>
<p><strong>O(N!): factorial</strong><br />
When N increases by 1, running time increases by a factor of N.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chinacatblog.com/2010/07/08/bigo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
