RSS

Monthly Archives: January 2015

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: ""
}]
Advertisements
 
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).
}
 
1 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: , , , ,

 
%d bloggers like this: