Uber | Phone screen | Group anagrams

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2019-06-25Added By Anonymous
Source: Link

Company: Uber
Role: software engineer i or software engineer ii
Interview Stage: Phone Screen
Location: unknown
Year: unknown

Original Post Content:

Question 1:
https://leetcode.com/problems/group-anagrams

My Answer:

func groupAnagrams(_ strs: [String]) -> [[String]] {
    var dict = [String: [String]]()
    for str in strs {
        let sortedString = String(str.sorted())
        if dict[sortedString] == nil {
            dict[sortedString] = [str]
        } else {
            dict[sortedString]?.append(str)
        }
    }
    return Array(dict.values)
}

Question 2:
Find a view from a hierarchy clone given the B in a hierarchy find the b from the clone.
Image showing original and clone view hierarchies

As in the above image the "clone @" is clone of "original A". Given B find b from "Clone @"

if let view = findView(B, in: a) {
 print("Found view \(view.tag)")
 } else {
 print("No view found!")
 }

//solution
func findView(_ view: UIView, in cloneView: UIView) -> UIView? {
    var indices = [Int]() 
    var viewB = view
    while let parentOfB = viewB.parentView { //Get the parentview of B and iterate it until it reach no parents which is the root
        let bIndex = parentOfB.Views.indexOf(viewB)  // get index of the child view 
        indices.append(bIndex) //save it in array
        viewB = parentOfB 
    }
    let newView: UIView
    for indx in indices.reversed() { //iterate cloneView to find b. Reverse it as we are starting from parent view to child
        newView = (cloneView.views)[indx] 
        
    }
    return newView
}

Note: The second code snippet is presented as is from the original post, including any potential logical inconsistencies or incomplete parts.

Comments