Grokking Oracle Coding Interview

0% completed

Hidden Document
Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content Hidden Document Content

.....

.....

.....

Like the course? Get enrolled and start learning!
Yan Pomanitskiy (Pliant GmbH)

Yan Pomanitskiy (Pliant GmbH)

· 2 years ago

  • The stack is processed in LIFO (Last-In-First-Out) order. Each component is popped and appended to the start of the result string, preceded by "/".

This is a bit wrong.

The stack contains [a, b, c]. Using LIFO will create a string: /c/b/a

It should be noted that for-each uses internal array-based representation.

Show 2 replies
Renat Zamaletdinov

Renat Zamaletdinov

· 2 years ago

So I ended up with that awful, yet working solution

class Solution:     def simplifyPath(self, path):         stack = []         l = len(path)         i = 0         while i < l:             char = path[i]             if char == '/':                 while stack and stack[-1] == '/':                     stack.pop()             elif char == '.':                 if i < l - 1 and path[i + 1] == '.':                     if stack and stack[-1] == '/':                         stack.pop()                     while stack and stack[-1] != '/':                         stack.pop()             if char != '.':                 stack.append(char)             i += 1         while stack and stack[-1] == '/':             stack.pop()         if not stack:             return '/'         re
K

krishi87

· a year ago

class Solution: def simplifyPath(self, path): stack = [] for index in range(len(path)): char = path[index] if stack: if char == "." and index != len(path)-1 and path[index+1] == char: # Go back a dir, pop twice stack.pop() if stack: stack.pop() # Always check for underflow while popping elif char == "/" and ((index != len(path)-1 and path[index+1] == char) or stack[-1] == char): # Simplify // to / by not pushing anything to stack continue if char not in ["."]: # Ignore . and push char to stack stack.append(char) return "".join(
Maharshi Jinandra

Maharshi Jinandra

· 3 years ago

return join statement uses stack as array ....

D

Davide Pugliese

· 2 years ago

In my view, even in Java, as for Python (where you have syntactic sugar like list comprehensions and tuple unpacking for example), the solution should leverage the language specific API.

In modern Java we don't use Stack anymore, so ArrayDeque should be used. In Java ArrayDeque is a double-ended queue that can be used to add elements to the head or to the tail and remove elements from the head and from the tail. Unlike LinkedList, ArrayDeque guarantees to retrieve the elements in the same order as they were entered when using an iterator (for loop).

import java.util.*; import java.util.stream.*; public class Solution { public String simplifyPath(String path) { Deque<String> stack = new ArrayDeque<>(); var patWoSlash = path.split("/");
Mohammed Dh Abbas

Mohammed Dh Abbas

· 2 years ago

class Solution:     def simplifyPath(self, path):                 stack = []         i = 0         temp = []  # for accumulating sub path characters like "foo/home". it stores foo and home         while i < len(path):             # if we have perviously accumulated path push it to the stack             if path[i] == "/" and len(temp) > 0:                 stack.append(''.join(temp))                 temp = []             # ".." case. pop the previous element             elif path[i] == "." and len(stack) > 0 and stack[-1] == '.':                 stack.pop()                 if len(stack) > 0:                     stack.pop()             # "/." case. pop the "."             elif path[i] == "/" and len(stack) > 0 and stack[-1] == '.':                 stack.pop()             #
Marie Arroyo

Marie Arroyo

· 2 years ago

As in, we go from step 4, to step 4. :)