Skip to content

Instantly share code, notes, and snippets.

@hilfritz
Last active August 20, 2018 01:55
Show Gist options
  • Save hilfritz/1f81501c7d3770d80427d62d2cc712eb to your computer and use it in GitHub Desktop.
Save hilfritz/1f81501c7d3770d80427d62d2cc712eb to your computer and use it in GitHub Desktop.
java/android exam 1
1. Find the longest String in an array of Strings
//ANSWER:
public String getLongestStringInArray(String[] array){
int index = 0;
int elementLength = array[0].length();
int arraySize = array.length;
for(int i=1; i< arraySize; i++) {
int itemSize= array[i].length();
if(itemSize > elementLength) {
index = i; elementLength = itemSize;
}
}
return array[index];
}
//TO TEST:
String[] stringArray = {"a", "b", "abab", "c","edf"};
String longestString = getLongestStringInArray(stringArray); //ANSWER SHOULD BE "abab"
2)Write a recursive function to do 1+...+n = n*(n+1)/2 = sumAll( n )
//ANSWER:
public int sumAll(int n){
if (n==1)
return 1;
else
return sumAll(n-1)+n;
}
//TO TEST
System.out.println("sumAll(2) = 3; "+sumAll(2)+": ");
System.out.println("sumAll(3) = 6; "+sumAll(3)+": ");
System.out.println("sumAll(5) = 15; "+sumAll(5)+": ");
3)Given two sorted lists (or arrays) and a number k, create an algorithm to fetch the least k numbers of the two lists.
//ANSWER:
public int[] getLeastNumbers(int k, int[] array1, int[] array2){
int x = 0,y = 0,z = 0;
int[] retVal = new int[k];
while (x<k && x<array1.length && x<array2.length )
{
if (array1[y] <= array2[z])
{
retVal[x] = array1[y];
y++;
}
else
{
retVal[x] = array2[z];
z++;
}
x++;
}
while (x<k && x<array1.length) {
retVal[x]=array1[y];
x++;
y++;
}
while (x<k && x<array2.length) {
retVal[x]=array1[z];
x++;
z++;
}
return retVal;
}
//TO TEST:
int[] ar1 = new int[]{1, 2, 3, 4, 5, 6};
int[] ar2 = new int[]{1, 2, 2, 3, 4, 5};
String str = Arrays.toString(main.getLeastNumbers(6, ar1, ar2));
System.out.println("leastNumbers:"+str);
4)Write a Program to solve the problem below (not complete)
You have an Employee class |
class Employee{
String name;
Integer id,
Employee manager
}
Each employee has a manager and the manager of CEO is null. Find all direct and indirect reportees of a manager.
Eg. Say Employee e1 reports to CEO,
e2 and e3 reports to e1,
e4 and e5 reports to e2,
e6 reports to e3.
Then by giving e1 as input, output should be e2, e3, e4, e5 and e6.
class Employee{
String name;
int id;
Employee reportingOfficer;
public Employee(String name, int id, Employee reportingOfficer) {
this.name = name;
this.id = id;
this.reportingOfficer = reportingOfficer;
}
}
ArrayList<Employee> employees = new ArrayList<>();
Employee ceo = new Employee("ceo", 0, null);
Employee e1 = new Employee("employee1", 1, ceo);
Employee e2 = new Employee("employee2", 2, e1);
Employee e3 = new Employee("employee3", 3, e1);
Employee e4 = new Employee("employee4", 4, e2);
Employee e5 = new Employee("employee5", 5, e2);
Employee e6 = new Employee("employee6", 6, e3);
employees.add(ceo);
employees.add(e1);
employees.add(e2);
employees.add(e3);
employees.add(e4);
employees.add(e5);
employees.add(e6);
public ArrayList<Employee> getEmployeesUnderEmployee(Employee parent, ArrayList<Employee> list){
ArrayList<Employee> subordinates = new ArrayList<>();
int counter = 0;
int size = list.size();
for(int x = 0; x<size; x++){
Employee item = list.get(x);
if (item.reportingOfficer==parent){
subordinates.add(item);
continue;
}else{
}
}
return subordinates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment