Directi Interview Question for SDE1s


Country: India
Interview Type: Written Test




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

In the input string, only two characters are important: The firsr and the 2nd last character (provided the last character is '?'). Consider the following cases:
1)If the first and the second last characters are equal, then we the output can be either of the other two letters. But we need the character which comes first in alphabetical order. So if the first character is 'a', then the output will be 'b', in all other cases it will be 'a'.
2) If the first and the second last characters are unequal, then we will have to give the remaining character as output. Eg. if the first and second last characters are 'a' and 'c' are unequal, then 'b' will be the output. So I have written the following code. It uses a bitwise XOR operator, see if it helps you:

#include<stdio.h>

void main()
{
    char str[100];
    int i;
    scanf("%s", str);
    for(i=0; str[i]!='\0'; i++);
        i=(str[0]^str[i-1]);
        printf("The character is=");
    if(i==0)
    {
        if(str[0]=='a')

            printf("b");
        else
            printf("a");
    }
    else
    printf("%c", 96+i);



}

- 123ayanjit August 31, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In this code, you have to omit giving the '?' in the end. I forgot about that while writing.

- 123ayanjit August 31, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;

/*  A string can contain only a, b or c. 
    There cannot be 2 consecutive same character. 
    First and the last character cannot be the same. 
    Now given a string with ‘a’, ‘b’, ‘c’ or ‘?’. 
    We need to find the string replacing ‘?’ that 
    satisfy the above conditions. For multiple-answer 
    display lexicographically smallest string. For no 
    answer possible display “Not Possible”. */

namespace ReplaceQuestionMarks
{
    class myClass 
    {
        static void Main()
        {
            string original = "ab?";
            char[] solution = replaceChars(original.ToCharArray());
            Console.WriteLine($"Original: {original}, solution: {(Array.IndexOf(solution, '?') >= 0 ? "Not Possible" : new string(solution))}");
        }

        public static char[] replaceChars(char[] soFar)
        {
            if (soFar == null || soFar.Length < 1) return soFar;
            int nextQuestionMark = Array.IndexOf(soFar, '?');
            if (nextQuestionMark < 0) return soFar;
            char[] nextAttempt = (char[])soFar.Clone();
            nextAttempt[nextQuestionMark] = 'a';
            if (isValid(nextAttempt)) return replaceChars(nextAttempt);
            nextAttempt[nextQuestionMark] = 'b';
            if (isValid(nextAttempt)) return replaceChars(nextAttempt);
            nextAttempt[nextQuestionMark] = 'c';
            if (isValid(nextAttempt)) return replaceChars(nextAttempt);
            return soFar;
        }

        public static bool isValid(char[] soFar)
        {
            // Single character strings are always valid
            if (soFar.Length < 2) return true;

            // Ensure first and last are different
            if (soFar[0] == soFar[soFar.Length - 1]) return false;

            // Ensure no neighbors match
            for (int index = 1; index < soFar.Length; index++)
            {
                if (soFar[index] != '?' && (soFar[index] == soFar[index - 1] || (index < soFar.Length - 1 && soFar[index] == soFar[index + 1])))
                    return false;
            }

            // It's valid
            return true;
        }
    }
}

- Jeff September 03, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;

/*  A string can contain only a, b or c. 
    There cannot be 2 consecutive same character. 
    First and the last character cannot be the same. 
    Now given a string with ‘a’, ‘b’, ‘c’ or ‘?’. 
    We need to find the string replacing ‘?’ that 
    satisfy the above conditions. For multiple-answer 
    display lexicographically smallest string. For no 
    answer possible display “Not Possible”. */

namespace ReplaceQuestionMarks
{
    class myClass 
    {
        static void Main()
        {
            string original = "ab?";
            char[] solution = replaceChars(original.ToCharArray());
            Console.WriteLine($"Original: {original}, solution: {(Array.IndexOf(solution, '?') >= 0 ? "Not Possible" : new string(solution))}");
        }

