---
# System prepended metadata

title: 蔡 interview

---

# 蔡 interview

## Q1.
取出工作經歷(employmentHistory)中，任職最久的一份工作
```php=
<?php

$payload = '{
  "employmentHistory": [
    {
      "employer": "JKL Inc.",
      "position": "Web Developer",
      "startDate": "2008-01-01",
      "endDate": "2009-12-31"
    },
    {
      "employer": "ABC Inc.",
      "position": "Software Engineer",
      "startDate": "2010-01-01",
      "endDate": "2012-06-30"
    },
    {
      "employer": "MNO Corp.",
      "position": "Software Engineer",
      "startDate": "2010-01-01",
      "endDate": "2011-12-31"
    },
    {
      "employer": "XYZ Corp.",
      "position": "Senior Software Engineer",
      "startDate": "2012-07-01",
      "endDate": "2013-12-31"
    },
    {
      "employer": "UVW Inc.",
      "position": "Senior Software Engineer",
      "startDate": "2012-01-01",
      "endDate": "2014-12-31"
    },
    {
      "employer": "Acme Corp.",
      "position": "Lead Software Engineer",
      "startDate": "2014-01-01",
      "endDate": "2015-12-31"
    },
    {
      "employer": "DEF Inc.",
      "position": "Software Developer",
      "startDate": "2016-01-01",
      "endDate": "2017-12-31"
    },
    {
      "employer": "GHI Corp.",
      "position": "Systems Analyst",
      "startDate": "2018-01-01",
      "endDate": "2019-12-31"
    },
    {
      "employer": "LMN Inc.",
      "position": "Senior Systems Analyst",
      "startDate": "2020-01-01",
      "endDate": "2021-06-30"
    },
    {
      "employer": "PQR Corp.",
      "position": "Software Engineer",
      "startDate": "2021-07-01",
      "endDate": null
    }
  ]
}';

$payload_array = json_decode($payload_array, true);
$new_array = [];
$time_array = [];

foreach ($payload_array['employmentHistory'] as $item) {
    $start_date = $item['startDate']; //轉為 timestamp
    $end_date = ($item['endDate']!==null) ?$item['endDate'] : time();
    $times = ($item['startDate']-$item['endDate']);
    $new_array[$times] = $item;
    $time_array[] = $times;
}
```

$max_time = $time_array//取最大值
$answer = $new_array[$max_time];


## Q2. 
Valid Parentheses
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Every close bracket has a corresponding open bracket of the same type.

```php=
<?php

    function isValid($s) {
        $array = explode("", $s);
        foreach ($array as $key) {
            
        }
    }

/**
Example 1:
Input: s = "()"
Output: true
({[}])
Example 2:
Input: s = "()[]{}"
Output: true

Example 3:
Input: s = "(]"
Output: false

Example 4:
Input: s = "{[({})]}"
Output: true
**/
```