Skip to main content

Lnt Mindtree part1

 1)Given an number N and an array of size N-1 containing N-1 numbers between 1 to N. Find the number(between 1 to N), that is not present in the given array.

let N = 5;

let Arr = [1, 2, 4, 5] 

sum=n*(n+1)/2

for(let i=0;i<arr.length;i++){

sum-=arr[i]

}

console.log(sum)

2)Input: pattern = "abba", s = "dog cat cat dog "
Output: true

function wordPattern(pattern, s) {
    const words = s.split(' ');
    if (pattern.length !== words.length) {
        return false;
    }
    
    const charToWord = new Map();
    
    for (let i = 0; i < pattern.length; i++) {
        const char = pattern[i];
        const word = words[i];
        
        if (charToWord.has(char)) {
            if (charToWord.get(char) !== word) {
                return false;
            }
        } else {
            charToWord.set(char, word);
        }
    }
    
    return true;
}

// Example usage:
const pattern1 = "abba";
const s1 = "dog cat cat dog";
console.log(wordPattern(pattern1, s1)); // Output: true

const pattern2 = "abba";
const s2 = "dog cat cat fish";
console.log(wordPattern(pattern2, s2)); // Output: false


3)objects in typescript?

In TypeScript, as in JavaScript, objects are collections of key-value pairs where keys are strings (or symbols) and values can be of any data type, including other objects. TypeScript offers static typing and additional features for defining and working with objects.

There are several ways to define objects in TypeScript:

Literal Syntax: You can define objects using literal syntax, which involves enclosing key-value pairs within curly braces {}.
typescript
Copy code
let person: { name: string, age: number } = {
    name: "John",
    age: 30
};
Interface: Interfaces in TypeScript are a powerful way to define the shape of an object. They provide a contract for what properties and methods an object should have.
typescript
Copy code
interface Person {
    name: string;
    age: number;
}

let person: Person = {
    name: "John",
    age: 30
};
Type Alias: You can also use type aliases to define an object type.
typescript
Copy code
type Person = {
    name: string;
    age: number;
};

let person: Person = {
    name: "John",
    age: 30
};
Class: You can define objects using classes, which provide a blueprint for creating objects with specific properties and methods.
typescript
Copy code
class Person {
    name: string;
    age: number;

    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
}

let person = new Person("John", 30);
In all these examples, person is an object with properties name and age. TypeScript provides type checking and IntelliSense support, which helps catch errors at compile-time and provides better code completion and documentation when working with objects.


4)Enum in typescript?
In TypeScript, enum is a way to define a set of named constants. It allows you to define a collection of related values that can be numeric or string-based. Enums are useful when you have a fixed set of values that are known at compile time.

Here's how you can define and use enums in TypeScript:

Numeric Enums:
Numeric enums are auto-incremented starting from 0, but you can also set their values explicitly.

typescript
Copy code
enum Direction {
    Up,
    Down,
    Left,
    Right
}

let move: Direction = Direction.Up;
console.log(move); // Output: 0
console.log(Direction[move]); // Output: "Up"
In this example, Direction is a numeric enum where Up has the value 0, Down has 1, Left has 2, and Right has 3.

String Enums:
String enums have string values associated with each enum member.

typescript
Copy code
enum Direction {
    Up = "UP",
    Down = "DOWN",
    Left = "LEFT",
    Right = "RIGHT"
}

let move: Direction = Direction.Up;
console.log(move); // Output: "UP"
console.log(Direction[move]); // Output: "UP"
In this example, Direction is a string enum where each enum member has an associated string value.

Heterogeneous Enums:
You can mix string and numeric members in the same enum, but it's not common.

typescript
Copy code
enum BooleanLikeHeterogeneousEnum {
    No = 0,
    Yes = "YES"
}
Reverse Mapping:
Enums in TypeScript support reverse mapping, which means you can access the name of the enum member by its value.

typescript
Copy code
enum Direction {
    Up,
    Down,
    Left,
    Right
}

console.log(Direction.Up); // Output: 0
console.log(Direction[0]); // Output: "Up"
Enums in TypeScript provide a convenient way to work with sets of constants, making your code more readable and maintainable. They can be particularly useful when you have a finite set of options or states in your application.









Comments

Popular posts from this blog

Globant part 1

 1)call,apply,bind example? Ans: a. call Method: The call method is used to call a function with a given this value and arguments provided individually. Javascript code: function greet(name) {   console.log(`Hello, ${name}! I am ${this.role}.`); } const person = {   role: 'developer' }; greet.call(person, 'Alice'); // Output: Hello, Alice! I am developer. In this example, call invokes the greet function with person as the this value and passes 'Alice' as an argument. b. apply Method: The apply method is similar to call, but it accepts arguments as an array. Javascript code: function introduce(language1, language2) {   console.log(`I can code in ${language1} and ${language2}. I am ${this.name}.`); } const coder = {   name: 'Bob' }; introduce.apply(coder, ['JavaScript', 'Python']); // Output: I can code in JavaScript and Python. I am Bob. Here, apply is used to invoke introduce with coder as this and an array ['JavaScript', 'Pyt...

Node.js: Extract text from image using Tesseract.

In this article, we will see how to extract text from images using Tesseract . So let's start with this use-case, Suppose you have 300 screenshot images in your mobile which has an email attribute that you need for some reason like growing your network or for email marketing. To get an email from all these images manually into CSV or excel will take a lot of time. So now we will check how to automate this thing. First, you need to install Tesseract OCR( An optical character recognition engine ) pre-built binary package for a particular OS. I have tested it for Windows 10. For Windows 10, you can install  it from here. For other OS you make check  this link. So once you install Tesseract from windows setup, you also need to set path variable probably, 'C:\Program Files\Tesseract-OCR' to access it from any location. Then you need to install textract library from npm. To read the path of these 300 images we can select all images and can rename it to som...

CSS INTERVIEW QUESTIONS SET 2

  You make also like this CSS interview question set 1. Let's begin with set 2, 5)What is the difference between opacity 0 vs display none vs visibility hidden? Property           | occupies space | consumes clicks | +--------------------+----------------+-----------------+ | opacity: 0         |        yes      |        yes       | +--------------------+----------------+-----------------+ | visibility: hidden |        yes       |        no        | +--------------------+----------------+-----------------+ | display: none      |        no       |        no        | When we say it consumes click, that means it also consumes other pointer-events like onmousedown,onmousemove, etc. In e...