Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Programming without library functions

Options
  • 20-05-2008 12:53pm
    #1
    Closed Accounts Posts: 8,199 ✭✭✭


    Hey folks,

    I'm going to an interview soon and I will be asked to pseudo-code up a solution to some sort of problem. I'm pretty sure I won't be allowed to use standard C# functions like Int32.parse etc.

    So lets say I wanted to convert an Int to a float or vice versa, how can I do it without using something like Convert.ToInt or Int32.parse?

    I've no idea why in reality this would ever be the case in a normal job, but it's something I need to know about. I'm most proficient with C# and I know it has various rules so you simply can't pass a varable of one type to another without converting it.

    Cheers,


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Don't work with C# but what you are trying to do is a cast.
    http://msdn.microsoft.com/en-us/library/ms173105(VS.80).aspx


  • Registered Users Posts: 2,150 ✭✭✭dazberry


    Hey folks,

    I'm going to an interview soon and I will be asked to pseudo-code up a solution to some sort of problem. I'm pretty sure I won't be allowed to use standard C# functions like Int32.parse etc.

    So lets say I wanted to convert an Int to a float or vice versa, how can I do it without using something like Convert.ToInt or Int32.parse?

    I've no idea why in reality this would ever be the case in a normal job, but it's something I need to know about. I'm most proficient with C# and I know it has various rules so you simply can't pass a varable of one type to another without converting it.

    Cheers,

    Hmm, if they're looking for pseudo-code, it might suggest that its more at the computer science end of the scale they're looking at, so needs to take into consideration the sign, exponent and fraction - :eek:

    Its seems straight forward enough to convert without using the library functions although I don't get to use C# all that much.
                float f = 32.9F;
                int i = (int)f;
                float f2 = i;
                this.Text = String.Format("{0}", f2); 
    

    I also whipped together a string to int converter just as an exercise, but can't help feel that that's not the point of the exercise. Is there any sort of mathematical element to the job, statistics, fixed point math or anything similar???

    I did do an interview years ago where they wanted a sorting algorithm and prime number generator in pseudo code - but libraries and frameworks were much less common in those days.

    D.


  • Closed Accounts Posts: 413 ✭✭sobriquet


    Casting an int to a char won't work here, C# uses the C style ASCII table for characters, so if we have char c = '0', and cast it as int i = (int)c - i will be 48.

    Try this:
    char c = '0';
    int i = (int)c;
    Console.WriteLine("0 == " + i.ToString());
    
    int j = i - 48;
    Console.WriteLine(j.ToString());
    
    string numbers = "0123456789";
    foreach (char number in numbers)
    {
    	Console.Write((((int)number) - 48).ToString() + ", ");
    }
    
    // Exercise: If "1050", ie a string representing one thousand and fifty is to be converted
    // to the integer 1050, how is this accomplished?
    
    Console.Read();
    

    WRT to the interview, it's probably just to test your programming chops. You wouldn't (well, shouldn't) not use a library function in practice unless it was known (and proven, with data) to be defective before you go rolling your own.

    Even if you get caught on something from left field, try to work it out out loud, how you'd go about investigating a solution. In this case, when you remove the use Int32.Parse or Convert.ToInt32, you try to print a casted char but it's off, so then you modify that, etc etc. Even if you don't solve it, this should reflect well on you.

    Stuff like that should be more commonplace IMO. I've always been a bit wary at interviews where they've no interest in asking technical questions at all.


  • Closed Accounts Posts: 8,199 ✭✭✭G-Money


    I've never been employed as a commercial developer so my skills can be rusty at times. When I write a tool I usually just think about what it needs to do and sit down and write it.

    In the interview situation it can be very hard as they will ask you some mad question about extracting a value from a singly linked list or something equally obscure. I once got asked to convert a float to an int or vice versa so automatically I said I would use Int32.parse. At the time I couldn't remember the exact name of the function but I knew roughly what it was. He then goes to me "you can't use that". I was like "WTF is this?"

    I know they need to ask tech questions but I don't really know why they insist that you don't use standard functions that are widely used and recommended.

    In all its a tough experience as you only get a few minutes to work the thing out and you feel thick if you can't. In a real world scenario programmers and coders would be at a desk with the compiler, books, people and the internet all at their disposal but in the interview you have nothing to reference except whats in your head.


  • Closed Accounts Posts: 413 ✭✭sobriquet


    I once got asked to convert a float to an int so automatically I said I would use Int32.parse. At the time I couldn't remember the exact name of the function but I knew roughly what it was. He then goes to me "you can't use that".
    Did he not just mean that Int32.Parse is inappropriate - ie Double.Parse should be used?
    In a real world scenario programmers and coders would be at a desk with the compiler, books, people and the internet all at their disposal but in the interview you have nothing to reference except whats in your head.
    This is why I pointed out that you should show your work, and give a good attempt. Evidence of reasoned thinking about a problem is great even if you don't have the right answer. Most interviewers will understand this, especially if you've no prior working experience.

    Besides that, in the real world you may have a compiler, books and resources etc - but that doesn't mean you should or can use them. I spend my days in C# - but only 1.1, which means tons of stuff on the web is irrelevant to me. The large third party UI libraries I have access to are a good four years old and lacking in many ways that need compensating for (indexes into certain collections for example). My last job was writing network code in C - all I could use was the stdlib. I've never had a job where I wasn't constrained one way or another.
    I was like "WTF is this?" [...] I know they need to ask tech questions but I don't really know why they insist that you don't use standard functions that are widely used and recommended. [...] In all its a tough experience as you only get a few minutes to work the thing out and you feel thick if you can't.
    To be honest if I was doing interviews I'd be looking to see evidence of someone's approach to solving problems; a good way to do that is present a constrained environment. If they got pissy with a question they weren't comfortable with I'd be inclined to think they weren't self-confident with their abilities, which most likely stems from lack of experience or ability.

    Being asked to do something without generics or Linq or any post-1.1 C# features would be a very appropriate question in our interviews, but would irritate someone like yourself as it seems arbitrary and pointless. Which I suppose I could've wrote instead of all of the above. Oh well.


  • Advertisement
  • Closed Accounts Posts: 8,199 ✭✭✭G-Money


    What he meant was that I wasn't allowed to use that function even though it would have worked and that I had to come up with an alternative solution which quite frankly left me truly stuck.

    I see your point in that in some situations a person may be using an older version of the language or using a dev environment where there may not be an abundance of reference material. I guess it just caught me by surprise and not being an experienced programmer, I was finding it hard to know of any other way around the issue.


  • Closed Accounts Posts: 413 ✭✭sobriquet


    What he meant was that I wasn't allowed to use that function even though it would have worked and that I had to come up with an alternative solution which quite frankly left me truly stuck.
    Fair enough.
    in some situations a person may be using
    I don't think it's even a case of may - I honestly can't think of any programming job I know anyone doing day to day that that isn't the case to some non-trivial extent.

    As to being caught by surprise, that was probably the point. ;)


  • Closed Accounts Posts: 8,199 ✭✭✭G-Money


    You're probably right. :)

    Anyway the interview should be finished before this time tomorrow so hopefully I won't be pulling what's left of my hair, out in frustration.


  • Closed Accounts Posts: 8,199 ✭✭✭G-Money


    Ok guys, interview is over. I was expecting it to be tough enough and it was I think. The guy who interviewed me was really friendly and patient which he had to be.

    The first solution I was asked to solve was one where the program reads in a string such as "54" etc but if the string contained text, so "54Sometext" then that would be invalid, and the string had to be converted to an int.

    I got into all sorts of trouble with this one. I didn't realise that the string is just a set of single characters and that the thing was reading in one at a time. So it would read in the 5 then the 4 seperately and I had to store that somehow and convert it to an int. As as I expected, I wasn't allowed to use Convert.ToInt so I suggested a cast, although I'm not sure if it would compile properly.

    This issue caught me out as the solution was to do some mathematics with the 5 then the 4 and output it and he mentioned "to the power of" and all that which completely f****d me up. I'm really bad with numbers.

    Eventually we moved on to another question which was a palindrome. I think I managed to sort this out in about 5-10 minutes without too much help but I feel like the damage was already done with the original thing.

    Fun and games.


  • Closed Accounts Posts: 413 ✭✭sobriquet


    Chin up dude, sure you gave it a shot. Might turn out ok yet. Good experience too.

    You didn't perchance work through the code I supplied above? Simply casting doesn't work - it compiles, but doesn't do what you think it might. If you did there's an obvious `bool isDigit(char c)` function that could come out of it, once you've that stripping the number sequence out of the string isn't hard.

    Converting it to it's integer form isn't really hard either, it does involve powers of ten. Think back to tens and units (and hundreds, and thousands etc).

    If you want to have a go do, for the experience. I wrote up a solution there, PM me for it if you want.


  • Advertisement
  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    What part of don't request PMs in thread is unclear?!


  • Closed Accounts Posts: 6,718 ✭✭✭SkepticOne


    Good luck in your next interview if you did not get the current one.

    The point of pseudo-code in an interview is to show that you understand how to specify an algorithm without reference to any specific language. Knowing too much about one particular language may hinder you rather than help.

    In the question given, I think the interviewer wanted to know if you could construct a number given its decimal expression in a string of numerals. This is a exercise rather than a problem you are likely to encounter in a typical job.

    As sobriquet advises, show your work. The interviewer should then be able to see that you have a basic grasp. Here's how I would proceed:

    The string "534" for example represents 5*100 + 3*10 + 4*1

    or 5*10^2 + 3*10^1 + 4*10^0

    You can see that the digit is multiplied by a power of 10 and this power is based on the position of the digit in the string as measured from the right. All these products are then added up giving us the number.

    Then just express this in pseudo code.


  • Closed Accounts Posts: 891 ✭✭✭conceited


    hex $30 to $39 represent the ascii 0-9

    http://webster.cs.ucr.edu/AoA/Windows/HTML/MoreDataRepresentationa2.html#4.4.1

    Read that and that should help you in the future.
    Goodluck in your next interview.


  • Registered Users Posts: 5,379 ✭✭✭DublinDilbert


    conceited wrote: »
    hex $30 to $39 represent the ascii 0-9

    http://webster.cs.ucr.edu/AoA/Windows/HTML/MoreDataRepresentationa2.html#4.4.1

    Read that and that should help you in the future.
    Goodluck in your next interview.

    On the first question he obviously wanted you to scan the string one character at a time, till valid numeric ASCII characters were found, as shown above to be values in the range 0x30 -> 0x39.

    When you find the group of numeric values, you need to subtract off 0x30 and multiply the value by the correct power of 10. So for the example above with the string "534" this would be stored as the ASCII characters 0x35 0x33 0x34, and you would do the conversion as follows:-
    (0x35-0x30)*100 + (0x33-0x30)*10 + (0x34-0x30)*1 = 534


  • Registered Users Posts: 205 ✭✭Stugots


    SkepticOne wrote: »
    The point of pseudo-code in an interview is to show that you understand how to specify an algorithm without reference to any specific language. Knowing too much about one particular language may hinder you rather than help.

    In the question given, I think the interviewer wanted to know if you could construct a number given its decimal expression in a string of numerals. This is a exercise rather than a problem you are likely to encounter in a typical job.

    Actually I think the point of this problem is not to find out if you understand base 10 numbers, but rather to find out whether you understand how data is stored in memory, specifically how a string is stored in this case. I would consider this to be a basic requirement for someone developing embedded software or cross platform software. If you are operating at a high level, it may not always be necessary to have this level of knowledge, but if you do have it, you are more likely to be able to produce efficient code and debug it.


  • Closed Accounts Posts: 6,718 ✭✭✭SkepticOne


    Stugots wrote: »
    Actually I think the point of this problem is not to find out if you understand base 10 numbers, but rather to find out whether you understand how data is stored in memory, specifically how a string is stored in this case. I would consider this to be a basic requirement for someone developing embedded software or cross platform software. If you are operating at a high level, it may not always be necessary to have this level of knowledge, but if you do have it, you are more likely to be able to produce efficient code and debug it.
    You are right. When I hear the word pseudocode I tend to think the emphasis is on algorithms rather than specific language features such as how a string is stored. Having said that, if you don't understand how base 10 or another base numbers are constructed you won't do well either in this problem.


Advertisement