RSS

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: , , ,

What is Sharding?

Sharding is a type of database partitioning that is used to separate very large databases the into smaller, faster, more easily managed pieces called data shards.

It’s partitioning that separates very large databases the into smaller, faster, more easily managed parts called data shards. The word shard means a small part of a whole.

 
Leave a comment

Posted by on January 8, 2015 in General

 

Tags: , , , ,

QuickSort Algorithm

Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays.

The steps are:

  1. Pick an element, called a pivot, from the array.
  2. Reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called thepartition operation.
  3. Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.

quicksort-algorithm

Reference: Wikipedia

 
Leave a comment

Posted by on December 30, 2014 in General

 

Tags: ,

What You Can Learn From My Christmas Card

I don’t think I ever hand out Christmas card to friends or colleagues, but this year, I decided to do just the opposite. Here’s the card I wrote for my colleagues at work.

what-you-can-learn-from-my-christmas-card

This is a legit, working code by the way. And I think there are few things we can learn from my Christmas card:

  • Readbility
    Good codes are readable by everybody, not just the developer. This includes the use of:

    • Indentation
    • Self-describing variable names
  • Reusability
    I write the code once and re-use it for every Christmas card I handed out this year. Write once, use many.
  • I am not good at drawing, thus the “<img src='santa.jpg' />” shortcut.

Merry Christmas and Happy New Year to all of you!

 
Leave a comment

Posted by on December 23, 2014 in General

 

Tags:

 
Follow

Get every new post delivered to your Inbox.

Join 51 other followers

%d bloggers like this: