<?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>StaticMethod &#187; Programming Problems</title>
	<atom:link href="http://www.staticmethod.net/category/like-things-youd-see-on-an-interview/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.staticmethod.net</link>
	<description>Things programmers might find interesting</description>
	<lastBuildDate>Thu, 04 Mar 2010 20:05:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Project Euler</title>
		<link>http://www.staticmethod.net/2009/05/28/project-euler/</link>
		<comments>http://www.staticmethod.net/2009/05/28/project-euler/#comments</comments>
		<pubDate>Thu, 28 May 2009 20:54:29 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Data and Algorithms]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Programming Problems]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[project euler]]></category>

		<guid isPermaLink="false">http://www.staticmethod.net/?p=88</guid>
		<description><![CDATA[Found a cool site called Project Euler
From their description:
Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.
I&#8217;ve done [...]]]></description>
			<content:encoded><![CDATA[<p>Found a cool site called <a href="http://www.projecteuler.net">Project Euler</a></p>
<p>From their description:</p>
<blockquote><p>Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.</p></blockquote>
<p>I&#8217;ve done a few of the first problems, implemented them all in ruby so far (or just did the math required), and they&#8217;ve definitely been interesting and fun to work on.  4 down, 242 to go.  All of the problems are math related, and you are basically presented with the problem and a field to input your answer.  After you supply the correct answer you&#8217;re given a link to the forum (most of which are locked now) where people have discussed their approaches to solving the problem.</p>
<p>If you&#8217;re looking to brush up on some math skills, or just need some interesting problems to throw some code at, this site provides some great things to work on.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.staticmethod.net/2009/05/28/project-euler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript Map &#8211; Version 0.2</title>
		<link>http://www.staticmethod.net/2009/05/20/javascript-map-02/</link>
		<comments>http://www.staticmethod.net/2009/05/20/javascript-map-02/#comments</comments>
		<pubDate>Wed, 20 May 2009 17:41:30 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[Programming Problems]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[map]]></category>

		<guid isPermaLink="false">http://www.staticmethod.net/?p=79</guid>
		<description><![CDATA[After trying to use my previous map implementation in a Firefox extension I&#8217;ve been working on I realized that you can&#8217;t add random attributes to some objects you deal with in that world.  This made my previous approach unworkable. After doing some more searching, I found an approach that seems to work well so far.
The [...]]]></description>
			<content:encoded><![CDATA[<p>After trying to use my previous map implementation in a Firefox extension I&#8217;ve been working on I realized that you can&#8217;t add random attributes to some objects you deal with in that world.  This made my previous approach unworkable. After doing some more searching, I found an approach that seems to work well so far.</p>
<p>The basic idea is that we want to be able to generate an identifier for a random object consistently, then we can associate this identifier with a value.  The limitation is that the identifier needs to be a string (limitation of Javascript&#8217;s internal associative array/object construct), and that native javascript objects don&#8217;t have an internal mechanism for generating an ID (unlike Java&#8217;s Object class, which provides a hashCode() method).</p>
<p>The process to solve this problem has two steps in my current implementation:</p>
<ol>
<li>Store the &#8216;key&#8217; object in an array</li>
<li>Use the &#8216;key&#8217; object&#8217;s array index as the key value for the association</li>
</ol>
<p>This way, I can tell if a passed in &#8216;key&#8217; object has a value in my map by seeing if it&#8217;s in my internal array.  If it&#8217;s in there, I can look up it&#8217;s index&#8217;s value in my associative array (or anonymous object).  If I&#8217;m adding a new key/value pair, I insert the key object into my internal array and then use it&#8217;s index as the &#8216;key&#8217; for my associative array.  Removing a key/value pair from the map involves the following steps:</p>
<ol>
<li>Look up the index of the key object in my internal array</li>
<li>Remove the value that corresponds to the index from the associative array/anonymous object</li>
<li>Remove the key object from the internal array</li>
</ol>
<p>Here&#8217;s an example of the code that inserts a key/value pair into the map, this is a snip from the code, which is more object-oriented:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Internal holders</span>
<span style="color: #003366; font-weight: bold;">var</span> _internalMap <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> _internalArray <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/*
 * Private function for getting the key for an object
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> getKey<span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> key <span style="color: #339933;">=</span> _internalArray.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>key <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        _internalArray.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        key <span style="color: #339933;">=</span> _internalArray.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #006600; font-style: italic;">// &quot;Keys&quot; will be of the form &quot;OR.x&quot; where 'x' is the index in the array</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;OR.&quot;</span> <span style="color: #339933;">+</span> key<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/*
 * Internal put method
 */</span>
put <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> objKey <span style="color: #339933;">=</span> getKey<span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    _internalMap<span style="color: #009900;">&#91;</span>objKey<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The full implementation has get, remove, and size methods as well, and is built as a class, but this kind of shows the mechanism behind it all.  One thing to note is that this implementation relies on Javascript 1.6 (or higher) as the &#8216;indexOf()&#8217; array method was introduced in Javascript 1.6</p>
<p>I&#8217;ve attached the new version (version 0.2), you can find it here: <a rel="attachment wp-att-78" href="http://www.staticmethod.net/2009/05/20/javascript-map-02/jsmap-02/">JsMap Version 0.2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.staticmethod.net/2009/05/20/javascript-map-02/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Really quick overview of Java Concurrency/Threads</title>
		<link>http://www.staticmethod.net/2009/05/18/really-quick-overview-of-java-concurrencythreads/</link>
		<comments>http://www.staticmethod.net/2009/05/18/really-quick-overview-of-java-concurrencythreads/#comments</comments>
		<pubDate>Mon, 18 May 2009 18:53:03 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Programming Problems]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[quick]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://www.staticmethod.net/?p=74</guid>
		<description><![CDATA[Threading is a topic that I feel gets asked about a lot in interviews, but in certain practices doesn&#8217;t get used very much, so it&#8217;s easy to forget the details.  I stumbled across this really quick java threading/concurrency review that didn&#8217;t really teach me anything new, but I still thought it was a good [...]]]></description>
			<content:encoded><![CDATA[<p>Threading is a topic that I feel gets asked about a lot in interviews, but in certain practices doesn&#8217;t get used very much, so it&#8217;s easy to forget the details.  I stumbled across this really quick java threading/concurrency review that didn&#8217;t really teach me anything new, but I still thought it was a good reminder:</p>
<p><a href="http://www.vogella.de/articles/JavaConcurrency/article.html">Java Concurrency / Multithreading &#8211; Tutorial</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.staticmethod.net/2009/05/18/really-quick-overview-of-java-concurrencythreads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cuckoo Hash &#8211; Part 2</title>
		<link>http://www.staticmethod.net/2009/05/13/cuckoo-hash-part-2/</link>
		<comments>http://www.staticmethod.net/2009/05/13/cuckoo-hash-part-2/#comments</comments>
		<pubDate>Wed, 13 May 2009 19:26:13 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Data and Algorithms]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Programming Problems]]></category>
		<category><![CDATA[cuckoo hash]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.staticmethod.net/?p=70</guid>
		<description><![CDATA[Small update, the recursion was killing me, so I introduced some attempts to control how deeply the code recurses.  My approach was basically to introduce a depth count, and if the depth count reaches 0, I force a storage growth on the next rehash, thus reducing the number of collisions by increasing the storage size.
This [...]]]></description>
			<content:encoded><![CDATA[<p>Small update, the recursion was killing me, so I introduced some attempts to control how deeply the code recurses.  My approach was basically to introduce a depth count, and if the depth count reaches 0, I force a storage growth on the next rehash, thus reducing the number of collisions by increasing the storage size.</p>
<p>This approach is working pretty well, especially if the size of the storage is set to a larger size to start with.   The insertions and hashing and lookups are fast, it&#8217;s the rehashing and collisions that cause lots of problems.<a rel="attachment wp-att-71" href="http://www.staticmethod.net/2009/05/13/cuckoo-hash-part-2/cuckoo_hash1/"></a></p>
<p><a rel="attachment wp-att-71" href="http://www.staticmethod.net/2009/05/13/cuckoo-hash-part-2/cuckoo_hash1/">Cuckoo Hash Part 2</a> &#8211; Second &#8216;version&#8217; of my cuckoo hash implementation, including the changes above.  It now uses a list of 499 commonly used english words, and inserts 6 permutations of the list (pattern: &#8220;word-<em>i</em>&#8220;, where i is 1-6), for a total insertion count of 2994.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.staticmethod.net/2009/05/13/cuckoo-hash-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cuckoo Hash</title>
		<link>http://www.staticmethod.net/2009/05/12/cuckoo-hash/</link>
		<comments>http://www.staticmethod.net/2009/05/12/cuckoo-hash/#comments</comments>
		<pubDate>Tue, 12 May 2009 17:44:41 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Data and Algorithms]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Programming Problems]]></category>
		<category><![CDATA[cuckoo hash]]></category>
		<category><![CDATA[hashing]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.staticmethod.net/?p=65</guid>
		<description><![CDATA[From Wikipedia:
Cuckoo hashing is a scheme in computer programming for resolving hash collisions of values of hash functions in a table
Very basically, a Cuckoo hash is a table, where the position of an element can be one of two places.  On insertion, if there is another element (call it &#8216;y&#8217;) in the candidate spot, it [...]]]></description>
			<content:encoded><![CDATA[<p>From <a href="http://en.wikipedia.org/wiki/Cuckoo_hashing">Wikipedia</a>:</p>
<blockquote><p><strong>Cuckoo hashing</strong> is a scheme in computer programming for resolving <span class="mw-redirect">hash collisions</span> of values of hash functions in a table</p></blockquote>
<p>Very basically, a Cuckoo hash is a table, where the position of an element can be one of two places.  On insertion, if there is another element (call it &#8216;y&#8217;) in the candidate spot, it is replaced by the item you are inserting.  Then you re-add the &#8216;y&#8217; element to the hash, using the second hash method.  Doing this could cause another element (call it &#8216;z&#8217;) to be displaced, in which case the cycle repeats until all elements are in their new spots.</p>
<p>To look up if an element is in the table, you need to check both possible spots.</p>
<p>You can run into a problem with cycles when inserting values if, for example,  <em>h1(x) = h2(y)</em> and  <em>h2(x) = h1(y)</em>, but you can limit the number of re-insertions to avoid this.  If you reach this limit, you can change your hashing methods and re-insert everything again. This problem happens even more frequently if the load factor on your table is too high, so you can put checks in place to increase the size of the table if you detect that the load is getting high.  In my example, I set the top table load factor to 60%, at which point I double the size of the table and re-insert the elements.</p>
<p>The main problem I&#8217;m running into is that the <em>insert</em> method can call the <em>rehash</em> method if it loops too many times trying to place everything&#8230;but <em>rehash</em> calls <em>insert</em> to rebuild the table with new hashing methods.  This seems to lead to deeply recursive problems, even with a load set to 50%.  I&#8217;m not sure how to fix this right now, but I&#8217;ll keep working at it and take any suggestions.  One method might be to find better hashing algorithms, but I thought the one I was using was descent.</p>
<p>Anyway, since I&#8217;m still learning about it, here&#8217;s a few links to read:</p>
<ul>
<li><a class="external text" title="http://www.it-c.dk/people/pagh/papers/cuckoo-undergrad.pdf" rel="nofollow" href="http://www.it-c.dk/people/pagh/papers/cuckoo-undergrad.pdf">Cuckoo Hashing for Undergraduates, 2006</a>, R. Pagh, 2006. (PDF)</li>
<li><a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/142054">Some String hashing algorithms</a></li>
<li><a rel="attachment wp-att-67" href="http://www.staticmethod.net/2009/05/12/cuckoo-hash/cuckoo_hash/">Cuckoo Hash Attempt 1</a> &#8211; My first attempt at implementing a Cuckoo Hash in Ruby</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.staticmethod.net/2009/05/12/cuckoo-hash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Javascript Map Implementation</title>
		<link>http://www.staticmethod.net/2009/05/11/simple-javascript-map-implementation/</link>
		<comments>http://www.staticmethod.net/2009/05/11/simple-javascript-map-implementation/#comments</comments>
		<pubDate>Mon, 11 May 2009 19:59:30 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Programming Problems]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[map]]></category>

		<guid isPermaLink="false">http://www.staticmethod.net/?p=59</guid>
		<description><![CDATA[One of the downsides to javascript (at least in my opinion) has been it&#8217;s lack of more intelligent collection classes, such as the Map.  Javascript&#8217;s array and object constructs are very flexible and useful in their own right, but sometimes you just need a way to map one object (as a key) to some arbitrary [...]]]></description>
			<content:encoded><![CDATA[<p>One of the downsides to javascript (at least in my opinion) has been it&#8217;s lack of more intelligent collection classes, such as the Map.  Javascript&#8217;s array and object constructs are very flexible and useful in their own right, but sometimes you just need a way to map one object (as a key) to some arbitrary value, and then have the possibility to look up that value later.  I was playing around with some ideas for how to get a simple map to work in javascript and came up with my JsMap class.  It&#8217;s a very small class, more of an exercise than anything, but maybe someone will find it useful.</p>
<p>This map class is very limited, as (as far as I can tell) javascript objects don&#8217;t have internal hashing methods or object ids that allow for simple identification in the context of a map.  In order to make up for this, my map class generates a random ID for a passed in &#8220;key&#8221; object and then uses that random ID as the key in the internal map.  The generated key is then stored in a variable <em>__jsKey </em>in the passed in &#8220;key&#8221; object.  When the &#8220;get&#8221; method on the map is called, it looks to see if the passed in &#8220;key&#8221; object has a <em>__jsKey</em> variable, and if so, looks up the value associated with it in the internal map.</p>
<p>Here is a link to the attachment page containing the javascript file, which I&#8217;ll update with changes as they&#8217;re made: [<a rel="attachment wp-att-57" href="http://www.staticmethod.net/2009/05/11/simple-javascript-map-implementation/jsmap/">JsMap</a>].</p>
<p>Here&#8217;s a simple HTML test of the map class: [<a rel="attachment wp-att-58" href="http://www.staticmethod.net/2009/05/11/simple-javascript-map-implementation/test/">JsMap Test/Example usage file</a>]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.staticmethod.net/2009/05/11/simple-javascript-map-implementation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bloom Filters</title>
		<link>http://www.staticmethod.net/2009/05/03/bloom-filters/</link>
		<comments>http://www.staticmethod.net/2009/05/03/bloom-filters/#comments</comments>
		<pubDate>Mon, 04 May 2009 01:48:20 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[Programming Problems]]></category>
		<category><![CDATA[bloom filter]]></category>
		<category><![CDATA[data structures]]></category>

		<guid isPermaLink="false">http://www.staticmethod.net/?p=34</guid>
		<description><![CDATA[Bloom filters are a mechanism for &#8220;indexing&#8221; a set and quickly determining whether or not a given element is part of the set that the Bloom Filter represents.  You do not actually store items in a bloom filter, the bloom filter itself is simply an array of bits.  The (very) basic idea is that for [...]]]></description>
			<content:encoded><![CDATA[<p>Bloom filters are a mechanism for &#8220;indexing&#8221; a set and quickly determining whether or not a given element is part of the set that the Bloom Filter represents.  You do not actually store items in a bloom filter, the bloom filter itself is simply an array of bits.  The (very) basic idea is that for every element <em>i</em> in the set, you generate a series of array indexes (usually via some hashing method), and then set those positions in your bit array to 1.</p>
<p>For example, for the input <em>&#8220;foo&#8221;</em> you could generate the indices 1, 4 and 6.  Then in your (fixed size) array, you set positions 1, 4 and 6 to &#8220;1&#8243;.     For the input <em>&#8220;bar&#8221;</em> you generate the indices 1, 4, and 8, and set those positions to &#8220;1&#8243;.  When you want to find out if an input is in the set, you generate the index values (via your hashing method) and check if those positions are set to 1 in the array.  If all of them are, the input was likely in the set.  If one or more of the indices were not set to 1, then the input was definitely not in the set.  You can vary the number of index positions your hashing function provides and the size of your bit-array in order to reduce the number of false positives.</p>
<p>After your bit-array is built, representing your set, it becomes a constant time operation to check if something is in the set.  Insertion of new items into the Bloom Filter is also a constant time operation.  One major downside to Bloom Filters that I can see is that there isn&#8217;t a way to remove something from the bloom filter without causing false negatives.</p>
<p>The two articles I found useful in learning about Bloom Filters were:</p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Bloom_filter">Bloom Filter</a> &#8211; Wikipedia Bloom Filter Article</li>
<li><a href="http://www.coolsnap.net/kevin/?p=13">Article</a> at Cool/Snap? blog</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.staticmethod.net/2009/05/03/bloom-filters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
