Wednesday, June 17, 2009

Tackling C Interviews

Preface

Last Sunday I met an interesting person who is supposedly my relative. He works as an Interviewer and a Technical Analyst as well. I was a lot more interested when he told me that his field of interest is also C/UNIX. We spoke for a while about general things and this is something I grabbed from him during the short convo. I am hoping to interact a lot more with him in the near future J it’s very hard to find people who really “LOVE” the technology and are passionate about C or UNIX. I know people who do hundreds and thousand lines of coding even without knowing the basic mechanism of how something work “internally”. Well… as I always say, “We need not know everything; just know things in detail”.

Disclaimer

This is a bit lengthier. Read it when you are relatively free.

Finally, here it is…The Simple Question

The moment I ask anyone what is the different between a Structure and a Union in C, they immediately pop up one of the below answers,

“I think Structure is memory waste while Union saves memory”

“Oh, that’s simple! In Union only one item can be accessed while in Structure all the items have their own individual memory”

“Hmmm, the size of a Union will be the size of the biggest variable”

None of those answers were wrong. Now, let me ask you something more…

Consider 5 variables with the below sizes:

Var1 – 2
Var2 – 4
Var3 – 2
Var4 – 1
Var5 – 8

As per the above definitions, each and every Variable in the structure has its own independent memory locations. So,

Size of Structure = Var1 + Var2 + Var3 + Var4 + Var5 = 17

Var1 | Var2 | Var3 | Var4 | Var5 |

If it were a Union , then it will look like below, assuming that Var5 is the biggest variable (in terms of size)

|----------------Var5------------|

Size of this Union = Size of the biggest variable = Var5 = 8.

This is how it gets tricky

Now, how the other variables are accommodated? We say that at any point of time only one variable can be retrieved from the Union . What will happen to the existing var5 if a new value is assigned to a variable, say Var4? How will this single memory segment of size 8 be made available for rest of the variables as well?

Various irrelevant answers

You will be surprised to read… these are some of the REAL answers which I got from experienced people (especially in C).

Answer 1:

“I think Var1 will be stored first. It is just of size 2. Then Var2 is just 4 so it will also be stored, Var3 is 2 and the Var4 is just 1. But…. But…. The fifth variable var5 is too big to fit in there…I think…. Well… It’s something… ”

Answer2:

I think that is the functionality of Union . It will take care of it all by itself

Answer3:

X: I think C Compiler handles it or something to do with the Computer memory management?

Y: Say what? Computer memory management????

X: No... I meant the Compiler Memory Management… May be…

There really were many other such ridiculous answers. I am not humiliating anyone here. I myself was like this during my early days. However, once you get a hold and start working in C, should we not be taking some pain to know its mechanism? We sure should!

So how does Union work?

This is where people go wrong! Just knowing the definition will NOT do. Know its mechanism and that is what the Interviewer is expecting from you! Also, to explain these basic stuff you don not need a C Bible or Dennis Ritchie. All you need is a C Compiler. Trust me, most of the basic things can be learnt only by experiencing – not only in the Programming world but also in the real world! Go for it! Experience it! Enlighten yourself! The below short & simple program will make you understand how Union really differs from the Structures.

% cat structunion.c
#include <stdio.h>
#include <stdlib.h>
typedef union
{
int Wind_Chill;
char Heat_Index;
} Condition;
typedef struct
{
float temp;
Condition feels_like;
} Temperature;

void main()
{
Temperature *tmp;
tmp = (Temperature *)malloc(sizeof(Temperature));
printf("\nAddress of Temperature = %u", tmp);
printf("\nAddress of temp = %u, feels_like = %u", &(*tmp).temp, &(*tmp).feels_like);
printf("\nWind_Chill = %u, Heat_Index= %u\n", &((*tmp).feels_like).Wind_Chill, &((*tmp).feels_like).Heat_Index);
}


% cc -o structunion structunion.c


% ./structunion
Address of Temperature = 165496
Address of temp = 165496, feels_like = 165500
Wind_Chill = 165500, Heat_Index= 165500

