DEV Community

Pykash
Pykash

Posted on

Wipro talent Array Based Question.

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)

Collapse
 
xmachli profile image
XMachli

class Validator {
// ID Validation
public static Boolean isValidEmployeeId(Long empId) {
String empIdString = String.valueOf(empId);
return empIdString.matches("[0-9]{6,7}");
}

// Email Validation
public static Boolean isValidEmployeeEmail(String empEmail) {
    String[] parts = empEmail.split("@");
    if (parts.length != 2) {
        return false;
    }
    String part1 = parts[0];
    String part2 = parts[1];

    if (part1.isEmpty() || part2.isEmpty()) {
        return false;
    }

    if (part1.length() > 64 || part2.length() > 255) {
        return false;
    }

    if (part1.startsWith(".") || part1.endsWith(".") || part2.startsWith(".") || part2.endsWith(".")) {
        return false;
    }

    if (part1.contains("..") || part2.contains("..")) {
        return false;
    }

    if (part1.matches(".*[._-]{2,}.*")) {
        return false;
    }

    if (!part1.matches("[A-Za-z0-9._-]+") || !part2.matches("[A-Za-z0-9.-]+")) {
        return false;
    }

    return true;
}

// Password Validation
public static Boolean isValidEmployeePassword(String empPassword) {
    if (empPassword.length() < 8 || empPassword.length() > 20) {
        return false;
    }

    if (!Pattern.compile("[0-9]").matcher(empPassword).find()) {
        return false;
    }

    if (!Pattern.compile("[A-Z]").matcher(empPassword).find()) {
        return false;
    }

    if (!Pattern.compile("[a-z]").matcher(empPassword).find()) {
        return false;
    }

    if (!Pattern.compile("[@#]").matcher(empPassword).find()) {
        return false;
    }

    if (Pattern.compile("\\s").matcher(empPassword).find()) {
        return false;
    }

    return true;
}
Enter fullscreen mode Exit fullscreen mode

// 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+");
}

// DOB Validation
public static Boolean isValidEmployeeAge(Integer age) {
    return age >= 18 && age < 45;
}
Enter fullscreen mode Exit fullscreen mode

}

Collapse
 
xmachli profile image
XMachli

String[] parts = empEmail.split("@");
if (parts.length != 2) {
return false;
}
String part1 = parts[0];
String part2 = parts[1];

    if (part1.isEmpty() || part2.isEmpty()) {
        return false;
    }

    if (part1.length() > 64 || part2.length() > 255) {
        return false;
    }

    if (part1.startsWith(".") || part1.endsWith(".") || part2.startsWith(".") || part2.endsWith(".")) {
        return false;
    }

    if (part1.contains("..") || part2.contains("..")) {
        return false;
    }

    if (part1.matches(".*[._-]{2,}.*")) {
        return false;
    }

    if (!part1.matches("[A-Za-z0-9._-]+") || !part2.matches("[A-Za-z0-9.-]+")) {
        return false;
    }

    return true;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
xmachli profile image
XMachli

import java.util.Scanner;

class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Employee emp = new Employee();

    Long empId = sc.nextLong();
    emp.setEmpId(empId);

    String email = sc.next();
    emp.setEmpEmail(email);

    String password = sc.next();
    emp.setEmpPassword(password);

    Long phoneNo = sc.nextLong();
    emp.setEmpPhoneNo(phoneNo);

    Integer age = sc.nextInt();
    emp.setAge(age);

    // System.out.println(emp);

    Validator.validate(emp);
    sc.close();
}
Enter fullscreen mode Exit fullscreen mode

}

class Employee {
private Long empId;
private String empEmail;
private String empPassword;
private Long empPhoneNo;
private Integer age;

public Long getEmpId() {
    return empId;
}

public void setEmpId(Long empId) {
    this.empId = empId;
}

public String getEmpEmail() {
    return empEmail;
}

public void setEmpEmail(String empEmail) {
    this.empEmail = empEmail;
}

public String getEmpPassword() {
    return empPassword;
}

public void setEmpPassword(String empPassword) {
    this.empPassword = empPassword;
}

public Long getEmpPhoneNo() {
    return empPhoneNo;
}

public void setEmpPhoneNo(Long empPhoneNo) {
    this.empPhoneNo = empPhoneNo;
}

public Integer getAge() {
    return age;
}

public void setAge(Integer age) {
    this.age = age;
}
Enter fullscreen mode Exit fullscreen mode

}

class Validator {

public static void validate(Employee employee) {
    System.out.println(Validator.isValidEmployeeId(employee.getEmpId()));
    System.out.println(Validator.isValidEmployeeEmail(employee.getEmpEmail()));
    System.out.println(Validator.isValidEmployeePassword(employee.getEmpPassword()));
    System.out.println(Validator.isValidEmployeePhoneNo(employee.getEmpPhoneNo()));
    System.out.println(Validator.isValidEmployeeAge(employee.getAge()));

}

// ID Validation
public static Boolean isValidEmployeeId(Long empId) {
    return null;
}

// Email Validation
public static Boolean isValidEmployeeEmail(String empEmail) {
    return null;
}

// Password Validation
public static Boolean isValidEmployeePassword(String empPassword) {
    return null;
}

// PhoneNo Validation
public static Boolean isValidEmployeePhoneNo(Long empPhoneNo) {
    return null;
}

// DOB Validation
public static Boolean isValidEmployeeAge(Integer age) {
    return null;
}
Enter fullscreen mode Exit fullscreen mode

}

Collapse
 
xmachli profile image
XMachli

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

Collapse
 
xmachli profile image
XMachli

String empIdString = String.valueOf(empId);
return empIdString.matches("[0-9]{6,7}");