r/csinterviewproblems • • Dec 18 '15

[DataStructure]Iterator with Peak

1 Upvotes

This isn't relevant to all but many languages.

An iterator normally has a "next()" method being the only way to access the elements.

However, when a next() is called, you cannot access the returned element again.

Design an iterator class with "peek()" method. Your constructor should take in a normal iterator. It should be able to handle big size data (hint: copying everything to a queue or list isn't.) and be reasonably efficient.


r/csinterviewproblems • • Dec 17 '15

[Recursion] Write a function to print the n'th Fibonacci number

3 Upvotes

The Fibonacci sequence is defined by the function:

f(n) = f(n-1) + f(n-2)
f(0) = 0
f(1) = 1

0, 1, 1, 2, 3, 5, 8...

Write a function to print the n'th number in this sequence.

Sample input: 4

Sample output: 3

Follow up: Can you do it without recursion?


r/csinterviewproblems • • Dec 17 '15

[Linked Lists] Write a function to get the k'th to last node in a linked list

2 Upvotes

Write a function that returns the k'th to last node in a singly linked list with no loops. Example input:

list = (a) -> (b) -> (c) -> (d)

k = 0: returns (d)

k = 1: returns (c)

etc.

Follow up 1: Can you do it without using another data structure (like an array)?

Follow up 2: Can you do it in < O(n)?