Ebay Interview Question for Software Developers


Team: OpenStack
Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

private static final Pattern PATTERN = Pattern.compile(
"^(([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();
}

- krishnama chary October 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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;
}

- #Robo October 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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;
}

- #Robo October 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am sure you are not looking for the answer. Simple answer would be to use a Regex or split based on . and compare each number.

- Abhi October 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- IC October 23, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More