Just a random passing thought…..

In Windows, we call them folders but on *nix we call them directories.

However the catch is in DOS, we also used to call them directories. Somewhere along the way (presumably the switch from Windows 3.1(1) to Windows95) the nomenclature was changed for this entity and it was renamed from a directory to a folder.

Funnily enough, i’ve never actually caught myself using the wrong nomenclature until now….I’ve always referred to them as directories when in Linux or in the DOS-shell, and referred to them as folders when working with the incumbent.

how strange.

I’ve recently been getting my hands dirty with WPF as a successor to WinForms, and one thing is for sure – its like i’m starting from ground zero, all over again…

One point which i’d heard while watching a screencast, is that working with WPF changes the developer mindset from “here is a visual component i wish to use” to “here is the behaviour i would like from a visual component”. I might be wrong, but i recall this as being referred to as the “visualless state” of controls. (i’ll try to find that reference and correct it).

Either way, it’s actually quite true. In WinForms, you pick a control because of the behaviour, but also because its appearance is something that (within reasonable tolerance) suits to your situation. In WPF, that concept of “the control which looks right” is gone. You can style/design your controls to look and behave in a way you would want. This was never clearer than the proof that you can turn a regular listbox as you know it in the Win32 control set to look like anything you want:

WPF Custom ListBox

…and the XAML is really straightforward – just style your item template to look exactly how you want. Good-bye owner drawing of Win32!

<pre>
<Window x:Class=”WPFCustomListBoxLayout.Window1″ xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” Title=”Window1″ Height=”300″ Width=”300″ Loaded=”Window_Loaded”>
<Grid>
<ListBox Name=”lstItems” MaxHeight=”300″>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush=”Blue” Margin=”4,0,4,0″ BorderThickness=”1″ CornerRadius=”3″>
<StackPanel Orientation=”Vertical”>
<StackPanel Orientation=”Horizontal” Background=”AntiqueWhite”>
<TextBlock FontSize=”16″ Text=”{Binding Path=FirstName}” />
<TextBlock FontSize=”16″ Text=” ” />
<TextBlock FontSize=”16″ Text=”{Binding Path=LastName}” />
</StackPanel>
<TextBlock FontSize=”12″ Text=”{Binding Path=Age}” />
<TextBlock FontSize=”12″ Text=”{Binding Path=FavouriteMovie}” />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation=”Horizontal” />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
</Window>
</pre>

This is really just a stub post, and should force me to come back and add more detail to the topic.

In a nutshell, be careful if you have parallelism turned ON for your multi-core SQL server as SQL’s internal worker threads can seemingly jam themselves into a deadlock scenario when executing a parallelised (sp?) query.

to disable parallelism, use the OPTION (MAXDOP 1) query hint.

SELECT col1, col2, col3
FROM table1 t1 INNER JOIN table 2 t2 on t1.id = t2.id
WHERE SomeCondition = SomeValue
OPTION (MAXDOP 1)

This will force SQL Server to run the query with a max parallelism of 1 (ie: single thread). Good-bye locks!

Some references:
Query Hint (T-SQL)
Appropriate Uses of Parallelism in SQL Server
Detecting and Ending Deadlocks
Deadlock Troubleshooting (Part 1)

Recent problems with a client website have required me to re-discover a whole lot of performance tid-bits which i’d either long forgot or just plan never knew.

In trying to stress-test the SQL server forming the backend of the website, we ended up exceeding the web-server’s capabilities and it would die with a not-so-nice error citing “HttpException (0x80004005): Server Too Busy” and a call-stack dump internal to the framework.

I was lucky enough to find an article on the MSDN which explains a work-around for the ASP.NET web-application request queue limit.

<httpRuntime appRequestQueueLimit=”5000″>

Just add this element in your <system.web /> web.config element (and change the value 5000 to whatever you deem appropriate)

As the title suggests, here is my solution for A Programming Job Interview Challenge #13 – Brackets

Your input is a string which is composed from bracket characters. The allowed characters are:’(‘, ‘)’, ‘[‘. ‘]‘, ‘{’, ‘}’, ‘< ’ and ‘>’. Your mission is to determine whether the brackets structure is legal or not.

Example of a legal expression: “([](< {}>))”.

Example of an illegal expression: “({< )>}”.

Provide the most efficient, elegant and simple solution for that problem.I think that this is a well known problem but I am not sure, so excuse me if this week’s question is screwed.

I think this one is rather easy….unless i missed something…..

So to start with, you have a dictionary/hashtable and a stack. The dictionary keys consist of all opening bracket characters (ie: “(, [, <, {” ), and the corresponding values for each key are the closing bracket characters (ie: “), ], >, }”).

