Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Sith Interviewing Tactics [modified]

Sith Interviewing Tactics [modified]

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpcareercomgraphicsalgorithms
74 Posts 20 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    Hmm, I have written many, many recursive functions. None that break out of recursion however. Well maybe in VB a long time ago, using goto labels. Loop: and Exit:. It was still considered bad form then. My Java mentor from Sun was crazy about recursive logic. For this problem a simple for loop would have been a much eligant solution. I good at identifying eligance and implimenting it, but it is just not logical to do otherwise. Microsoft actually considered removing recursive funcion from C# when they ported it from MS Java. I think they are about as bad as mutiple class inheritance. Powerful, easy to abuse, rairly needed.

    S Offline
    S Offline
    Super Lloyd
    wrote on last edited by
    #19

    Fun Bio by the way! Those evil MVP are sneaky bastard! :laugh:

    A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

    L 1 Reply Last reply
    0
    • L Lost User

      Hmm, I have written many, many recursive functions. None that break out of recursion however. Well maybe in VB a long time ago, using goto labels. Loop: and Exit:. It was still considered bad form then. My Java mentor from Sun was crazy about recursive logic. For this problem a simple for loop would have been a much eligant solution. I good at identifying eligance and implimenting it, but it is just not logical to do otherwise. Microsoft actually considered removing recursive funcion from C# when they ported it from MS Java. I think they are about as bad as mutiple class inheritance. Powerful, easy to abuse, rairly needed.

      M Offline
      M Offline
      molesworth
      wrote on last edited by
      #20

      The point about interview questions is not to pose real world problems, but to see if candidates have the knowledge required for the job. Our interview questions can be similarly "odd", and will vary depending on the specialisation being interviewed for. What we're looking for is the ability to solve problems, and apply knowledge. What you should have done is written the algorithm, then gone on to explain why the recursive version is not as good as the iterative one - that would probably have impressed the interviewers more than someone who just wrote the code with no further explanation.

      TheArchitectualizer wrote:

      Microsoft actually considered removing recursive funcion from C#

      I think that's a myth. There are plenty of cases where recursion is in fact the easiest and best way to implement things.

      There are three kinds of people in the world - those who can count and those who can't...

      1 Reply Last reply
      0
      • S Super Lloyd

        No worry, just checking! I was wondering why you could not write the recursive function he asked! I guess now it was a psychological blockage! Next time, don't stress too much about the purity of the algorithm. Perfectionism is over hyped! I am not saying we should not be perfectionist, this is an important quality for an engineer. However perfectionism often become stubbornness, which is obviously not good hey!? ;) To stretch it a lot, that remind me of a yahoo science / psychology article about terrorists. They did some psychology profiling on terrorist and, to everyone surprise, terrorist are more likely to be engineer. They explored many conjecture as to why but the best candidate seems to be that the engineer doesn't think with nuance. Things work or don't. Ideas are good or not. Engineer are often less good at compromise. I found the idea interesting. Noticed myself I do tend to be stubborn sometimes on point of detail! ;-) Hey, don't worry, I am not implying anything about you, just let my mind wander quite a lot and thought aloud! ;)

        A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #21

        Hmm, Being a x-military anti-terrorism super freak, I understand the subject very well. My daily exersize druing my life in the millitary was to figure out how to take control of the work in 24 hours with a team of Seven. After 9/11 I was terrified at how close my anti-terrisim counter measures were to what is possible. My job, run three miles to jump on an airplane and blow up the world.

        1 Reply Last reply
        0
        • S Super Lloyd

          Fun Bio by the way! Those evil MVP are sneaky bastard! :laugh:

          A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #22

          Evil MVP a.k.a DrDotNetSky

          1 Reply Last reply
          0
          • L Lost User

            Hmm, Well if you want a laugh... I broke the pencile four times, lead twice and then I broke the pencil in half after erasing three holes into the paper. I am a visual type myself, I see all the code in my head orginized into simple structures of reusable logic, for complex things over ~200 lines of code, I use state diagrams and use cases. But I don't use a pencile for that. Seems like a wasted step. I can orginize my ideas using graphics tools. Me personal fav. is MS One Note.

            F Offline
            F Offline
            Fatbuddha 1
            wrote on last edited by
            #23

            :laugh: I was thought that someone reply to that. I have to say its long time ago I really used pencils, nowadays I am using a whiteboards or as well graphic tools. However if this thing works for you its good and cool. But I think you will agree that lot of coders are thinking that they can do it as well, which then ends up with posts in the coding horrors (worst case ). :laugh: Which is good for us, but bad for the guys that have to find such bugs. So I think to lean back and think before someone writes a function is not a wasted time. Either it be with pencil or other tools. And I have to say, letting someone writing a small function recursive is during a job interview is not cruel. Considering that they don't check the syntax. So would ask for a pseudo code or what ever. Cheers

            You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)

            1 Reply Last reply
            0
            • L Lost User

              Once I travled 400 miles to interview with a startup. All went well untill I was handed a pencile and a white sheet of paper and asked to write a recursive function in C# to produce a Fabbinicc sequence. Needles to say I didn't get the job. Do these people know that recursive algroythms = spigetti code? Hey Interviewers here is an IQ test: Penciles are for drawing as code is to? :confused: Hmm, the only thing important about Fabbinicci numbers and programming is that 1^n + 2^n ... + x^n has infinate solutions. And I'm not writting crypto software so it doesn't really matter. ~~Update~

              _I have learned much from this thread. Thanks to all who gave me a hard time!
              As a result of all my research and learning I created a 'Big O Analyzer'.

              Hope it helps someone other than myself._

              Big O Algroythm Analyzer for .NET[^] ~~Update~ 'The great advantage of recursion is that an infinite set of possible sentences, designs or other data can be defined, parsed or produced by a finite computer program.' Reference: Wikipedia on Recursion ~~Update~ If you tried the Big O tool and were disapointed that it did not find any Big O's at all, it's been updated. At infinity point = 1000 it's about 99.9991% acurate (good as gold). You might need to use .00002% brain power to figure out what the Big O is. ~TheArch :cool:

              modified on Wednesday, July 22, 2009 4:56 AM

              D Offline
              D Offline
              Distind
              wrote on last edited by
              #24

              If you want a terrifying interview question try this beast on for size From the command line take in two numeric strings, determine how you would have to create an equation out of the numbers given in the first string to result in the second or display that it is impossible. Sample input: 12 3 1+2 = 3 34 12 3*4 = 12 Only, there was one of these samples roughly 20 characters long. You either had the four typical operators or perhaps just addition and multiplication to cover. Do this in C, with no libraries, in under 6 hours, time also split doing a sodoku solver(which is a pain for those of us who'd never done one of the puzzles before, did it in two hours including learning the game). That said, if this is some how as easy as a Fibonacci sequence I would love to be proven an idiot, this thing has been bugging me for a while now. And I may have finally figured the thing out writing this, I'll have to see if my idea works once I'm out of work.

              L 1 Reply Last reply
              0
              • S Super Lloyd

                Maybe more in line with what they asked.... static int RFibonacci(int index) { if (index < 1) return 1; if (index < 2) return 2; return RFibonacci(index - 1) + RFibonacci(index - 2); } static int DFibonacci(int index) { if (index < 1) return 1; if (index < 2) return 2; int u = 1; int v = 2; int N = 1; while (N < index) { int tmp = u + v; u = v; v = tmp; N++; } return v; } But RFibonnaci, the recursive version, has to perform approximately 2^(index-1) method call. End result, the recursive method is so ungodly slow compare to DFibonnacci that it is tearful...

                A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                S Offline
                S Offline
                supercat9
                wrote on last edited by
                #25

                But RFibonnaci, the recursive version, has to perform approximately 2^(index-1) method call. If you do it right, the number of method calls will precisely equal the result. In any case, a naive approach is going to take O(phi^n) calls to complete (since the ratio of adjacent Fibonacci numbers approaches phi, i.e. (1+sqrt(5))/2 aka the Golden Mean). An alternate recursive approach is to write a method which returns two adjacent Fibonacci numbers (either as a structure, or using pass-by-reference). Starting with having an input with n<=1 yielding (0,1), and otherwise having the code for n read the values for (n-1) into (x,y) and return (y,x+y), the recursion runs nicely in linear time.

                L 1 Reply Last reply
                0
                • S supercat9

                  But RFibonnaci, the recursive version, has to perform approximately 2^(index-1) method call. If you do it right, the number of method calls will precisely equal the result. In any case, a naive approach is going to take O(phi^n) calls to complete (since the ratio of adjacent Fibonacci numbers approaches phi, i.e. (1+sqrt(5))/2 aka the Golden Mean). An alternate recursive approach is to write a method which returns two adjacent Fibonacci numbers (either as a structure, or using pass-by-reference). Starting with having an input with n<=1 yielding (0,1), and otherwise having the code for n read the values for (n-1) into (x,y) and return (y,x+y), the recursion runs nicely in linear time.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #26

                  Hmm, it is a small world. While researching ML on another thread I found three F# implimentations: (* Fibonacci Number formula *) let rec fib n = match n with | 0 | 1 -> n | _ -> fib (n - 1) + fib (n - 2) (* An alternative approach - a lazy recursive sequence of Fibonacci numbers *) let rec fibs = seq { yield! [1; 1]; for (x, y) in Seq.zip fibs (Seq.skip 1 fibs) -> x + y } (* Print even fibs *) [1 .. 10] |> List.map fib |> List.filter (fun n -> (n mod 2) = 0) |> printlist (* Same thing, using Comprehension syntax *) [ for i in 1..10 do let r = fib i if r % 2 = 0 then yield r ] |> printlist It's also recursive...

                  1 Reply Last reply
                  0
                  • D Distind

                    If you want a terrifying interview question try this beast on for size From the command line take in two numeric strings, determine how you would have to create an equation out of the numbers given in the first string to result in the second or display that it is impossible. Sample input: 12 3 1+2 = 3 34 12 3*4 = 12 Only, there was one of these samples roughly 20 characters long. You either had the four typical operators or perhaps just addition and multiplication to cover. Do this in C, with no libraries, in under 6 hours, time also split doing a sodoku solver(which is a pain for those of us who'd never done one of the puzzles before, did it in two hours including learning the game). That said, if this is some how as easy as a Fibonacci sequence I would love to be proven an idiot, this thing has been bugging me for a while now. And I may have finally figured the thing out writing this, I'll have to see if my idea works once I'm out of work.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #27

                    I have no idea, in a simular post I offered the use of lambda functions and red black trees to solve the problem. 'I am a mathimatician suffering from Pseudo-Intellectual Academia.'

                    modified on Monday, July 6, 2009 1:41 PM

                    1 Reply Last reply
                    0
                    • L Lost User

                      Once I travled 400 miles to interview with a startup. All went well untill I was handed a pencile and a white sheet of paper and asked to write a recursive function in C# to produce a Fabbinicc sequence. Needles to say I didn't get the job. Do these people know that recursive algroythms = spigetti code? Hey Interviewers here is an IQ test: Penciles are for drawing as code is to? :confused: Hmm, the only thing important about Fabbinicci numbers and programming is that 1^n + 2^n ... + x^n has infinate solutions. And I'm not writting crypto software so it doesn't really matter. ~~Update~

                      _I have learned much from this thread. Thanks to all who gave me a hard time!
                      As a result of all my research and learning I created a 'Big O Analyzer'.

                      Hope it helps someone other than myself._

                      Big O Algroythm Analyzer for .NET[^] ~~Update~ 'The great advantage of recursion is that an infinite set of possible sentences, designs or other data can be defined, parsed or produced by a finite computer program.' Reference: Wikipedia on Recursion ~~Update~ If you tried the Big O tool and were disapointed that it did not find any Big O's at all, it's been updated. At infinity point = 1000 it's about 99.9991% acurate (good as gold). You might need to use .00002% brain power to figure out what the Big O is. ~TheArch :cool:

                      modified on Wednesday, July 22, 2009 4:56 AM

                      I Offline
                      I Offline
                      Ian_Sharpe
                      wrote on last edited by
                      #28

                      For me it would depend on the circumstances. This is an easy bit of code just a few lines long. Sitting here under no pressure I had it working in a couple of minutes. I would be happy to tackle it using pencil and paper. In a interview, if they said go sit in the corner for 10 minutes and see what you can come up with, I'd likely be OK. But if the interviewer sat close and watched every move of the pencil then I might go blank if I was not previously at ease. Then it becomes as much about self-confidence and ability to think under pressure as it does about coding.

                      V 1 Reply Last reply
                      0
                      • L Lost User

                        Once I travled 400 miles to interview with a startup. All went well untill I was handed a pencile and a white sheet of paper and asked to write a recursive function in C# to produce a Fabbinicc sequence. Needles to say I didn't get the job. Do these people know that recursive algroythms = spigetti code? Hey Interviewers here is an IQ test: Penciles are for drawing as code is to? :confused: Hmm, the only thing important about Fabbinicci numbers and programming is that 1^n + 2^n ... + x^n has infinate solutions. And I'm not writting crypto software so it doesn't really matter. ~~Update~

                        _I have learned much from this thread. Thanks to all who gave me a hard time!
                        As a result of all my research and learning I created a 'Big O Analyzer'.

                        Hope it helps someone other than myself._

                        Big O Algroythm Analyzer for .NET[^] ~~Update~ 'The great advantage of recursion is that an infinite set of possible sentences, designs or other data can be defined, parsed or produced by a finite computer program.' Reference: Wikipedia on Recursion ~~Update~ If you tried the Big O tool and were disapointed that it did not find any Big O's at all, it's been updated. At infinity point = 1000 it's about 99.9991% acurate (good as gold). You might need to use .00002% brain power to figure out what the Big O is. ~TheArch :cool:

                        modified on Wednesday, July 22, 2009 4:56 AM

                        V Offline
                        V Offline
                        Vozzie2
                        wrote on last edited by
                        #29

                        I wonder, if recursion = spogitta then how do you enumerate directories?

                        It feels good to learn and achieve

                        L 2 Replies Last reply
                        0
                        • L Lost User

                          Once I travled 400 miles to interview with a startup. All went well untill I was handed a pencile and a white sheet of paper and asked to write a recursive function in C# to produce a Fabbinicc sequence. Needles to say I didn't get the job. Do these people know that recursive algroythms = spigetti code? Hey Interviewers here is an IQ test: Penciles are for drawing as code is to? :confused: Hmm, the only thing important about Fabbinicci numbers and programming is that 1^n + 2^n ... + x^n has infinate solutions. And I'm not writting crypto software so it doesn't really matter. ~~Update~

                          _I have learned much from this thread. Thanks to all who gave me a hard time!
                          As a result of all my research and learning I created a 'Big O Analyzer'.

                          Hope it helps someone other than myself._

                          Big O Algroythm Analyzer for .NET[^] ~~Update~ 'The great advantage of recursion is that an infinite set of possible sentences, designs or other data can be defined, parsed or produced by a finite computer program.' Reference: Wikipedia on Recursion ~~Update~ If you tried the Big O tool and were disapointed that it did not find any Big O's at all, it's been updated. At infinity point = 1000 it's about 99.9991% acurate (good as gold). You might need to use .00002% brain power to figure out what the Big O is. ~TheArch :cool:

                          modified on Wednesday, July 22, 2009 4:56 AM

                          _ Offline
                          _ Offline
                          _Erik_
                          wrote on last edited by
                          #30

                          Well, I think this is really a great question for an interview, becouse you can get a lot of conclusions depending on the answer. If the answer was: int BadFib(int n) { if (n == 1 || n == 2) return 1; else return BadFib(n - 1) + BadFib(n - 2); } This answer means that the guy knows what recursion is, but he has no idea about algorithm complexity, and he does not mind performance at all. So, my next question would be: Can you do it better? But now, if somebody came up with this solution: public int GoodFib(int n) { return Fib(n, 1, 1); } private int Fib(int n, int n1, int n2) { if (n == 1 || n == 2) return 1; else if (n == 3) return n1 + n2; else return Fib(n - 1, n1 + n2, n1); } This means that the guy knows what recursion is, he knows what algorithm complexity is, and he is worried about the performance of his code. So, my next question would be: When would yo be able to start with the job? I guess the interviewer just wanted to know how skilled you were about programming.

                          modified on Tuesday, July 7, 2009 11:30 AM

                          P L 2 Replies Last reply
                          0
                          • _ _Erik_

                            Well, I think this is really a great question for an interview, becouse you can get a lot of conclusions depending on the answer. If the answer was: int BadFib(int n) { if (n == 1 || n == 2) return 1; else return BadFib(n - 1) + BadFib(n - 2); } This answer means that the guy knows what recursion is, but he has no idea about algorithm complexity, and he does not mind performance at all. So, my next question would be: Can you do it better? But now, if somebody came up with this solution: public int GoodFib(int n) { return Fib(n, 1, 1); } private int Fib(int n, int n1, int n2) { if (n == 1 || n == 2) return 1; else if (n == 3) return n1 + n2; else return Fib(n - 1, n1 + n2, n1); } This means that the guy knows what recursion is, he knows what algorithm complexity is, and he is worried about the performance of his code. So, my next question would be: When would yo be able to start with the job? I guess the interviewer just wanted to know how skilled you were about programming.

                            modified on Tuesday, July 7, 2009 11:30 AM

                            P Offline
                            P Offline
                            Paulo Zemek
                            wrote on last edited by
                            #31

                            In one of my interviews, I was asked to code a function to convert an string (char *, it was as C interview) into an int. Considering that there are already a lot of functions that do it already, it looks stupid. Considering it was an way to know if I know how to solve problems, I did it. Later, the interviewer was surprised, because I was the only one of the candidates to answer such question.

                            D _ L 3 Replies Last reply
                            0
                            • P Paulo Zemek

                              In one of my interviews, I was asked to code a function to convert an string (char *, it was as C interview) into an int. Considering that there are already a lot of functions that do it already, it looks stupid. Considering it was an way to know if I know how to solve problems, I did it. Later, the interviewer was surprised, because I was the only one of the candidates to answer such question.

                              D Offline
                              D Offline
                              Dan Neely
                              wrote on last edited by
                              #32

                              Paulo Zemek wrote:

                              Later, the interviewer was surprised, because I was the only one of the candidates to answer such question.

                              That's sad. I wrote the pascal equivalent after roughly half a year of programming in high school, and then wrote the equivalent to convert a string into a float. :(( I was writing custom numeric input handlers that rejected garbage keystrokes; offhand I don't recall why I didn't want to use the standard library validation/conversion functions.

                              The European Way of War: Blow your own continent up. The American Way of War: Go over and help them.

                              1 Reply Last reply
                              0
                              • V Vozzie2

                                I wonder, if recursion = spogitta then how do you enumerate directories?

                                It feels good to learn and achieve

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #33

                                I would use LINQ to Objects:

                                using System;
                                using System.IO;
                                using System.Collections.Generic;
                                using System.Linq;
                                using System.Text;

                                namespace Linq
                                {
                                class Program
                                {
                                static void Main(string[] args)
                                {
                                ListFiles(new DirectoryInfo("c:\\"));
                                }

                                    static void ListFiles(DirectoryInfo dir)
                                    {
                                        var Directories = from dirs in dir.GetDirectories()
                                                          orderby dirs.FullName
                                                          select dirs;
                                
                                        foreach(DirectoryInfo directory in Directories)
                                        {
                                            Console.WriteLine("Directory: <" + directory.FullName + "> contains the following files:");
                                
                                            var Files = from file in directory.GetFiles()
                                                        orderby file.FullName
                                                        select file;
                                
                                            foreach (FileInfo file in Files)
                                            {
                                                Console.WriteLine("---" + file.FullName);
                                            }
                                        }
                                    }
                                }
                                

                                }

                                If the system had multi core I would use PLINQ to Objects...

                                modified on Saturday, July 18, 2009 11:12 AM

                                S 1 Reply Last reply
                                0
                                • V Vozzie2

                                  I wonder, if recursion = spogitta then how do you enumerate directories?

                                  It feels good to learn and achieve

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #34

                                  If you just want the directories:

                                      static void ListDirectories(DirectoryInfo dir)
                                      {
                                          var BaseDir = from dirs in dir.GetDirectories()
                                                               orderby dirs.FullName
                                                               select dirs;
                                  
                                          foreach (DirectoryInfo thisDir in BaseDir)
                                          {
                                              var TheDirectory = from dirs in thisDir.GetDirectories("\*", SearchOption.AllDirectories)
                                                                 orderby dirs.FullName
                                                                 select dirs;
                                  
                                              Console.WriteLine("Directory: <" + thisDir.FullName + "> contains the following directories:");
                                  
                                              foreach (DirectoryInfo directory in TheDirectory)
                                              {
                                                  Console.WriteLine("  --\[" + directory.FullName + "\]");
                                              }
                                          }
                                      }
                                  

                                  modified on Saturday, July 18, 2009 11:14 AM

                                  L 1 Reply Last reply
                                  0
                                  • L Lost User

                                    If you just want the directories:

                                        static void ListDirectories(DirectoryInfo dir)
                                        {
                                            var BaseDir = from dirs in dir.GetDirectories()
                                                                 orderby dirs.FullName
                                                                 select dirs;
                                    
                                            foreach (DirectoryInfo thisDir in BaseDir)
                                            {
                                                var TheDirectory = from dirs in thisDir.GetDirectories("\*", SearchOption.AllDirectories)
                                                                   orderby dirs.FullName
                                                                   select dirs;
                                    
                                                Console.WriteLine("Directory: <" + thisDir.FullName + "> contains the following directories:");
                                    
                                                foreach (DirectoryInfo directory in TheDirectory)
                                                {
                                                    Console.WriteLine("  --\[" + directory.FullName + "\]");
                                                }
                                            }
                                        }
                                    

                                    modified on Saturday, July 18, 2009 11:14 AM

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #35

                                    Okay make it better:

                                        static void ListDirectories(DirectoryInfo dir)
                                        {
                                            var BaseDir = from dirs in dir.GetDirectories()
                                                          orderby dirs.FullName
                                                          select dirs;
                                            
                                            Thread.BeginCriticalRegion();
                                            foreach (DirectoryInfo thisDir in BaseDir)
                                            {
                                                try
                                                {
                                    
                                                    var TheDirectory = from dirs in thisDir.GetDirectories("\*", SearchOption.AllDirectories)
                                                                       orderby dirs.FullName
                                                                       select dirs;
                                    
                                                    Console.WriteLine("Directory: <" + thisDir.FullName + "> contains the following directories:");
                                    
                                                    foreach (DirectoryInfo directory in TheDirectory)
                                                    {
                                                        Console.WriteLine("  --\[" + directory.FullName + "\]");
                                                    }
                                                }
                                                catch (AccessViolationException ave)
                                                {
                                                    Console.WriteLine("Access Violation for: \[" + thisDir.FullName + "\] ave:" + ave.Message);
                                                }
                                                catch (UnauthorizedAccessException uave)
                                                {
                                                    Console.WriteLine("Unathorized Access Violation for: \[" + thisDir.FullName + "\] uave:" + uave.Message);
                                                }
                                            }
                                            Thread.EndCriticalRegion();
                                        }
                                    
                                    V C 2 Replies Last reply
                                    0
                                    • _ _Erik_

                                      Well, I think this is really a great question for an interview, becouse you can get a lot of conclusions depending on the answer. If the answer was: int BadFib(int n) { if (n == 1 || n == 2) return 1; else return BadFib(n - 1) + BadFib(n - 2); } This answer means that the guy knows what recursion is, but he has no idea about algorithm complexity, and he does not mind performance at all. So, my next question would be: Can you do it better? But now, if somebody came up with this solution: public int GoodFib(int n) { return Fib(n, 1, 1); } private int Fib(int n, int n1, int n2) { if (n == 1 || n == 2) return 1; else if (n == 3) return n1 + n2; else return Fib(n - 1, n1 + n2, n1); } This means that the guy knows what recursion is, he knows what algorithm complexity is, and he is worried about the performance of his code. So, my next question would be: When would yo be able to start with the job? I guess the interviewer just wanted to know how skilled you were about programming.

                                      modified on Tuesday, July 7, 2009 11:30 AM

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #36

                                      Okay after much testing I have a non recursive version:

                                      using System;

                                      namespace Test
                                      {
                                      class fib
                                      {
                                      double n = 0;
                                      public double next
                                      {
                                      get
                                      {
                                      return n;
                                      }

                                              set
                                              {
                                                  n = Math.Round(((Math.Pow(fib.golden(),value)) - Math.Pow((1-fib.golden()),value)) / Math.Sqrt(5));
                                              }
                                          }
                                      
                                          private static double golden()
                                          {
                                              return (1 + Math.Sqrt(5)) / 2;
                                      
                                          }
                                      }
                                      

                                      }

                                          public static double MyFib(int n)
                                          {
                                              fib f = new fib();
                                              f.next = n;
                                              return f.next;
                                          }
                                      

                                      Like I said, 'recursive algroythms are a bad idea.' GoodFib(6000) -1142292160 MyFib(6000) Infinity GoodFib(1000) 1556111435 MyFib(1000) 4.3466557686938915E+208 If you change GoodFib to return a double: GoodFib(6000) Infinity GoodFib(1000) 4.3466557686937428E+208 MyFib looses precision (I think):

                                          public static double NotEqual()
                                          {
                                              double count;
                                              double result1 = 0;
                                              double result2 = 0;
                                              for (count = 1;; count++)
                                              {
                                                  result1 = GoodFib(count);
                                                  result2 = MyFib(count);
                                                  if (result1 != Math.Floor(result2))
                                                      break;
                                              }
                                              Console.WriteLine("Result1: \[" + result1.ToString() + "\]");
                                              Console.WriteLine("Result2: \[" + result2.ToString() + "\]");
                                              return count;
                                          }
                                      

                                      NotEqual() 71.0 Result1: [308061521170129] Result2: [308061521170130] ?!?! Not really sure which is correct. The online sources say GoodFib is right. If I was really to do something like this I would use Math Lab C# extensions. Guarinteed precision.

                                      modified on Wednesday, July 8, 2009 2:42 AM

                                      L _ 2 Replies Last reply
                                      0
                                      • L Lost User

                                        Okay after much testing I have a non recursive version:

                                        using System;

                                        namespace Test
                                        {
                                        class fib
                                        {
                                        double n = 0;
                                        public double next
                                        {
                                        get
                                        {
                                        return n;
                                        }

                                                set
                                                {
                                                    n = Math.Round(((Math.Pow(fib.golden(),value)) - Math.Pow((1-fib.golden()),value)) / Math.Sqrt(5));
                                                }
                                            }
                                        
                                            private static double golden()
                                            {
                                                return (1 + Math.Sqrt(5)) / 2;
                                        
                                            }
                                        }
                                        

                                        }

                                            public static double MyFib(int n)
                                            {
                                                fib f = new fib();
                                                f.next = n;
                                                return f.next;
                                            }
                                        

                                        Like I said, 'recursive algroythms are a bad idea.' GoodFib(6000) -1142292160 MyFib(6000) Infinity GoodFib(1000) 1556111435 MyFib(1000) 4.3466557686938915E+208 If you change GoodFib to return a double: GoodFib(6000) Infinity GoodFib(1000) 4.3466557686937428E+208 MyFib looses precision (I think):

                                            public static double NotEqual()
                                            {
                                                double count;
                                                double result1 = 0;
                                                double result2 = 0;
                                                for (count = 1;; count++)
                                                {
                                                    result1 = GoodFib(count);
                                                    result2 = MyFib(count);
                                                    if (result1 != Math.Floor(result2))
                                                        break;
                                                }
                                                Console.WriteLine("Result1: \[" + result1.ToString() + "\]");
                                                Console.WriteLine("Result2: \[" + result2.ToString() + "\]");
                                                return count;
                                            }
                                        

                                        NotEqual() 71.0 Result1: [308061521170129] Result2: [308061521170130] ?!?! Not really sure which is correct. The online sources say GoodFib is right. If I was really to do something like this I would use Math Lab C# extensions. Guarinteed precision.

                                        modified on Wednesday, July 8, 2009 2:42 AM

                                        L Offline
                                        L Offline
                                        Lost User
                                        wrote on last edited by
                                        #37

                                        'Maybe the SS uses this formula...' :laugh:

                                        1 Reply Last reply
                                        0
                                        • L Lost User

                                          Okay make it better:

                                              static void ListDirectories(DirectoryInfo dir)
                                              {
                                                  var BaseDir = from dirs in dir.GetDirectories()
                                                                orderby dirs.FullName
                                                                select dirs;
                                                  
                                                  Thread.BeginCriticalRegion();
                                                  foreach (DirectoryInfo thisDir in BaseDir)
                                                  {
                                                      try
                                                      {
                                          
                                                          var TheDirectory = from dirs in thisDir.GetDirectories("\*", SearchOption.AllDirectories)
                                                                             orderby dirs.FullName
                                                                             select dirs;
                                          
                                                          Console.WriteLine("Directory: <" + thisDir.FullName + "> contains the following directories:");
                                          
                                                          foreach (DirectoryInfo directory in TheDirectory)
                                                          {
                                                              Console.WriteLine("  --\[" + directory.FullName + "\]");
                                                          }
                                                      }
                                                      catch (AccessViolationException ave)
                                                      {
                                                          Console.WriteLine("Access Violation for: \[" + thisDir.FullName + "\] ave:" + ave.Message);
                                                      }
                                                      catch (UnauthorizedAccessException uave)
                                                      {
                                                          Console.WriteLine("Unathorized Access Violation for: \[" + thisDir.FullName + "\] uave:" + uave.Message);
                                                      }
                                                  }
                                                  Thread.EndCriticalRegion();
                                              }
                                          
                                          V Offline
                                          V Offline
                                          Vozzie2
                                          wrote on last edited by
                                          #38

                                          Very nice, But i have visual studio 2003, :laugh: I always liked the elegance of recursion... I never understood why i learned this technique on fobenuca and not on directories,... That was the point i wanted to make . X|

                                          It feels good to learn and achieve

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups