Google Interview Question
Software Engineer InternsCountry: United States
"remove" should remove the last returend value.
so:
Iterator<String> iter = repeat("Test repeater", 7);
while (iter.hasNext()) {
System.out.println(iter.next());
iter.remove();
}
should print 7 values, I think you code doesn't. should be:
@Override
public void remove() {
// Simply do nothing
}
// An example in golang using a closure.
// It simply decrements the counter on every call.
// Returns nil when the counter is zero.
// A use case is shown in the main function.
package main
import (
"fmt"
)
// Returns an iterator of obj
func Iterator(obj interface{}, n int) (func() interface{}) {
f := func() interface{} {
if n > 0 {
n--
return obj
} else {
return nil
}
}
return f
}
type Person struct {
Name string
Age int
}
func main() {
p := Person{Name:"Bruce", Age:45}
g := Iterator(p, 5)
for v := g(); v != nil; v = g() {
fmt.Println(v.(Person))
}
}
/*
Output:
{Bruce 45}
{Bruce 45}
{Bruce 45}
{Bruce 45}
{Bruce 45}
*/
public class ObjIterator<Object> implements Iterator<Object>{
private ArrayList<Object> ls;
int pos=0;
ObjIterator(int count,Object toRepeat)throws NullPointerException, IllegalArgumentException
{
if(toRepeat==null)
{
throw new NullPointerException();
}
if(count<0)
{
throw new IllegalArgumentException
}
ls=new ArrayList<Object>(count);
for(int i=0;i<count;i++)
{
ls.add(toRepeat);
}
}
public boolean hasNext()
{
return (pos<ls.size());
}
public Object next()
{
return ls.get(pos++);
}
public Object remove()
{
return ls.remove(pos);
}
}
public ObjectIterator<Object> repeat(Object e, int n)
{
ObjectIterator it=new ObjectIterator(e,n);
return it;
}
c++, implementation
#include <iostream>
#include <string>
using namespace std;
template <typename T>
struct Iterator {
T obj;
int cnt;
Iterator(T e, int n) : obj(e), cnt(n) {}
bool operator() (T& ret) {
if (cnt > 0) {
cnt--;
ret = obj;
return true;
}
return false;
}
};
template <typename T>
Iterator<T> repeat(T e, int n) {
Iterator<T> iterator(e, n);
return iterator;
}
struct Object {
int value;
string name;
};
int main(int argc, char* argv[]) {
int n = 3;
Iterator<int> integerIterator = repeat(n, 5);
n = -1;
while (integerIterator(n)) {
cout << n << " ";
}
cout << "\n";
Object obj;
obj.value = 7;
obj.name = "ABC";
Iterator<Object> objectIterator = repeat(obj, 3);
obj.value = -1;
obj.name = "";
while (objectIterator(obj)) {
cout << "(" << obj.value << ", " << obj.name << ") ";
}
cout << "\n";
return 0;
}
how come this is creating copy of object?
bool operator() (T& ret) {
if (cnt > 0) {
cnt--;
ret = obj;
return true;
}
return false;
}
public <T> Iterator<T> repeat(final T obj, final int n) {
return new AbstractList<T>() {
public T get(int index) {
return obj;
}
public int size() {
return n;
}
}.iterator();
}
public class Repeat {
public static <T> Iterator<T> repeat(T object, int n) {
return new MyIterator(object, n);
}
private static class MyIterator<T> implements Iterator<T> {
final T object;
int n;
public MyIterator(T object, int n) {
this.object = object;
this.n = n;
}
@Override
public boolean hasNext() {
return n > 0 ? true : false;
}
@Override
public T next() {
if (!hasNext()) throw new IllegalStateException("No element to iterate over");
n--;
return object;
}
}
}
c# implementation, quite straightforward.
using System;
namespace IteratorProducingElementsNTimes {
public interface IIterator<T> {
void First();
void Next();
bool IsDone();
T CurrentItem();
}
public class Iterator<T> : IIterator<T> {
private readonly T _obj;
private readonly int _total;
private int _current = 0;
public Iterator( T obj, int total ) {
_obj = obj;
_total = total;
}
public void First() {
_current = 0;
}
public void Next() {
_current++;
}
public bool IsDone() {
return _current > _total - 1;
}
public T CurrentItem() {
return _obj;
}
}
public struct Person {
public string Name;
public string Surname;
public override string ToString() {
return $"{Name}, {Surname}";
}
}
public static class Program {
private static Iterator<T> _repeate<T>( T obj, int n ) {
return new Iterator<T>(obj, n);
}
static void Main(string[] args) {
var iter = _repeate( new Person { Name = "John", Surname = "Coombes" }, 3 );
iter.First();
while ( !iter.IsDone() ) {
Console.WriteLine( iter.CurrentItem() );
iter.Next();
}
Console.ReadLine();
}
}
}
- artak.a.petrosyan November 13, 2015