Test Automation Application Exercises - 2017

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Test Automation Application Exercises

2017.04.20

Instructions
Here are a couple of coding problems we would like you to complete. Please answer with the language you
feel most comfortable using.

Please only take about 60 minutes to complete the questions below.

Please add your answers to this document and email it back to us in .DOCX format.

Questions
1.a) Write classes to find any pairs of numbers in a sequence that add up to 10. Example:

Sample input: 1, 8, 2, 3, 5, 7
Sample output: (8,2), (3, 7)

Your code must include the following:

A class to encapsulate a pair of numbers.


Feel free to add features to this class that would be useful for this exercise.
A utility class that finds the pairs. This must have:
A constructor.
A method or function that takes a list of numbers (input).
A method or function that returns a list of pairs of numbers (output).
A method or function that can use the list of pairs and output formatted answer strings such as:
(8, 2), (3 7).
Comments in your code that state any assumptions you are making.

Answer:
_________________________

import java.util.ArrayList;
import java.util.List;

public class SumTheNumbers {


List<Integer> inputList;
List<Object> outputList = new ArrayList<Object>();

// default constructor
public SumTheNumbers() {

public List<Integer> readingTheInput(List<Integer> inputList) {


this.inputList = inputList;
return inputList;
}

public List<Object> returnPairs() {


int n = inputList.size();
// Iterating the list with two pointers
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int sum = inputList.get(i) + inputList.get(j);
if (sum == 10) {
outputList.add("(" + inputList.get(i) + "," + inputList.get(j) + ")");
}
}
}
return outputList;

public String toString() {


String string = "";
int size = inputList.size();
if (size > 2) {
for (int i = 0; i < outputList.size(); i++) {
if (i != outputList.size() - 1) {
string = string + outputList.get(i) + ",";
} else {
string = string + outputList.get(i);
}
}
if (string.equals("")) {
string = "There are no pair of numbers in a sequence that add up to 10";
}
} else {
string = "Cann't make a pair if a list size is less than 2";
}
return string;
}

}
_________________________

1.b) Write 5 sample test inputs and corresponding output pairs for your function. Bonus: Write your answer
as a unit test.

Answer:
_________________________

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.testng.annotations.Test;

import junit.framework.Assert;
import junit.framework.TestCase;

public class TestCases extends TestCase {


static List<Integer> list;

// we can generalize the receiving input using scanner or Buffered Reader as


// well
// give valid input in below method
@Test(priority = 1)
public void testTogenerateValidPairs() {
SumTheNumbers sumTheNumbers = new SumTheNumbers();
list = new ArrayList<Integer>(Arrays.asList(1, 8, 2, 3, 5, 7));
sumTheNumbers.readingTheInput(list);
sumTheNumbers.returnPairs();
Assert.assertEquals("(8,2),(3,7)", sumTheNumbers.toString());

@Test(priority = 2)
public void testLessTwoSize() {
SumTheNumbers sumTheNumbers = new SumTheNumbers();
list = new ArrayList<Integer>(Arrays.asList(1));
sumTheNumbers.readingTheInput(list);
sumTheNumbers.returnPairs();
// System.out.println(sumTheNumbers.toString(li));
Assert.assertEquals("Cann't make a pair if a list size is less than 2",
sumTheNumbers.toString());
}

@Test(priority = 3)
public void testToUnableToGeneratePairs() {
SumTheNumbers sumTheNumbers = new SumTheNumbers();
list = new ArrayList<Integer>(Arrays.asList(1, 2, 3));
sumTheNumbers.readingTheInput(list);
sumTheNumbers.returnPairs();
Assert.assertEquals("There are no pair of numbers in a sequence that add
up to 10", sumTheNumbers.toString());
}

// Test with whether output returns as expected with negative numbers


@Test(priority = 4)
public void testToAcceptNegativeNumbers() {
SumTheNumbers sumTheNumbers = new SumTheNumbers();
list = new ArrayList<Integer>(Arrays.asList(1, 12, -2));
sumTheNumbers.readingTheInput(list);
sumTheNumbers.returnPairs();
Assert.assertEquals("(12,-2)", sumTheNumbers.toString());
}

@Test(priority = 5)
public void testWithLargeValues() {
SumTheNumbers sumTheNumbers = new SumTheNumbers();
list = new ArrayList<Integer>(Arrays.asList(1, 5, 60, 34, 11, 5, 4, 6,
10, 2, 8, 9, 1));
sumTheNumbers.readingTheInput(list);
sumTheNumbers.returnPairs();
Assert.assertEquals("(1,9),(5,5),(4,6),(2,8),(9,1)",
sumTheNumbers.toString());
}

}
_________________________
2.a) Write a short function to determine if a string is a substring of a longer one. (Note: Don't use a system or
library built-in function like variable.substr() or variable.contains(). Instead, write the code that would
implement the .substr() or .contains() method.) Example:

Sample input: "This is a string", "is a"


Sample output: "Yes"

Answer:
_________________________

public class DetermineSubString {


public static String isSubString(String str, String sub) {
int i = 0, j = 0;
String string = "", output = "";
while (sub.length() <= str.length() && j < sub.length()) {
if (str.charAt(i) == sub.charAt(j)) {
string += sub.charAt(j);
j++;
}
i++;
if (i > str.length() - 1) {
break;
}
}
if (string.equals(sub)) {
output = "Yes";
} else {
output = "No";
}
return output;
}
// The above is the one way of solving problem and We can use regex to find
// whether particular sub string is exist or not using Pattern and matcher
// classes
}
_________________________

2.b) Write 5 sample test inputs and corresponding output for your function.

Answer:
_________________________

import org.testng.annotations.Test;

import junit.framework.Assert;
import junit.framework.TestCase;

@SuppressWarnings("deprecation")
public class TestCases extends TestCase {

// we can generalize the receiving input using scanner or Buffered Reader as


// well
// give valid input in below method
@Test(priority = 1)
public void testValidSubStr() {
Assert.assertEquals("Yes", DetermineSubString.isSubString("This is a
String", "is a"));
}

@Test(priority = 2)
public void testSubStrWithCaseSensitive() {
Assert.assertEquals("No", DetermineSubString.isSubString("This is a
String", "Is a"));
}

@Test(priority = 3)
public void testSubStrWithMorelength() {
Assert.assertEquals("No", DetermineSubString.isSubString("Problem
Solved", "Problem Solved but"));
}

@Test(priority = 4)
public void testSubStrWithSpecialChars() {
Assert.assertEquals("Yes", DetermineSubString.isSubString("&$$Synopsys",
"&$$"));
}

@Test(priority = 5)
public void testSubStrIsInvalid() {
Assert.assertEquals("No", DetermineSubString.isSubString("Synopsys Smart
Secure Everything", "software"));
}
}
_________________________

3) How long did it take you to finish these exercises?

Answer:

It took 55 min for me to solve the given problems.

You might also like