1) Simple Encoded Array: Maya has stored few confidential numbers in an array (array of int). To ensure that others do not find the numbers easily, she has applied a simple encoding.
Encoding used: Each array element has been substituted with a value that is the sum of its original value and its succeeding element’s value.
i.e. arr[i] = arr[i] + arr[i+1]
e.g. value in arr[0] = original value of arr[0] + original value of arr[1]
Also note that value of last element i.e. arr[last index] remains unchanged.
Example:
If the original array is –
{2, 5, 1, 7, 9, 3}
The encoded array would be –
{7, 6, 8, 16, 12, 3}
Provided the encoded array, you are expected to find the –
a) First number (value in index 0) in the original array
b) Sum of all numbers in the original array
The prototype of the function is:
public static void findOriginalFirstAndSum(int[] input1);
where input1 is the encoded array.
The method is expected to –
- find the value of the first number of the original array and store it in the member output1 and
- find the sum of all numbers in the original array and store it in the member output2
Assumption(s):
- The array elements can be positive and/or negative numbers
Example 1:
Original array = {2, 5, 1, 7, 9, 3}
Encoded array = {7, 6, 8, 16, 12, 3}
First number in original array = 2
Sum of all numbers in original array = 27
NOTE: Only the “Encoded array” will be supplied to the function and it is expected to do the processing to find the expected result values.
Top comments (5)
class Validator {
// ID Validation
public static Boolean isValidEmployeeId(Long empId) {
String empIdString = String.valueOf(empId);
return empIdString.matches("[0-9]{6,7}");
}
// PhoneNo Validation
public static Boolean isValidEmployeePhoneNo(Long empPhoneNo) {
String phoneNoString = String.valueOf(empPhoneNo);
return phoneNoString.matches("[1-9]{1}[0-9]{9}") && !phoneNoString.matches("(.)\1+");
}
}
String[] parts = empEmail.split("@");
if (parts.length != 2) {
return false;
}
String part1 = parts[0];
String part2 = parts[1];
import java.util.Scanner;
class Main {
}
class Employee {
private Long empId;
private String empEmail;
private String empPassword;
private Long empPhoneNo;
private Integer age;
}
class Validator {
}
This question aims to test your knowledge of Regex and Java Library. Implement as per the given requirement.
Requirement:
MotoX, a leading MNC in India wants to vaccinate all its employees who are of age 18-44. So, to make the vaccination drive smooth they are creating a new portal in which all the details of its Employees will be taken and validated.
To save time MotoX needs to automate the new portal. As per the first iteration, a series of validations are required for the below components:
Employee ID
Employee Email
Employee Password
Employee Phone Number
Employee age
Instructions:
As per the above requirement, the Employee class which is a simple model class, and the Main class accepting all the above parameters with the proper function calls are already provided to you.
Implement the below methods in the Validator class to complete the validation process:
public static Boolean isValidEmployeeId(Long empId):
This method validates the empId.
empId should be a 6-digit or 7-digit positive integer.
If the above condition is satisfied, return true, otherwise, return false.
Example-
Valid: 123456, 789654, 8765324, 987654, 9876512.
Invalid: 12334, 78965, 89098765, -7098567, -897654.
public static Boolean isValidEmployeeEmail(String empEmail):
This method validates empEmail.
The email can be of pattern part1@part2
part1 can contain both upper case, lower case, and digits from 0-9
part1 can contain a single dot(.) or underscore() or hyphen(-)
dot(.) or underscore() or hyphen(-) shouldn't be the first or last character in the part1
dot(.) or underscore(_) or hyphen(-) shouldn't appear consecutively anywhere in the part1
part1 should be of a maximum of 64 characters
part2 can contain both upper case, lower case, and digits from 0-9
part2 shouldn’t contain any other special character except a dot(.)
dot(.) shouldn't be the first or last character in the part2
dot(.) shouldn't appear consecutively anywhere in part2.
part1 and part2 shouldn’t contain any white spaces.
If the above conditions are satisfied, return true, otherwise, return false.
Example-
Valid: Sachin_Tendulkar@infosys.com, SachinTendulkar@gmail.com, Sachin_Tendulkar123@yahoo.com, Sachi123.tendul@hotmail.com, Sachi.123tendul@hotmail.com
Invalid: Sachin Tendulkar@infosys.com, .SachinTendulkar@infosys.com, Sachin_Tendulkar@.infosys.com, Sachin..Tendulkar@infosys..com
public static Boolean isValidEmployeePassword(String empPassword):
This method validates empPassword.
It contains at least 8 characters and at most 20 characters.
It should contain at least one digit.
It should contain at least one upper case alphabet.
It should contain at least one lower case alphabet.
It should contain at least one special character which includes ‘@’, ’#’.
It should not contain any white space.
If the above conditions are satisfied, return true, otherwise, return false.
Example-
Valid: Sachin@1234, Jack#8989, Jack@Jill1234
Invalid: JackAndJill, Jack1234, 123456, jack#1234
public static Boolean isValidEmployeePhoneNo(Long empPhoneNo):
This method validates empPhoneNo.
PhoneNo must be a 10-digit number and all the numbers should not be the same.
If the above condition is satisfied, return true, otherwise, return false.
Example-
Valid: 7416306445,8179845754
Invalid: 741630644,9999999999
public static Boolean isValidEmployeeAge(Integer age):
This method validates the age of an Employee.
The ‘age’ must be greater than equals to 18 years and less than 45 years.
If the above conditions are satisfied, return true, otherwise, return false.
Example-
Valid: 18, 27, 35, 44.
Invalid: 17, 1, -1, 45, 46.
Fully implemented Employee.java, Main.java, and partially implemented Validator.java is already given to you.
Note:
Please don't alter/change the codes which have already provided.
Whatever class/interface you are adding OR already provided should not be 'public'.
Languages: Java
String empIdString = String.valueOf(empId);
return empIdString.matches("[0-9]{6,7}");