Now the process itself is to iterate over the string from left to right, and look up the current character in the dictionary.

  • If the current character is an opening bracket, get the corresponding close bracket, push it on the stack and move on.
  • If it is not an opening bracket, then pop the current closing bracket off the stack.
  • If the popped character is the same as the current character, then move on (ie: valid closing bracket match).
  • If the popped character is NOT the same as the current character, then we’ve hit a broken sequence and error out here.

There is some fine-tuning that would need to be done in the case where the input sequence commenced with a closing bracket and there is nothing in the stack, but what you see is basically it. 🙂 Simple and elegant IMO.

Thank God i don’t need to explain the mathematical complexity of this one…I really wouldn’t know where to start.

Wow lots has happened in the last 4 hours. Where do i start?

  • In order to diagnose a locking/blocking issue on SQL server, start with an sp_who2 to give you the list of active PIDS, and the PIDS blocking requests. Then execute DBCC INPUTBUFFER(<BLOCKING PID>) to find the offending SQL
  • In order to free space on a critical HDD, you can move the data over to a new location on the same machine and create a junction (a symoblic link for the *nix folk). New feature of NTFS 3.0 and IMO not used anywhere near as heavily as it should. Most underrated feature in Windows.
  • It’s hard to come across a good music compilation, but by God someone has managed to do it

On Saturday I had to take the wife to UTS library so that she could do some research for her masters thesis. With petrol prices being the way they are, and just generally the amount of time it was going to take to go home and come back, I’d decided i was going to pop into the office with my laptop, and play WoW for 2-3 hours while I waited for her. Flawless plan, really. The last time i took my personal laptop to the office, everything worked no problems and I was actually able to log onto WoW.

Well not so lucky this time :|. The IT admins had obviously blocked the WoW ports through the firewall. So after a bit of Googling and reading, I came across this article which clearly explains How To Tunnel WoW Through an SSH VPN Using Only Linux and PuTTY.

It was so awesome, it worked straight off the bat, no extra tweaking, I just had to blog about it. My only gripe would be that the latency was too high for me to do any instancing (i had about 800ms latency at best, and 3000 at worst). But that’s a combination of the fact that my company doesn’t want to pay for a fast internet connection, and my Linux server runs over wireless.

In it’s 11th installment in a series of posts of interesting/quirky questions you may (or may not) be asked in a job interview, this series has definitely challenged everything I knew (or thought I knew) about computer science fundamentals and mathematical reasoning.

My response is to the 11th question about summing numbers is below:

PROBLEM:
Given a list of n integers and another integer called m, determine (true / false) if there exist 2 numbers in that list which sum up to m.
Example: 2,6,4,9,1,12,7 and m=14 -> 2 and 12 sum up to 14, so the answer is true.
Provide the best algorithm in both manners: performance and memory to solve this puzzle. Don’t forget to mention the complexity of your solution!

Answer
I suppose I will start by giving my answer first because it seems logical to me, and i’m generally better at providing solutions rather than the mathematical proof behind them 🙂

Basically put, the most efficient way I can think of to determine if any of the numbers sum up to m is to work from left-right, subtract the current number from m, and then scan the rest of the list for the resulting number.
ie:

In a list consisting of the numbers 2,6,4,9,1,12,7 and m=14, my process will look like:

idx = 0
val = a[idx] (2)
searchFor = m - val (12)
Found 12 in the 5th position

Because you’re working from left to right, you don’t need to scan the array from the start every time, so on the first scan you’ll search through n – 1 elements, on the second scan you’l search through n – 2 elements and so on until you’re up to the last element in the array. According to the rules, there needs to exist 2 numbers which add up to m, so the last element CANNOT = m

I believe the most efficient way of scanning the list of values. Happy to be proven wrong 🙂 I guess one optimisation would be to sort the elements of the array upfront, and then you can use a super-efficient sorted list scan algorithm, if you have a large array of elements. Also one of the commenters questioned whether duplication of numbers was/was not allowed – i see this point as irrelevant under this solution because 7 + 7 = 14, so why wouldn’t it be allowed?!

Reasoning

Here (for me) is where things start falling apart. I might have this totally wrong but i’ll give it a shot anyway….

Given m=14,
With List = 2, 6, 4, 9, 1, 12, 7 (ie: 7 elements in array)

The maximum complexity of the alg would be given by the maximum number of scans it would have to do to find the winning combination of numbers. In this example, it would be expressed as:
(1 * 7-1) + (1 * 7-2) + (1 * 7-3) + (1 * 7-4) + (1 * 7-5) + (1 * 7-6)
Again, we don’t worry about case 7-7 because there needs to exist 2 numbers which add up to m (and either way it’s a zero ;P)

Which roughly translates into:
6 + 5 + 4 + 3 + 2 + 1

Now substituting for n, we see it comes to
(n – 1) + (n – 2) + (n – 3) + … (n – (n – 2)) + (n – (n – 1))
which boils down to n(n+1)/2

So the maximum complexity of this solution would be O(n(n+1)/2)

This feels like such a hacky maths proof – my HS maths teacher would be livid…