Yelp Interview Question


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 2 vote

-Firstly we take a/b in a string called currentdirectory
-Then we split "c/../d/e/../f" with delimiter as "/"
-When there is a ".." then we take a substring of the currentdirectory
-If there is a character, then we will append "/" and the character

public class FindResultantDirectory {

	private static String findResultingDirectory(String currentDirectory,
			String sequenceOfOperations) {

		String splitDirectories[] = sequenceOfOperations.split("/");

		for (int i = 0; i < splitDirectories.length; i++) {
			if (splitDirectories[i].equals("..")) {
				currentDirectory = currentDirectory.substring(0,
						currentDirectory.length() - 2);
			} else {
				currentDirectory = currentDirectory + "/" + splitDirectories[i];
			}
		}

		return currentDirectory;
	}

	public static void main(String[] args) {
		System.out.println(findResultingDirectory("a/b", "c/../d/e/../f"));
	}
}

- dhirajb1989 May 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

We can use a stack for this problem.
Step 1: If destination doesn't start with root, enter source directories to stack
Step 2: Split source and destination around match of regular expression "/" and put each of them in array.
Step 3: If a ".." pattern is found, pop the last added directory from array, otherwise push it to array.
Step 4: to get the end directory, look at the topmost element of the array

package career.cup.yelp;

import java.util.ArrayList;
import java.util.Stack;

public class getDirectory {
	
	public static void main(String args[]){
		
		getDirectiry("/logs", "test/nodes/../modules/logs");
		getDirectiry("/logs", "test/nodes/../modules/logs/..");
	}
	
	public static void getDirectiry(String src, String destination){
		
		Stack<String> directoryStack = new Stack<String>();
		
		String[] listDestination = destination.split("/");
		String[] listHost = src.split("/");
		
		if(!destination.startsWith("/")){
			for(String str : listHost){
				if(!(str.matches(".."))){
					directoryStack.push(str);
				}
				else if((str.matches(".."))){
					directoryStack.pop();
				}
			}
		}
		
		for(String str : listDestination){
			if(!(str.matches(".."))){
				directoryStack.push(str);
			}
			else if((str.matches(".."))){
				directoryStack.pop();
			}
		}
		
		System.out.println(directoryStack);
	}

}

- hm February 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can implement it by using a stack. At any point, top of the stack will be the present working directory. Add or remove elements to/from the stack according to the directory changing operation being performed.

- Kingsaint May 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What if directory names are longer than one character? Using currDir = currDir.substring(0, currDir.length()-2) wouldn't be correct in that case. Instead, we could find the occurrence of the last slash by using currDir.lastIndexOf("/") and take the substring from 0 to this index.

- Anonymous December 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class cdImplementation
{
  public static void main(String args[])
  {
    //('a/b','c/../d/e/..f')
    String input[] = new String[]{"a/b","c/../d/e/../f"};
    Stack<String> stack = new Stack<String>();
    String str = "";
    for (String word : input)
      for (String i : word.split("/"))
      {
        if (!i.equals(".."))
          stack.push(i);
        else
          stack.pop();
      }
    while (!stack.isEmpty())
    {
      str = stack.pop() + "/" + str ;
    }
    System.out.println(str);
  }
}

- Dee April 11, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More