RSS

What is HashSet Type?

  • It’s a collection, kind of like Array.
  • Store in form of object’s hash.
  • Unordered.
  • Has standard collection methods: Add, Remove, Contains.
  • Faster in term of performance, compare to List.
  • No access by index. Thus, most methods take in <T> as parameter.

Reference: MSDN

 
Leave a comment

Posted by on March 6, 2015 in General

 

Tags: ,

Bubble Sort Algorithm

A simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

bubble-sort-algorithm

Reference: Wikipedia

 
Leave a comment

Posted by on March 5, 2015 in General

 

Tags: ,

Software Laws

I was looking for a specific theorem called CAP theorem, but came across these interesting, sort of funny but somewhat true, software laws.

I think these laws are useful to get better understanding of philosophy behind a software or computer science in general.

  • Atwood’s Law

    Any software that can be written in JavaScript will eventually be written in JavaScript.

  • Brooks’s Law

    Adding manpower to a late software project makes it later.

  • Conway’s law

    Any piece of software reflects the organizational structure that produced it.

  • Miller’s law

    To understand what another person is saying, you must assume that it is true and try to imagine what it could be true of.

  • Wirth’s law

    Software gets slower faster than hardware gets faster.

 
Leave a comment

Posted by on March 1, 2015 in General

 

Tags: , , , ,

Determine Hierarchy Name Based on Inconsistent Level

Below are sets of inconsistent arrays that represents level in hierarchy:

  • [0, 1, 2]
  • [0, 1]
  • [0]

The order of hierarchy is (from the top to the bottom):

  • Director
  • Manager
  • Employee

The highest number in the array represents the bottom level in the hierarchy. The lowest number in the array represents higher level in the hierarchy.

So, given [0, 1, 2]: 0 means Director, 1 means Manager and 2 means Employee. And given [0, 1]: 0 means Manager and 1 means Employee.

What’s the most efficient way to assign LevelName property in the following data set. The answer doesn’t have to use specific programming language.

[{
    Fullname: "Bob",
    ReportTo: "",
    Level: "0",
    LevelName: ""
},{
    Fullname: "John",
    ReportTo: "Bob",
    Level: "1",
    LevelName: ""
},{
    Fullname: "Sally",
    ReportTo: "John",
    Level: "2",
    LevelName: ""
},{
    Fullname: "David",
    ReportTo: "John",
    Level: "2",
    LevelName: ""
},{
    Fullname: "Rachel",
    ReportTo: "Bob",
    Level: "1",
    LevelName: ""
},{
    Fullname: "Peter",
    ReportTo: "Rachel",
    Level: "2",
    LevelName: ""
}]
 
Leave a comment

Posted by on January 28, 2015 in General

 

Tags: , , ,

Differences Between ViewData and ViewBag in ASP.Net MVC

ViewData

  • Accessible in both controller and view.
  • Store objects as key / value pairs.
  • Introduced in ASP.Net MVC 2.
  • Use ‘magic strings’, such as:
    ViewData["stack247"] = "stack247";

ViewBag

  • Accessible in both controller and view.
  • Store objects as Dynamic type.
  • Introduced in ASP.Net MVC 3
  • Use ‘magic properties’, such as:
    ViewData.Stack247 = "stack247";

TempData

  • Accessible in both controller and view.
  • Store objects as key / value pairs.
  • Use ‘magic strings’, such as:
    TempData["stack247"] = "stack247";
  • Meant for very short-lived instance.
  • Persistent after a redirect, means TempData’s properties will still be accessible after redirection.
  • Good scenario to use is when passing error message to an error page.
  • More information: Greg Shackles’s TempData
 
Leave a comment

Posted by on January 26, 2015 in General

 

Tags: ,

Heapsort Algorithm

A comparison-based sorting algorithm, it divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the smallest element and moving that to the sorted region.
Larger nodes don’t stay below smaller node parents. They are swapped with parents, and then recursively checked if another swap is needed, to keep larger numbers above smaller numbers on the heap binary tree.

heapsort-algorithm

Reference: Wikipedia

 
Leave a comment

Posted by on January 14, 2015 in General

 

Tags: ,

Explicit Conversion X as Operator

What’s the difference between explicit conversion, as operator and calling a method to a conversion?

public void Test2()
{
    var obj = "stack247";

    var str1 = (string)obj;
    var str2 = obj as string;
    var str3 = obj.ToString();
}

Conclusion (as seen from the result below):

  • Explicit conversion throws InvalidCastException if obj is not a string type.
  • Explicit conversion assigns anything to obj, even if it’s null.
  • as operator returns null if obj is not a string type.
  • as operator can’t be use in value type because it’s not null-able, unless the value type is null-able (for example, int? will work)
  • ToString(), or whatever the method that return certain type, will throw NullReferenceException if obj is null.
  • ToString(), or whatever the method that return certain type, will return string representation of an object if the object can’t be converted.
public void Test1()
{
    var obj = "stack247";

    var str1 = (string)obj;
    // Return stack247

    var str2 = obj as string;
    // Return stack247

    var str3 = obj.ToString();
    // Return stack247
}
public void Test2()
{
    var obj = new object();

    var str1 = (string)obj;
    // Return InvalidCastException (Unable to cast object of type 'System.Object' to type 'System.String'

    var str2 = obj as string;
    // Return null

    var str3 = obj.ToString();
    // Return System.Object
}
public void Test3()
{
    var obj = 247;

    var str1 = (int)obj;
    // Return 247

    var str2 = obj as int;
    // Return The 'as' operator must be used with a reference type or nullable type ('int' is non-nullable value type).

    var str3 = obj as int?;
    // Return 247

    var str4 = obj.ToString();
    // Return 247
}
public void Test4()
{
    object obj = null;

    var str1 = (string)obj;
    // Return null

    var str2 = obj as string;
    // Return null

    var str3 = obj.ToString();
    // Return NullReferenceException (Object reference not set to an instance of an object).
}
 
Leave a comment

Posted by on January 13, 2015 in General

 

Tags: , , ,

 
Follow

Get every new post delivered to your Inbox.

Join 51 other followers

%d bloggers like this: