Difference between a static class and a singleton pattern – Interview Question

This is one of the favorite questions asked by interviewers.

I thought of putting my opinions on this topic.

  1. Most important singletons can be handled polymorphically without forcing their users to assume that there is only one instance.
  2. Singletons can implement interfaces (or derive from useful base classes).
  3. Singleton class follow the object oriented principles but not static class.
  4. A singleton can be initialized lazily or asynchronously while a static class is generally initialized when it is first loaded, leading to potential class loader issues.

My rule for choosing between static and singleton:

If there are bunch of functions should be kept together, then static is the choice.
Anything else which needs single access to some resources, could be implemented singleton.

Advertisements

Collection was modified; enumeration operation might not execute

“Collection was modified; enumeration operation might not execute”  this common error developer faces every time he is updating(add/delete/sort) the collection which is being enumerated.

The question is why this happens, it understandable that Deleting and Sorting might change the way index might have sorted into but Adding should work fine – if there is no sort defined.

All this is related to the way MS has implemented enumeration for its classes.

Take List<T> as an example.

In List, there is an internal variable by name “version” which is incremented on any kind of operation done on list, this includes Add, Delete, Sort. Now when enumerator object is requested from List, internally list instantiate a struct (Enumerator) which implements IEnumerator.

Initialization of struct enumerator requires list as its mandatory parameter and locally notes down current “version” of list You can think struct enumerator as Linked-list implementation using arrays.

“MoveNext()” API call, It first checks the version noted by enumerator and list matches, secondly gives the reference of next element in the array/list.

Whenever mismatch happens exception is thrown.

All above is said about MS implementation. If you create custom collection class and override GetEnumerator function you won’t receive the error.

Reteriving current directory in batch file.

Today I ran into a strange scenario, there was a need to create a batch file which will create a service.

Now the problem is service installation path was unknown to me and for any service to install it needs to its executable’s absolute path.

After doing some hit & trial and googling, came across the solution:

ECHO “Creating some service”

SET CURRENT_DIR=%CD%

sc create someService binPath= %CURRENT_DIR%”\someService.exe” start= demand obj=  password=

Hope  this helps!!

Finding subset from a given set

Your job is find out which of find which records are not present in child set but are present in parent set.

Suppose you have 2 set of records. Parent set containing 2000 odd records and child set containing say 700 records.

To accomplish this I’ll suggest to take help from excel.

Step 1: Load parent set in a worksheet. Name as “Parent”.

Step 2: Load child set in another worksheet. Name as “Child”

Step 3: Use VLOOKUP in conjuction with ISNA function

  • VLOOKUP will do the filtering job
  • ISNA is take the result of VLOOKUP. In case the interested record is not in the list then this function returns error as #NA. ISNA function returns TRUE/FALSE to the reuslt if VLOOKUP.
  • The formula is: =IF(ISNA(VLOOKUP(A2,Child!A$2:B$15,1,FALSE))=TRUE,”Y”,”N”)

Step 4: Create a extra colum in parent worksheet name “IsMatched?” and use above said formula to the rest of job.

Record with “Y” suggest these records present in child set

Recrod with “N” suggest these records are not in child set

 

Hope this helps!!