Symantec Interview Question
Senior Software Development EngineersCountry: India
Interview Type: Phone Interview
import java.util.*;
public class Pract {
public static void main(String[] args) {
int[] ar=new int[3];
ArrayList<Integer> arr=new ArrayList<Integer>(3);
ar[0]=63;
ar[1]=43;
ar[2]=33;
arr.add(5);
arr.add(6);
arr.add(3);
Iterator it=arr.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
for(int i:ar)
{
System.out.println(i);
}
}
}
File Array.h
#ifndef __ARRAY_H_
#define __ARRAY_H_
#include "ArrayIteartor.h"
template <class T, int SIZE>
class Array
{
typedef ArrayIterator<T> iterator;
public:
Array(int size=SIZE):m_size(size)
{
m_data = new (nothrow) T[m_size];
if(!m_data)
{
cout<<"Memory allocation error."<<endl;
}
}
Array(const Array<T>& rhs)
{
if (rhs.m_size)
{
m_data = new (nothrow) T[rhs.m_size];
if (!m_data)
{
cout<<"Memory allocation error."<<endl;
}
iterator iter1 = rhs.begin();
int i=0;
while(iter1 != rhs.end())
{
m_data[i] = *iter1;
++iter1;
}
}
}
iterator begin()
{
iterator it(&m_data[0]);
return it;
}
iterator end()
{
iterator it(&m_data[m_size-1]);
return it;
}
private:
int* m_data;
int m_size;
};
#endif
File ArrayIterator.h
#ifndef __ARRAY_ITERATOR_H_
#define __ARRAY_ITERATOR_H_
template <class T>
class ArrayIterator;
template <class T>
bool operator!=(const ArrayIteartor<T>& first, const ArrayIterator<T>& second);
template <class T>
bool operator==(const ArrayIteartor<T>& first, const ArrayIterator<T>& second);
template <class T>
class ArrayIterator
{
public:
ArrayIterator(T* ptr=NULL):m_ptr(ptr)
{
}
ArrayIterator(const ArrayIterator& x):m_ptr(x.m_ptr)
{
}
ArrayIterator& operator++()
{
++m_ptr;
}
ArrayIterator operator++(int)
{
ArrayIterator<T> temp(*this);
++m_ptr;
return temp;
}
ArrayIterator& operator--()
{
--m_ptr;
}
ArrayIterator operator--(int)
{
ArrayIterator<T> temp(*this);
--m_ptr;
return temp;
}
T* operator->()
{
return m_ptr;
}
T& operator*()
{
return *m_ptr;
}
friend bool operator!= <>(const ArrayIterator<T>& first, const ArrayIterator<T>& second);
friend bool operator== <>(const ArrayIterator<T>& first, const ArrayIterator<T>& second);
private:
T* m_ptr;
};
template <class T>
bool operator==(const ArrayIterator<T>& first, const ArrayIterator<T>& second)
{
return (first.m_ptr == second.m_ptr);
}
template <class T>
bool operator!=(const ArrayIterator<T>& first, const ArrayIterator<T>& second)
{
return (first.m_ptr != second.m_ptr);
}
#endif
- iterator for int array April 05, 2013