Wasn’t that clear? To add on, let us consider the below quote from ANSI/ISO 9899:1990 (the ANSI C standard) Section 6.5.2.1
Structure and union specifiers have the same form. [ . . . ] The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be stored in a union object at any time. A pointer to a union object, suitably converted, points to each of its members (or if a member is a bit-field, then to the unit in which it resides), and vice versa.

Do It Yourself

Grab a C Compiler and try executing the below snippet. You will be more clear.

#include <stdio.h>
#include <stdlib.h>
/* Declares union */
union one{
char one;
int two;
float three;
};
/* Declares structure */
struct two{
char one;
int two;
float three;
};
int main(void)
{
/* Uses tag names to create structure S and union U. */
struct two S;
union one U;
/* Outputs object sizes to screen. */
printf("%d is the size of S, as structure.\n", sizeof(S));
printf("%d is the size of U, as union.\n\n", sizeof(U));
/* Loads values into S and U, as below. */
S.one = 'A';
S.two = 3645;
S.three = 678.32;
U.one = 'A';
U.two = 3645;
U.three = 678.32;
/* Echos values to screen, so conclusions are easier. */
printf("The values 'A', 3645 and 678.32 have been loaded"
"in structure S members and\n union U members both.\n\n");
/* Shows values now in S members. */
printf("%c is equal to the value S.one\n", S.one);
printf("%d is equal to the value S.two\n", S.two);
printf("%3.2f is equal to the value S.three\n\n", S.three);
/* Shows the value now stored as corrupted and good. */
printf("%c is equal to the value U.one\n", U.one);
puts("Oops! Doesn't look like an 'A'!\n");
printf("%d is equal to the value U.two\n", U.two);
puts("Oops! Doesn't look like 3645 either!\n");
printf("%3.2f is equal to the value U.three\n\n", U.three);
/* Non-pertinent stuff for running under Windows and stuff */
system("PAUSE");
return 0;
}
Conclusion

“Know the mechanism” – This helps you fine tune your programming skills and makes you an outstanding programmer. It helps you outshine in any interview as well for mechanism is what the Interviewers are after (Unless and until the one that is interviewing you is comparatively dumb haha)

Is the below statement correct?

Mr.X is married with two daughters

I initially though it is incorrect but a bit of Grammar Rules clears that YES, it’s correct!

How?

As per the English Grammar, the verb phrase is to be married to. With the noun, you have the option of using with or to.

Examples

Mr.X has a happy marriage with/to his friend’s cousin. (Marriage is the noun here.)

S.No Statement Meaning
1 Mr.X is married with his two daughters Mr.X is the husband of two daughters (the statement does not clarify whose daughters are they)
2 Mr.X is married with two daughters Mr.X is married and living with his two daughters (the statement does not clarify who is the wife of Mr.X)
3 Mr.X is married to two daughters Same meaning as statement 1

All that glitters is not gold

Similarly, not everything that sounds correct is correct. For example, how many times have you seen people using these phrases?

  • “I can’t able to do it”, - Can’t itself shows the inability. What is the word “able” doing there?
  • “This looks much more better”, - Better itself is a comparative degree, do we really need a “more”?
  • “Did you went to office today?”  - Should we be using past tense verbs for a sentence beginning with “Did”?
  • We have less cars for sale now than last month – Less or Fewer??
  • Many more happy returns of the day – This is very common. I myself use it at times. MORE should not be preceded by MANY.
  • “… and many more features” – same as above
  • These all are useless – ALL is not at all needed there. This is because of the mother tongue influence. (In Tamil we say, Iwai anaithum. Similarly in Hindi, “Ye sab”)
  • I am having two daughters – Should it not be “I have two daughters”?

These are widely used these days and are 100 percent incorrect! Just because everyone is using and it sounds correct, it does not mean it is correct! Let’s try to correct ourselves :)

You might feel this article boring and vague if you do not know what are RSS, Atom.XML, Feeds, etc.image

