TATA Consultancy Services Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

#include <iostream>
#include <string>
#include <list>
#include <conio.h>

using namespace std;

list<string> insert_spaces(string str)
{
    list<string> result;
    string temp;
    for(unsigned int i = 1; i < 2 << str.size() - 2; i++)
    {
        int f = i, j = 1;
		temp = str;
        while(f != 0)
        {
            if(f % 2 == 1)
				temp.insert(j++, "_");
            f /= 2;
			++j;
        }
        result.push_back(temp);
        temp.clear();
    }
    return result;
}

int main()
{
    list<string> string_list = insert_spaces("abcd");
    for( list<string>::iterator i = string_list.begin(); i != string_list.end(); i++)
    {
        cout << *i << endl;
    }
	_getch();
    return 0;
}

- AndrewRadkovich August 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class Spaces {
    
    public List<String> getSpaces(String initial){
        String[] chars = initial.split("");
        List<String> charList = Arrays.asList(chars);
        List<String> pals = new ArrayList<String>();
        getCombinaciones(charList.get(1), charList, pals, 2);
        return pals;
    }

    private void getCombinaciones(String curString, List<String> charList, List<String> pals, int i) {
        if(i >= charList.size()){
             pals.add(curString);
        }else{
           getCombinaciones(curString + "_" + charList.get(i), charList, pals, i+1);
           getCombinaciones(curString + "" + charList.get(i), charList, pals, i+1);
        }
    }
    
    public static void main(String[] args) {
        Spaces program = new Spaces();
        List<String> comb = program.getSpaces("abcd");
        
        for (String string : comb) {
            System.out.println(string);
        }
    }

- Adrian Garcia August 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is a_bc_d is valid along with a_b_cd, ab_cd and ab_c_d ?

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

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <bitset>

using namespace std;

string insert_spaces(string x, string y){
	int i;
	string result;
	for(i=0;i<x.length();i++){
		result += x[i];
		if(y[i]!='0'){
			result += "_";
		}
	}
	return result;
}
int main(){
	int i;
	string bitcode;
	for(i=0;i<8;i++){
		bitset<3> x(i);
		bitcode = x.to_string();
		bitcode += '0'; 
		cout << insert_spaces("abcd",bitcode) << endl;
	}
}

- Jason August 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it returns all the string combinations in form of array.

public static string[] string_combinations(string s)
        {
            //points to main array index
            int index = 0;
            //Create an array length of n(n+1)/2
            string[] combiArray = new string[(s.Length * (s.Length + 1))/2];
            combiArray[index++] = s;
            for (int i = 1; i < s.Length; i++)
            {    
                //recursive call on remaining string 
                //eg: if s = abcd and i = 1 recursive call of (bcd)
                //i = 2 recursive call of (cd)
                //i = 3 recursive call of (d)
                string[] subCombinations = string_combinations(s.Substring(i));                                   
                foreach (string item in subCombinations)
                {
                    if(item != null)
                        combiArray[index++] = s.Substring(0,i) + "_" + item;                                        
                }                                                                
            }
            return combiArray;
        }

- dirishkumar August 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

at line 6 : string[] combiArray = new string[(s.Length * (s.Length + 1))/2]; this should be changed to

string[] combiArray = new string[(s.Length * (s.Length + 1))];

- Anonymous August 05, 2014 | Flag


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