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

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

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

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 empIdString = String.valueOf(empId);
return empIdString.matches("[0-9]{6,7}");