Prime Number Check and Fibonacci Series Generation

viaGlassdoor

Problem: Two basic programming questions: (1) write a function to check whether a given number is prime; (2) write a function to generate the first N terms of the Fibonacci series.

Constraints: Standard integer inputs.

Example: isPrime(17) -> true; fibonacci(6) -> [0,1,1,2,3,5].

Approach: For prime check, test divisibility only up to sqrt(n) (skipping even numbers after checking 2) for O(sqrt(n)) time. For Fibonacci, use an iterative approach maintaining the last two values and updating them in a loop for O(n) time and O(1) space (avoiding the exponential blowup of naive recursion).

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account