Ebay Interview Question
Software DevelopersTeam: OpenStack
Country: United States
Interview Type: In-Person
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
using namespace std;
// You won't need this
typedef char TCHAR;
vector<string> split(TCHAR* str, TCHAR delimiter)
{
const string data(str);
vector<string> elements;
string element;
for(int i = 0; i < data.size(); ++i)
{
if (data[i] == delimiter)
{
elements.push_back(element);
element.clear();
}
else
element += data[i];
}
elements.push_back(element);
return elements;
}
bool toInt(const string& str, int* result)
{
if (str.find_first_not_of("0123456789") != string::npos)
return false;
stringstream stream(str);
stream >> *result; // Should probably check the return value here
return true;
}
bool validate(TCHAR* ip)
{
const static TCHAR delimiter = '.';
const vector<string> parts = split(ip, delimiter);
if (parts.size() != 4)
return false;
for(int i = 0; i < parts.size(); ++i)
{
int part;
if (!toInt(parts[i], &part))
return false;
if (part < 0 || part > 255)
return false;
}
return true;
}
int main()
{
cout << validate("13.23.12.221") << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
using namespace std;
// You won't need this
typedef char TCHAR;
vector<string> split(TCHAR* str, TCHAR delimiter)
{
const string data(str);
vector<string> elements;
string element;
for(int i = 0; i < data.size(); ++i)
{
if (data[i] == delimiter)
{
elements.push_back(element);
element.clear();
}
else
element += data[i];
}
elements.push_back(element);
return elements;
}
bool toInt(const string& str, int* result)
{
if (str.find_first_not_of("0123456789") != string::npos)
return false;
stringstream stream(str);
stream >> *result; // Should probably check the return value here
return true;
}
bool validate(TCHAR* ip)
{
const static TCHAR delimiter = '.';
const vector<string> parts = split(ip, delimiter);
if (parts.size() != 4)
return false;
for(int i = 0; i < parts.size(); ++i)
{
int part;
if (!toInt(parts[i], &part))
return false;
if (part < 0 || part > 255)
return false;
}
return true;
}
int main()
{
cout << validate("13.23.12.221") << endl;
return 0;
}
Two implementations in C#
public static Boolean Validate(String ipAddress)
{
String[] parts = ipAddress.Split('.');
if (parts.Length != 4) return false;
foreach(String s in parts)
{
Byte part;
Boolean valid = Byte.TryParse(s, out part);
if (valid == false) return false;
}
return true;
}
public static Boolean Validate2(String ipAddress)
{
if (ipAddress == null) return false;
Int32 partInt = 0;
Int32 partCount = 0;
for (int i = 0; i < ipAddress.Length; i++)
{
byte b = (Byte) (ipAddress[i] - '0');
if ( b >= 0 && b <= 9)
{
partInt = partInt * 10 + b;
if (partInt > 255)
{
return false;
}
}
else if (ipAddress[i] == '.')
{
if ((partCount > 3) //only three allowed
|| (i == 0)) //cannot be the first character
{
return false;
}
partCount++;
partInt = 0;
}
else //any other character
{
return false;
}
}
return true;
}
private static final Pattern PATTERN = Pattern.compile(
- krishnama chary October 17, 2015"^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
public static boolean validate(final String ip) {
return PATTERN.matcher(ip).matches();
}