Technical Questions 1

1) Which is better – singly linked list or doubly linked list? What are the pros and cons of using each?

Singly-linked list:

Pros: Simple in implementation, requires relatively lesser memory for storage, assuming you need to insert/delete at next node; insertion/deletion is faster.

Cons: Cannot be iterated in reverse, need to maintain a handle to the head node of the list else, the list will be lost in memory. If you’re deleting previous node or inserting at previous node, you will need to traverse list from head to previous node to be able to carry out those operations – O(N) time.

That should be used when you have lesser memory and your main purpose is insertion/deletion and not searching elements.

Doubly-linked list:

Pros: Can be iterated in forward as well as reverse direction. In case of needing to delete previous node, there is no need to traverse from head node, as the node to be deleted can be found from ‘.previous’ pointer.

Cons: Relatively complex to implement, requires more memory for storage (1 ‘.previous’ pointer per node). Insertions and deletions are relatively more time consuming (assigning/reassigning ‘.previous’ pointer for neighbor nodes)

That should be used when you have no minimal limitations on memory, and your main purpose is to search for elements.

2) What’s the difference between Managed code and Native code? Give examples of each you have worked on.
The big difference is memory management. With Native Code, you need to do your own resource (RAM / Memory) allocation and cleanup. If you forget something, you end up with what’s called a “Memory Leak” that can crash the computer. A Memory Leak is a term for when an application starts using up (eating up) Ram/Memory but not letting it go so the computer can use if for other applications; eventually this causes the computer to crash. With Managed Code, all the resource (RAM / Memory) allocation and cleanup are done for you and the risk of creating “Memory Leaks” is reduced to a minimum. This allows more time to code features instead of spending it on resource management.

  • · Managed code runs under CLR whereas native code does not run under CLR.
  • · Managed code takes advantages of memory management, security and threading services CLR provides, whereas these are missing for native code that directly runs on the machine.
  • · The managed code is the one generated by C# and other .NET language compilers (IL) and gets finally converted to native code by JIT. The code in languages prior to .NET like VC++ get converted directly to native code and runs on the COM components

Over the past two years, I developed web applications for extending the server by modules which are developed in two ways (We have specific requirements that demand native code development and would like to convert our existing native ISAPI components, take advantage of this API to build server components.):

a) Using managed code, and the ASP.NET server extensibility APIs

b) Using native code, and the IIS native server extensibility APIs

3) What is thread safe code? Give an example of times you’ve used thread safe code.
Thread-safety is a computer programming concept applicable to multi-threaded programs. A piece of code is thread-safe if it is reentrant or protected from multiple simultaneous executions by some form of mutual exclusion.

Thread-safety is a key challenge in multi-threaded programming. In a multi-threaded program, several threads execute simultaneously in a shared address space. Every thread has access to virtually all the memory of every other thread. Thus the flow of control and the sequence of accesses to data often have little relation to what would be reasonably expected by looking at the text of the program. This violates the principle of least astonishment. Thread-safety is a property aimed for so as to minimize surprising behavior by re-establishing some of the correspondences between the actual flow of control and the text of the program.

General-purpose types are rarely thread-safe in their entirety, for the following reasons:

  • · The development burden in full thread safety can be significant, particularly if a type has many fields (each field is a potential for interaction in an arbitrarily multithreaded context).
  • · Thread safety can entail a performance cost (payable, in part, whether or not the type is actually used by multiple threads).
  • · A thread-safe type does not necessarily make the program using it thread-safe, and often the work involved in the latter makes the former redundant.

Example: Methods used in Logger, to write a log file, should be threading safe for an application that runs under multi-threading environment.

.csharpcode, .csharpcode pre
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
background-color: #f4f4f4;
width: 100%;
margin: 0em;
.csharpcode .lnum { color: #606060; }

   1: public static class SharedLogger

   2: {

   3:     private static LogToFile _writer = new LogToFile();

   4:     private static object _lock = new object();

   5:     public static void Write(string text)

   6:     {

   7:         lock (_lock)

   8:         {

   9:             _writer.LogToFile(string text);

  10:         }

  11:     }

  12: }