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

    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
                        • 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

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

                          I think you have not got the point. What I mean is that when an intervewer asks you to solve a problem within some kind of restrictions, as "recursive way" to do "whatever", the really important thing here is not the problem itself, but the way you can solve it applying those restrictions. That is why I have showed two possible ways to solve Fibonacci sequence in a recursive way. The first one is horrible because it has an exponential complexity, while the second one is linear. Sure, using golden proportion in this concrete case comes with a constant complexity algorithm, but you have not folowed the instructions. So, the problem here is not finding a Fibonacci number. The problem is to do it in a recursive way with a good performance. If the interviewer had asked you, for example, to find an iterative way to solve Hanoi's Tower problem, golden proportion would not be there to help you. On the other hand, when you say recursion is bad, sorry, but you are absolutely wrong. Many abstract data types are recursive by definition, like trees or graphs. Just implement a non recursive way to find a file within a tree of folders. When you get it, do it recursive. When finished, analyze both of them, how they work and what they do under the covers. When you get finished, I think you will really appreciate the real value of recursion.

                          L 3 Replies Last reply
                          0
                          • I Ian_Sharpe

                            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 Offline
                            V Offline
                            Viral Upadhyay
                            wrote on last edited by
                            #40

                            i agree with you. Some time interviewer make matter worst. I also cant code when some one watching me. Its really confuse me. :^)

                            Viral My Site Tips & Tracks

                            1 Reply 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.

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

                              That is exactly the point I wanted to reach.

                              1 Reply Last reply
                              0
                              • _ _Erik_

                                I think you have not got the point. What I mean is that when an intervewer asks you to solve a problem within some kind of restrictions, as "recursive way" to do "whatever", the really important thing here is not the problem itself, but the way you can solve it applying those restrictions. That is why I have showed two possible ways to solve Fibonacci sequence in a recursive way. The first one is horrible because it has an exponential complexity, while the second one is linear. Sure, using golden proportion in this concrete case comes with a constant complexity algorithm, but you have not folowed the instructions. So, the problem here is not finding a Fibonacci number. The problem is to do it in a recursive way with a good performance. If the interviewer had asked you, for example, to find an iterative way to solve Hanoi's Tower problem, golden proportion would not be there to help you. On the other hand, when you say recursion is bad, sorry, but you are absolutely wrong. Many abstract data types are recursive by definition, like trees or graphs. Just implement a non recursive way to find a file within a tree of folders. When you get it, do it recursive. When finished, analyze both of them, how they work and what they do under the covers. When you get finished, I think you will really appreciate the real value of recursion.

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

                                Yes I understand your point is valid. However, if the candidate tells the interviewer he understands recrusion and gives the text book definition and tells the interviewer that the problem is better solved using non-recrusive methods... If the interviewer does not trust the candidate it speaks bad for the company they represent. Why are recursive algrythms bad: IEEE Abstract - Recursive algorithms in computer science courses: Fibonacci numbersand binomial coefficients[^] I did some BigO testing of the various algorithms, here are the results: BadFib(40) LoopCount: [204668309] - SplitTimeMicro: [15042441.7069767] GoodFib(40) LoopCount: [38] - SplitTimeMicro: [1171.09856140934] MyFib(40) LoopCount: [1] - SplitTimeMicro: [6122.8452219486] - 'can be improved using the correct math lib.' LinerFib(40) LoopCount: [41] - SplitTimeMicro: [652.317543151434] Liner Fib:

                                    public static double LinerFib(double n)
                                    {
                                        double previous = -1;
                                        double result = 1;
                                        double sum = 0;
                                        for (double i = 0; i <= n; ++i)
                                        {
                                            sum = result + previous;
                                            previous = result;
                                            result = sum;
                                        }
                                        return result;
                                    }
                                

                                'Most solutions to problems which are inherintly recrusive in nature can be solved using liner proofs...'

                                1 Reply Last reply
                                0
                                • _ _Erik_

                                  I think you have not got the point. What I mean is that when an intervewer asks you to solve a problem within some kind of restrictions, as "recursive way" to do "whatever", the really important thing here is not the problem itself, but the way you can solve it applying those restrictions. That is why I have showed two possible ways to solve Fibonacci sequence in a recursive way. The first one is horrible because it has an exponential complexity, while the second one is linear. Sure, using golden proportion in this concrete case comes with a constant complexity algorithm, but you have not folowed the instructions. So, the problem here is not finding a Fibonacci number. The problem is to do it in a recursive way with a good performance. If the interviewer had asked you, for example, to find an iterative way to solve Hanoi's Tower problem, golden proportion would not be there to help you. On the other hand, when you say recursion is bad, sorry, but you are absolutely wrong. Many abstract data types are recursive by definition, like trees or graphs. Just implement a non recursive way to find a file within a tree of folders. When you get it, do it recursive. When finished, analyze both of them, how they work and what they do under the covers. When you get finished, I think you will really appreciate the real value of recursion.

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

                                  _Erik_ wrote:

                                  The problem is to do it in a recursive way with a good performance. If the interviewer had asked you, for example, to find an iterative way to solve Hanoi's Tower problem, golden proportion would not be there to help you.

                                  This can be solved in linear time using a Hamiltonian Path: Linear-time algorithms for the Hamiltonian problems on distance-hereditary graphs [^]

                                  1 Reply Last reply
                                  0
                                  • _ _Erik_

                                    I think you have not got the point. What I mean is that when an intervewer asks you to solve a problem within some kind of restrictions, as "recursive way" to do "whatever", the really important thing here is not the problem itself, but the way you can solve it applying those restrictions. That is why I have showed two possible ways to solve Fibonacci sequence in a recursive way. The first one is horrible because it has an exponential complexity, while the second one is linear. Sure, using golden proportion in this concrete case comes with a constant complexity algorithm, but you have not folowed the instructions. So, the problem here is not finding a Fibonacci number. The problem is to do it in a recursive way with a good performance. If the interviewer had asked you, for example, to find an iterative way to solve Hanoi's Tower problem, golden proportion would not be there to help you. On the other hand, when you say recursion is bad, sorry, but you are absolutely wrong. Many abstract data types are recursive by definition, like trees or graphs. Just implement a non recursive way to find a file within a tree of folders. When you get it, do it recursive. When finished, analyze both of them, how they work and what they do under the covers. When you get finished, I think you will really appreciate the real value of recursion.

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

                                    Using infinite asymptotics... MyFib(1477) Loops: [1477] Steps: [Approx:25109] GoodFib(1477) Loops: [1088552] Steps: [6537220] LinearFib(1477) Loops: [1092980] Steps: [8752702] Even though Linerfib takes more steps, it out performs GoodFib in a clock test. In engineering we call this the 'Proof from the pudding...' LinerFib(1477) LoopCount: [1478] - SplitTimeMicro: [44.6984183744023] MyFib(1477) LoopCount: [1] - SplitTimeMicro: [48.8888950970026] GoodFib(1477) LoopCount: [1475] - SplitTimeMicro: [507.047683434626]

                                    modified on Wednesday, July 8, 2009 2:35 PM

                                    _ 1 Reply Last reply
                                    0
                                    • 0 0x3c0

                                      The Fibonacci sequence doesn't have that formula. It's the recurrence relationship Fn-1 + Fn-2, with seed values of F0 = 0 and F1 = 1. And it seemed to be a test of your capability to write recursive functions. You're right about one thing though - pencils and paper aren't a development environment. They might be useful for simplifying an algorithm, or brainstorming (oh, how I hate that word) ideas, but when testing how well somebody writes code, they need to be in as close to how they would develop while at the company as possible

                                      Between the idea And the reality Between the motion And the act Falls the Shadow

                                      L Offline
                                      L Offline
                                      Lutoslaw
                                      wrote on last edited by
                                      #45

                                      Computafreak wrote:

                                      The Fibonacci sequence doesn't have that formula.

                                      Well it has, but it's not a point of your post I suppose. I had a task on a math exam to derive a closed form expression of a given sequence defined by a linear recursion. It's quite easy when you know something about generating functions. That task killed me, though. ;)

                                      Greetings - Jacek

                                      1 Reply Last reply
                                      0
                                      • L Lost User

                                        Using infinite asymptotics... MyFib(1477) Loops: [1477] Steps: [Approx:25109] GoodFib(1477) Loops: [1088552] Steps: [6537220] LinearFib(1477) Loops: [1092980] Steps: [8752702] Even though Linerfib takes more steps, it out performs GoodFib in a clock test. In engineering we call this the 'Proof from the pudding...' LinerFib(1477) LoopCount: [1478] - SplitTimeMicro: [44.6984183744023] MyFib(1477) LoopCount: [1] - SplitTimeMicro: [48.8888950970026] GoodFib(1477) LoopCount: [1475] - SplitTimeMicro: [507.047683434626]

                                        modified on Wednesday, July 8, 2009 2:35 PM

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

                                        Ok, guy. Which part of "the really important thing here is not the problem itself, but the way you can solve it applying those restrictions" is the one you have not understood?

                                        L 1 Reply Last reply
                                        0
                                        • _ _Erik_

                                          Ok, guy. Which part of "the really important thing here is not the problem itself, but the way you can solve it applying those restrictions" is the one you have not understood?

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

                                          :~

                                          _Erik_ wrote:

                                          Ok, guy. Which part of "the really important thing here is not the problem itself, but the way you can solve it applying those restrictions" is the one you have not understood?

                                          I have all ready admited your point is valid! My problem with the interviewer was that I had expalined the problems with recrusion and how it causes bad things to happen like: heap pile up, stack overflow, huge thread stacks, and object over load due to the problem with not being able box objects for reuse. I also told the interviewer that I only had a basic understanding of the problem and that I knew it would be better solved with out using recursion. I did explain what recursion was and such. My biggest mistake was not asking the interviewer for a diffrent problem using the same constraints for which I completly undestood. We are human Eric, a knowlegable man learns from his own mistakes, a wise man learns from others. I was not trying to bash you in this thread, my honest appoligies if you though as much. My intent was to show how I arrived at my conclusions about the problem algroythm during the interview. Perhaps others reading this thread could gain wisdom from my mistake. ~TheArch :cool:

                                          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