GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  C#
Go To First  |  Previous Question  |  Next Question 
 C#  |  Question 424 of 436    Print  
C# Program to test efficient coding
You have several instances of the following class (User). Provide a class with three methods:
"AddUser" accepts a User instance as a parameter and add it to a collection.
"GetUser" accepts a UserID as a parameter and returns the corresponding User instance. It is important that this executes quickly as it will be used often.
"GetOrderedUserArray" returns an array of User instances ordered by UserID. It is important that this executes quickly as it will be used often.

public class User
{
public int UserID;
public string FamilyName;
public string GivenName;

public User(
int userID,
string familyName,
string givenName)
{
UserID = userID;
FamilyName = familyName;
GivenName = givenName;
}
}



  
Total Answers and Comments: 4 Last Update: August 01, 2009     Asked by: netacct 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
December 04, 2008 00:15:50   #1  
richa2008 Member Since: December 2008   Contribution: 2    

RE: C# Program to test efficient coding

public NewClass()
{


public Arraylist m_Arrusers;
public void AddUser(User objser)
{


m_arrusers.Add(objUser);

}

public User GetUser(int UserID)
{
User obj;
for( int i 0; i<m_ArrUsers.GetCount();i++)
{
obj (User)m_ArrUsers[i];
if(obj.Userid userID)
return obj;

}

}



 
Is this answer useful? Yes | No
January 22, 2009 11:06:22   #2  
Farohar Member Since: January 2009   Contribution: 3    

RE: C# Program to test efficient coding
If at an interview I would certainly have failed. Took me far to long to implement the sort. Also I have no clue weather this is efficient or not - all I know is that it works ...

using System;
using System.Collections;

namespace user
{
class Program
{
static void Main(string[] args)
{
UserList uList new UserList();

for (int i 100; i > 0; i--)
uList.AddUser(new User(i "FName" "GName"));

Console.WriteLine(uList.GetUser(2).FamilyName);

foreach (User u in uList.GetOrderedUserArray())
Console.WriteLine("ID: {0} FName: {1} GName: {2}"
u.UserID
u.FamilyName
u.GivenName);
Console.ReadKey();
}
}

public class User : IComparable
{
public int UserID;
public string FamilyName;
public string GivenName;

// default (ascending) sort
int IComparable.CompareTo(Object obj)
{
User uid1 (User)this;
User uid2 (User)obj;
if (uid1.UserID < uid2.UserID) return 1;
if (uid1.UserID > uid2.UserID) return -1;
else return 0;
}

public User(int userID string familyName string givenName)
{
UserID userID;
FamilyName familyName;
GivenName givenName;
}
}

// do descending sort on UserID property.
public class sortUIdDescendingHelper : IComparer
{
int IComparer.Compare(Object a Object b)
{
User uid1 (User)a;
User uid2 (User)b;
if (uid1.UserID > uid2.UserID) return 1;
if (uid1.UserID < uid2.UserID) return -1;
else return 0;
}
}

// the class holding our new three methods
public class UserList
{
ArrayList userList new ArrayList();

public void AddUser(User user) { userList.Add(user); }

public User GetUser(int userID)
{
foreach (User u in userList)
if (u.UserID userID) return u;
return (User)userList[0];
}

public ArrayList GetOrderedUserArray()
{
IComparer myComparer new sortUIdDescendingHelper();
// userList.Sort() // default (descending) sort
userList.Sort(myComparer); // ascending sort
return userList;
}
}
}


 
Is this answer useful? Yes | No
January 24, 2009 07:47:36   #3  
Farohar Member Since: January 2009   Contribution: 3    

RE: C# Program to test efficient coding
Actually the code can be a lot more efficient. First of all instead of using ArrayList one should use List<T>. That way we would not need to create a new ArrayList just to find a certain item in the array. We would just access the item via "listname[n]". That is instead of:

public User GetUser(int userID)
{
foreach (User u in userList)
if (u.UserID userID) return u;
return (User)userList[0];
}

we do

public User GetUser(int userID) { return userList[userID]; }

which is still not perfect as every time we access this method a new instance of the class User is created. Instead of doing it that way I would just access the list directly via "uList.userList[n].value".

Also I would not create a method "GetOrderedUserArray()" of type List<User> as calling it creates another list in memory. Now why would we need another memory consuming list if we got one already? Considering the method is supposedly called often it makes even less sense to design it that way. To sort the list we do not have to create a new list sort it and return it (or rather as I did it in the first example sort our existing list copy it to a new list and return that). Just sort the existing one and we're done. So instead of doing

foreach (User u in uList.GetOrderedUserArray())
Console.WriteLine("ID: {0} FName: {1} GName: {2}"
u.UserID
u.FamilyName
u.GivenName);

which creates a new instance of the class "User" and also creates a new "List<User>" we could just access those list items via

uList.userList.Sort();
for (int i 0; i < uList.userList.Count; i++)
Console.WriteLine("ID: {0} FName: {1} GName: {2}"
uList.userList[i].UserID
uList.userList[i].FamilyName
uList.userList[i].GivenName);

which uses our existing "List<User>" and does not need to instanciate a new class "User" either.

This means: two of the methods we were supposed to code are simply gone! We dont actually need them or rather dont even want them. The inherited function "Sort()" replaces "GetOrderedUserArray()" and instead of returning a new User instance with "GetUser()" we access the List directly to get our hands at its items.

This is not only more efficient memory wise it also is less code and less methods. Here the full code:

----- 8< ----- 8< ----- 8< ----- 8< ----- 8< ----- 8< -----

using System;
using System.Collections.Generic;

namespace UserSort
{
class Program
{
static void Main(string[] args)
{
UserList uList new UserList();

for (int i 100; i > 0; i--)
uList.AddUser(new User(i "FName" + i "GName" + i));

uList.userList.Sort();
for (int i 0; i < uList.userList.Count; i++)
Console.WriteLine("ID: {0} FName: {1} GName: {2}"
uList.userList[i].UserID
uList.userList[i].FamilyName
uList.userList[i].GivenName);
Console.ReadKey();
}
}

public class User : IComparable
{
public int UserID;
public string FamilyName;
public string GivenName;

// default (ascending) sort
int IComparable.CompareTo(Object obj)
{
User uid1 (User)this;
User uid2 (User)obj;
if (uid1.UserID < uid2.UserID) return 1;
if (uid1.UserID > uid2.UserID) return -1;
else return 0;
}

public User(int userID string familyName string givenName)
{
UserID userID;
FamilyName familyName;
GivenName givenName;
}
}

// the class holding our new three methods (rather one method now ... )
public class UserList
{
public List<User> userList new List<User>();
public void AddUser(User user) { userList.Add(user); }
}
}

----- >8 ----- >8 ----- >8 ----- >8 ----- >8 ----- >8 -----

I know it has been a long answer off a previous and long answer by myself. Nevertheless I believed it proper to post it as the way C# works it just far too easy to not care about how many resources an application is instanciating. One could argue "Well the garbage collecter will handle all this!" but then: more efficient is more efficient garbage collector or not.

 
Is this answer useful? Yes | No
August 01, 2009 00:40:47   #4  
broli Member Since: August 2009   Contribution: 1    

RE: C# Program to test efficient coding
Here's an easy way to take care of the sorting part by using the new Linq feature in .NET 3.5

Assuming you have an ArrayList with all the user objects.

ArrayList array new ArrayList();
Add all the user objects...........

//Arraylist isn't supported by Linq so you must specify type
var linq array.OfType<User>();
var sort from i in linq orderby i.UserID select i;

Now the arraylist "sort" will contain all the objects sorted by user id.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape