Paycom 面試題彙總

Borris發表於2019-10-16

前言

由於週五要面 Paycom,昨天把網上搜到的面試題彙總了一下,簡單寫一下思路。題目都不難,主要是熟悉一下解題過程。

真題

1. Remove / Find duplicate characters in String

如果只是將遇到的重複的 char 移除的話,可以建立一個 HashSet 和一個 ArrayList。HashSet 判斷 char 是否重複,list 中按順序儲存不重複元素。

Traverse each character in the string
    if the character not in HashSet
        add it to both HashSet and ArrayList
convert the list to string by using StringBuilder and then toString
  • 如果不是 remove 是 find,可以用 HashSet 存已經出現的元素,另一個 HashSet 存重複元素。這樣可以保證找出的重複元素不重複。
  • 但如果遇到 LeetCode 316 的要求,remove 肯定不能這樣做了。鑑於 316 是道 hard,而面試基本上不可能考,等這些題全總結完回頭再做。
  • 還有一題是這樣的:Given an array of unknown length, how would you determine if there are multiple items repeated within.
    • 這題還是用 HashSet。If the item are not in the set, add it to the set; else return true (there are repeated items).

2. Recursion to solve Fibonacci sequence

首先找出前後項關係式:f(n) = f(n - 1) + f(n - 2)

  • Recursion
    public int fib(n) {
    if (n <= 1) return n;
    return fib(n - 1) + fib(n - 2);
    }
  • Dynamic Programming
    public int fib(n) {
    int[] f = new int[n];
    f[0] = 0; f[1] = 1;
    for (int i = 2; i < n; i++) {
        f[n] = f[n - 1] + f[n -2];
    }
    return f[n];
    }

3. Count number of each element in an array

不知道題目是不是這個意思。

用 HashTable 就行,沒出現的元素 map.put(e, 1);

出現的元素 map.put(e, map.get(e) + 1);

4. LeetCode 319 - Bulb Switcher

The intuitive way to solve the problem is to traverse n switchers by 1, 2, ..., n steps.
Initialize an array of length n with all zeros.

for i in 1 : n steps
    for j = i; j < n; j = j + i
        if the switch is on: turn off;
        else: turn on

這個方法在複雜度過高,在 n 很大時會報錯。 319 discuss 裡有數學解法和證明。

5. LeetCode 242 - Valid Anagram

How to tell two strings are anagram?

Convert them to two char arrays and sort them.
If the two sorted arrays are equal then they are anagram.
  • Or record the times of appearance for each character in an array of size 26, for string t, add the counter of each charater by 1 if it apprears, and for string t, minus the counter by 1 if it appears. The final values in the array should all be 0s. This takes O(n) time.
  • Advanced: Leetcode 49 - Group Anagrams - 使用 HashMap
  • Follow Up: What if the inputs contain unicode characters? How would you adapt your solution to such case?
    • Use HashMap to store each character and the times it appears.
    • Traverse String s first to count appearance of characters and add the times into each key.
    • Then Traverse String t to minus the count if the character appears. If the count of a chacter is 1, remove it. Or there is no count of such character, return false.
    • After the traverse, the map should be empty. If it is the case, return true. If not, return false.

6. To check whether one string is the substring of the other

7. Detect the start of a loop in a linked list

8. Find if a number was a stepping number (Best Solution)
9. Determine if a word is a palindrome using any pseudo code you want.

相關文章