        public static char[] replaceChars(char[] soFar)
        {
            if (soFar == null || soFar.Length < 1) return soFar;
            int nextQuestionMark = Array.IndexOf(soFar, '?');
            if (nextQuestionMark < 0) return soFar;
            char[] nextAttempt = (char[])soFar.Clone();
            nextAttempt[nextQuestionMark] = 'a';
            if (isValid(nextAttempt)) return replaceChars(nextAttempt);
            nextAttempt[nextQuestionMark] = 'b';
            if (isValid(nextAttempt)) return replaceChars(nextAttempt);
            nextAttempt[nextQuestionMark] = 'c';
            if (isValid(nextAttempt)) return replaceChars(nextAttempt);
            return soFar;
        }

        public static bool isValid(char[] soFar)
        {
            // Single character strings are always valid
            if (soFar.Length < 2) return true;

            // Ensure first and last are different
            if (soFar[0] == soFar[soFar.Length - 1]) return false;

            // Ensure no neighbors match
            for (int index = 1; index < soFar.Length; index++)
            {
                if (soFar[index] != '?' && (soFar[index] == soFar[index - 1] || (index < soFar.Length - 1 && soFar[index] == soFar[index + 1])))
                    return false;
            }

            // It's valid
            return true;
        }
    }
}

- Jeff September 03, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void replace(String str) {
if (str == null || str.length() == 0) {
return;
}
char[] chars = str.toCharArray();

for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (c == '?') {
if (isEligible(chars, 'a', i)) {
chars[i] = 'a';
} else if (isEligible(chars, 'b', i)) {
chars[i] = 'b';
} else if (isEligible(chars, 'c', i)) {
chars[i] = 'c';
} else {
System.out.println("Not possible");
}
}
}
System.out.println(new String(chars));

}

private static boolean isEligible(char[] chars, char c, int i) {
int next = (i == chars.length - 1) ? 0 : i + 1;
int prev = (i == 0) ? chars.length - 1 : i - 1;

if (chars[prev] == c || chars[next] == c) {
return false;
}
return true;
}

- Sati September 03, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

"public static void replace(String str) {
if (str == null || str.length() == 0) {
return;
}
char[] chars = str.toCharArray();

for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (c == '?') {
if (isEligible(chars, 'a', i)) {
chars[i] = 'a';
} else if (isEligible(chars, 'b', i)) {
chars[i] = 'b';
} else if (isEligible(chars, 'c', i)) {
chars[i] = 'c';
} else {
System.out.println("Not possible");
}
}
}
System.out.println(new String(chars));

}

private static boolean isEligible(char[] chars, char c, int i) {
int next = (i == chars.length - 1) ? 0 : i + 1;
int prev = (i == 0) ? chars.length - 1 : i - 1;

if (chars[prev] == c || chars[next] == c) {
return false;
}
return true;
}"

- Sati September 03, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void replace(String str) {
        if (str == null || str.length() == 0) {
            return;
        }
        char[] chars = str.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (c == '?') {
                if (isEligible(chars, 'a', i)) {
                    chars[i] = 'a';
                } else if (isEligible(chars, 'b', i)) {
                    chars[i] = 'b';
                } else if (isEligible(chars, 'c', i)) {
                    chars[i] = 'c';
                } else {
                    System.out.println("Not possible");
                }
            }
        }
        System.out.println(new String(chars));

    }

    private static boolean isEligible(char[] chars, char c, int i) {
        int next = (i == chars.length - 1) ? 0 : i + 1;
        int prev = (i == 0) ? chars.length - 1 : i - 1;

        if (chars[prev] == c || chars[next] == c) {
            return false;
        }
        return true;
    }

- Sati September 03, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{public static void replace(String str) {
if (str == null || str.length() == 0) {
return;
}
char[] chars = str.toCharArray();

for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (c == '?') {
if (isEligible(chars, 'a', i)) {
chars[i] = 'a';
} else if (isEligible(chars, 'b', i)) {
chars[i] = 'b';
} else if (isEligible(chars, 'c', i)) {
chars[i] = 'c';
} else {
System.out.println("Not possible");
}
}
}
System.out.println(new String(chars));

}

private static boolean isEligible(char[] chars, char c, int i) {
int next = (i == chars.length - 1) ? 0 : i + 1;
int prev = (i == 0) ? chars.length - 1 : i - 1;

if (chars[prev] == c || chars[next] == c) {
return false;
}
return true;
}
}}}

- Sati September 03, 2019 | 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