Interview Question


Country: United States




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

C++ implementation assuming pattern cannot contain 0.

string specialSplit(const string& input, vector<int>& pattern, char separator)
{
  int index = 0;
  int count = 0;
  string result("");

  if(pattern.size() < 0) return result;

  for(int i = 0; i < input.length(); ++i)
  {
    if(index < (pattern.size() - 1) && pattern[index] == 0) 
    {
      result += separator;
      count = 0;
      index++;
    }

    result += input[i];
    count++;
    pattern[index]--;
  }
  
  if(pattern[index] > 0) 
    throw "Not enough chars in input!";
  else if(pattern[index] < 0) 
    throw "Too many chars in input!";
  
  return result;
}

int main()
{
  vector<int> pattern;
  pattern.push_back(1);
  pattern.push_back(2);
  try
  {
    cout << specialSplit("Cup", pattern, ';') << endl;
    // cout << specialSplit("Cu", pattern, ';') << endl;
    // cout << specialSplit("Cupp", pattern, ';') << endl;
  }
  catch(const char* ex)
  {
    cout << ex << endl;
  }

  return 0;
}

- Anonymous October 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C++ solution assuming pattern cannot contain 0.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

string specialSplit(const string& input, vector<int>& pattern, char separator)
{
  int index = 0;
  int count = 0;
  string result("");

  if(pattern.size() < 0) return result;

  for(int i = 0; i < input.length(); ++i)
  {
    if(index < (pattern.size() - 1) && pattern[index] == 0) 
    {
      result += separator;
      count = 0;
      index++;
    }

    result += input[i];
    count++;
    pattern[index]--;
  }
  
  if(pattern[index] > 0) 
    throw "Not enough chars in input!";
  else if(pattern[index] < 0) 
    throw "Too many chars in input!";
  
  return result;
}

int main()
{
  vector<int> pattern;
  pattern.push_back(1);
  pattern.push_back(2);
    
  try
  {
    cout << specialSplit("Cup", pattern, ';') << endl; 
    // cout << specialSplit("Cu", pattern, ';') << endl; 
    // cout << specialSplit("Cupp", pattern, ';') << endl; 
  }
  catch(const char* ex) 
  { 
    cout << e << endl; 
  }
   
  return 0;
}

- Anon October 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;

public class SplitTheString {

class SplitTheStringException extends Exception{
public SplitTheStringException(String str){
super(str);
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
String inputString = null;
String inputPattern = null;
String inputPatternWithoutSpace = null;
String separator = null;
char [] arrPattern = null;
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the String : ");
inputString = scanner.nextLine();

System.out.println("Enter the pattern : ");
inputPattern = scanner.nextLine();
inputPatternWithoutSpace = inputPattern.replaceAll("\\s", "");
arrPattern = inputPatternWithoutSpace.toCharArray();

System.out.println("Enter the separator: ");
separator = scanner.nextLine();
try{
System.out.println("Result : " +stringSeparator(inputString, arrPattern, separator));
}catch(Exception e){
e.printStackTrace();
}
}

public static String stringSeparator(String str, char[] arr, String separator) throws Exception{
StringBuilder strBuilder = new StringBuilder();
int startPosition = 0;
for(int i =0; i<arr.length; i++){
int endPosition = startPosition + Character.getNumericValue(arr[i]);
if(startPosition <= str.length() && endPosition <= str.length()){
strBuilder.append(str.substring(startPosition, endPosition));
if(i != arr.length-1)
strBuilder.append(separator);
startPosition = endPosition;
}else throw new SplitTheString().new SplitTheStringException("Invalid Pattern");
}
return strBuilder.toString();
}
}

- ABHISEK October 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
#include "stdafx.h"
#include "vector"
#include "iostream"
#include "algorithm"

using namespace std;

string specialSplit(const string& input, vector<int>& pattern, char separator)
{
int sum = 0;
string result;
for_each(pattern.begin(), pattern.end(), [&sum](int x){ sum += x; });

if (sum != input.size())
{
return nullptr;
}
int curIndex = 0;
for (auto pat : pattern)
{
result.append(input.substr(curIndex, pat));
result.append(1, separator);
curIndex += pat;
}
return result;
}

int _tmain(int argc, _TCHAR* argv[])
{
string input("abcdefghijklmn");
vector<int> v = {2,3,4,5};

cout << specialSplit(input, v, ',').c_str() << endl;

return 0;
}
}

- Shubha Debnath October 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.sunbeam;

import java.util.Scanner;

public class Program
{
public static void main(String[] args)
{
int j=0,k=0,z=0;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter String:");
String a=scanner.nextLine();
int arr[]= {2,1,4};
String pattern[] = {" "," "," "};
for(int i=0;i< a.length();i++)
{
if(k==arr[j])
{
System.out.printf("%s",pattern[z]);
j++;
k=0;
z++;
}
System.out.printf("%s",a.charAt(i));
k++;
}
}
}

- kedi March 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;

public class StringSplit extends Exception {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String input, pattern, seperator;
		char[] arrPattern ;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the String");
		input =sc.nextLine();
		System.out.println("Enter the pattern with spae between them");
		pattern = sc.nextLine();
		System.out.println("Enter the seperator");
		seperator = sc.nextLine();
		pattern=pattern.replaceAll("\\s","");
		arrPattern = pattern.toCharArray();
		splitSting(input, arrPattern, seperator);
	}

	public static void splitSting(String input, char[] patttern, String seperator) {
		int beginIndex = 0;
		String output;
		int sum =0;
		for(int i=0; i<patttern.length; i++){
			sum+=Character.getNumericValue(patttern[i]);
		}

		if(sum<input.length() || sum>input.length()){
			try {
				throw new StringSplit();
			} 
			catch (StringSplit e) {
				System.out.println(e+" Exception:Invalid Pattern");;
			}
		}
		else{
			try{
				for(int i=0 ;i<patttern.length; i++){
					int endIndex = beginIndex + Character.getNumericValue(patttern[i]);
					output=input.substring(beginIndex, endIndex);
					beginIndex = beginIndex + Character.getNumericValue(patttern[i]);
					if(endIndex<input.length()){
						System.out.print(output+seperator);
					}
					else
						System.out.print(output);
				}
			}
			catch(Exception e){
				System.out.println(e);
			}
		}
	}
}

- sahilchalke3 August 07, 2016 | 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