Very recently I thought of starting a lyrics website just to make $$$ out of it. It is not tough to create a website, promoting it or even making it rank top. What makes it hard is the content creation. Any website involves a lot of work on the content filling part. I can not sit and write all the lyrics letter by letter for all the songs out there. I rather can copy and paste from other sites which has the lyrics but then for how long? Everyone is busy these days so am I. I would go and read something instead of wasting my time in CTRL+C & CTRL+V. I then was wondering… could there be any script which reads data from other sites and write it automatically in my site. That is when I came across this Feed Parsing concept.

 

 

This is nothing new to the RSS world, but to me. I am glad that I got to know this at least by now. I have been reading the whole night about it and got to know what it is. I have not experienced it yet and I don’t know how it works either. I need to spend another week on it to know how it works. I then shall write you all a detailed article with appropriate script snippets.

Interested ones can read these:

Anatomy of RSS Feeds – Get started here if you are new to Feeds

Universal Feed Parser

XML Parser Functions

Magpie RSS

Download this – It parses RSS & Atom feeds in Python

Ruby Feed Parser

These will keep me occupied for some weeks J

It really seems cool and any scripting enthusiast (PHP/Ruby/Python) will love to explore this.

Sunday, June 7, 2009

Crazy Birthday Poem

I wanted to wish one of my dearest friends on her birthday through a poem and I definitely wanted it to be hilarious Lol and this what I dedicated her :)

You are old enough and look like a mice;
With age your brains grow bigger only in size.
Well, I wish for you a long, long life,
So your brain can grow bigger and stop causing strife.

And although we often disagree,
There’s a strong hope that I can see,
For it’s birthday time; a year has passed;
Your brain is getting bigger fast.

Hmm, how old are you now?
yeah yeah, I remember it all now!
So it's your fortieth birthday;
Four decades have gone and passed.
They say when you get older,
Time goes twice as fast.

On this day you blow the candles out;
On this day the gifts pile high;
For one day a year you’re the special kid;
"This is really great," you sigh.

"One birthday a year is really fun;
I want two!" you say.
You can’t? because you are becoming
a grandma on your way!

HAHAHA :)

Friends,

For the last 2-3 weeks I tried a lot to find a reputed and renowned University where I can do my M.Phil via distance education system. NO LUCK!

I tried contacting Madras University & Annamalai University. They told me that they do not offer distance M.Phil. I also tried Alagappa University and they said that they do not even have a M.Phil course. IGNOU seems to have M.Phil only in Philosophy while I am looking for the stream Computer Science or Management either. I even tried some of the foreign universities like University of Phoenix and Capella University. They offer Doctorate course via distance mode but then they want the student to travel twice to the US for the dissertation and presentation purpose which is not feasible for me. Well, all I want is Dr. MS, MBA. I am more than happy to die anytime afterward haha I then found that, BITS Pilani offers "OFF-Campus PhD" program. Isn't that great? :) For the full time employees, this is a boon! HP Labs already has a tie up with BITS for this program. Check http://www.hpl.hp.com/india/bits-hplindia_phd/index.html

Courses Offered:

  1. Biological Sciences
  2. Bioengineering
  3. Civil Engineering
  4. Chemical Engineering
  5. Chemistry
  6. Computer Science & Information Systems
  7. Economics and Finance
  8. Electrical and Electronics Engineering
  9. Humanistic Studies
  10. Languages
  11. Mathematics
  12. Mechanical Engineering
  13. Management
  14. Pharmacy
  15. Physics
  16. Educational Innovation and Institutional Development
Eligibility:

A Master Degree from the respective field. For example, if you got a M.S in S/W Engineering you can go for PhD in Computer Science & Information Systems. If you have a MBA then you can go for PhD in Management.

Duration:

Any PhD is for 5 years, Don't you know that? :) If you have done M.Phil, then it is just 3 years, I guess. I might be wrong, you need to check with the respective universities!

Read more at http://discovery.bits-pilani.ac.in/academics/doctoral.html.