Update an Employee’s Address

You are given an employee represented by an Employee structure containing the following fields:

  • id: An integer representing the employee's unique identifier.
  • name: A string representing the employee's name.
  • address: An Address structure containing the employee's city and pinCode.

You are also given a new address containing a city and a PIN code.

Your task is to update the employee's existing address with the new address. Modify the original Employee object directly, replacing both its city and PIN code while keeping the employee's ID and name unchanged.

The function returns no value, as the changes are made directly to the employee passed by reference.

Note: The driver code constructs an Employee object and an Address object from the supplied inputs before calling updateAddress(). After the function executes, the driver prints the updated employee details.

Example 1:

Input: id = 101, name = "Rahul", city = "Delhi", pinCode = 110001, newCity = "Mumbai", newPinCode = 400001

Output: {id: 101, name: "Rahul", address: {city: "Mumbai", pinCode: 400001}}

Explanation: Replace the old city Delhi with Mumbai and the old PIN 110001 with 400001 in the original employee's address. The ID remains 101 and the name remains Rahul. Therefore, the employee now has the Mumbai address while keeping the same identity.

Example 2:

Input: id = 202, name = "Priya", city = "Pune", pinCode = 411001, newCity = "Bengaluru", newPinCode = 560001

Output: {id: 202, name: "Priya", address: {city: "Bengaluru", pinCode: 560001}}

Explanation: The original address Pune, 411001 is replaced by Bengaluru, 560001. Only the nested address changes: Priya's ID is still 202 and her name is still Priya. The update happens on the employee passed by reference.

Now Your Turn!

Pick the correct output for the given input

Input: id = 303, name = "Amit", city = "Jaipur", pinCode = 302001, newCity = "Chennai", newPinCode = 600001

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 0 <= id <= 109
  • 0 <= pinCode, newPinCode <= 999999
  • Name and both city strings contain 1 to 100 English letters and spaces, without leading or trailing spaces.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
/**
* Definition for an address.
* struct Address {
* std::string city;
* int pinCode;
* };
*
* Definition for an employee.
* struct Employee {
* int id;
* std::string name;
* Address address;
* };
*/
class Solution {
public:
void updateAddress(Employee& employee, const Address& newAddress) {
// Write your code here
}
};
 
Test Case

Input:

City
Id
Name
New City
New Pin Code
Pin Code